bp_cmd Parsing API

Stateless command-line parsing and validation API for Bus Pirate commands.


Action Resolution

Match the first non-flag token against the command’s actions[] array.

bool bp_cmd_get_action(const bp_command_def_t *def, uint32_t *action);
ParameterDescription
defCommand definition containing actions array
actionOutput: matched action enum value
Returnstrue if action verb found, false otherwise

If the definition has a delegate instead of an actions array, the delegate function is called. Action enum values should start at 1 (0 = no action).

uint32_t action = 0;
if (bp_cmd_get_action(&dummy_def, &action)) {
    printf("Action: %s (enum=%d)\r\n",
           (action == DUMMY_INIT ? "init" : "test"), action);
}

Simple Flag Queries

Stateless functions that re-scan the command line buffer on each call. No validation against constraints.

bool bp_cmd_find_flag(const bp_command_def_t *def, char flag);
bool bp_cmd_get_uint32(const bp_command_def_t *def, char flag, uint32_t *value);
bool bp_cmd_get_int32(const bp_command_def_t *def, char flag, int32_t *value);
bool bp_cmd_get_float(const bp_command_def_t *def, char flag, float *value);
bool bp_cmd_get_string(const bp_command_def_t *def, char flag, char *buf, size_t maxlen);
FunctionReturns true when
bp_cmd_find_flagFlag is present (no value consumed)
bp_cmd_get_uint32Flag found with valid uint32 (supports 0x, 0b, decimal)
bp_cmd_get_int32Flag found with valid int32
bp_cmd_get_floatFlag found with valid float
bp_cmd_get_stringFlag found with string value copied into buf

All functions accept the def to correctly skip other flags’ consumed values during scanning.

Simple Positional Queries

Stateless functions that locate non-flag tokens by index. Flags and their consumed values are skipped automatically using the definition.

bool bp_cmd_get_positional_string(const bp_command_def_t *def, uint32_t pos, char *buf, size_t maxlen);
bool bp_cmd_get_positional_uint32(const bp_command_def_t *def, uint32_t pos, uint32_t *value);
bool bp_cmd_get_positional_int32(const bp_command_def_t *def, uint32_t pos, int32_t *value);
bool bp_cmd_get_positional_float(const bp_command_def_t *def, uint32_t pos, float *value);
PositionMeaning
0Command name
1First user argument
2+Subsequent arguments

Remainder Access

Raw pointer to everything after the command name. The returned region is not null-terminated; use *len.

bool bp_cmd_get_remainder(const bp_command_def_t *def, const char **out, size_t *len);

Returns true if any content exists after the command name.

Constraint-Aware Resolution

These functions use the bp_val_constraint_t attached to the option or positional descriptor for type-aware parsing and range validation.

bp_cmd_status_t bp_cmd_positional(const bp_command_def_t *def, uint32_t pos, void *out);
bp_cmd_status_t bp_cmd_flag(const bp_command_def_t *def, char flag, void *out);
bp_cmd_status_t bp_cmd_prompt(const bp_val_constraint_t *con, void *out);

Behaviour differences:

  • bp_cmd_flag() — writes the constraint’s default value to out when the flag is absent (BP_CMD_MISSING).
  • bp_cmd_positional() — does not prompt; returns immediately.
  • bp_cmd_prompt() — drives an interactive prompt loop using the constraint. Loops on invalid input until the user provides a valid value or cancels.

Status codes:

StatusMeaning
BP_CMD_OKValue obtained and valid
BP_CMD_MISSINGNot on command line (default written for flags)
BP_CMD_INVALIDPresent but failed validation (error already printed)
BP_CMD_EXITUser cancelled interactive prompt

Help System

bool bp_cmd_help_check(const bp_command_def_t *def, bool help_flag);
void bp_cmd_help_show(const bp_command_def_t *def);
FunctionDescription
bp_cmd_help_checkIf help_flag is true, displays help and returns true (caller should return)
bp_cmd_help_showUnconditional help display

Help output is auto-generated from the definition: usage examples, flags table, and actions list.

Flag Syntax

All flag query functions accept these formats:

FormatExample
-f value-i 123
-f=value-i=123
--long value--integer 123
--long=value--integer=123

Usage Example

From src/commands/global/dummy.c — a reference command exercising every API group:

void dummy_handler(struct command_result* res) {
    // 1. Help gate
    if (bp_cmd_help_check(&dummy_def, res->help_flag)) return;

    // 2. Action resolution
    uint32_t action = 0;
    if (bp_cmd_get_action(&dummy_def, &action)) {
        printf("Action: %s (enum=%d)\r\n",
               (action == DUMMY_INIT ? "init" : "test"), action);
    }

    // 3. Boolean flag
    bool b_flag = bp_cmd_find_flag(&dummy_def, 'b');

    // 4. Constraint-aware flag with status handling
    uint32_t value;
    bp_cmd_status_t i_status = bp_cmd_flag(&dummy_def, 'i', &value);
    if (i_status == BP_CMD_INVALID) { system_config.error = true; return; }

    // 5. Interactive prompt fallback
    if (action == DUMMY_INIT && i_status == BP_CMD_MISSING) {
        bp_cmd_status_t prompt_st = bp_cmd_prompt(&integer_range, &value);
        if (prompt_st != BP_CMD_OK) return;
    }

    // 6. String flag
    char file[13];
    bool f_flag = bp_cmd_get_string(&dummy_def, 'f', file, sizeof(file));
}

Function Signature Tables

Simple Queries

FunctionReturnsDescription
bp_cmd_get_action(def, &action)boolMatch first non-flag token to actions array
bp_cmd_find_flag(def, flag)boolCheck flag presence (no value)
bp_cmd_get_uint32(def, flag, &val)boolParse flag value as uint32
bp_cmd_get_int32(def, flag, &val)boolParse flag value as int32
bp_cmd_get_float(def, flag, &val)boolParse flag value as float
bp_cmd_get_string(def, flag, buf, len)boolCopy flag value as string
bp_cmd_get_positional_string(def, pos, buf, len)boolGet positional arg as string
bp_cmd_get_positional_uint32(def, pos, &val)boolGet positional arg as uint32
bp_cmd_get_positional_int32(def, pos, &val)boolGet positional arg as int32
bp_cmd_get_positional_float(def, pos, &val)boolGet positional arg as float
bp_cmd_get_remainder(def, &out, &len)boolRaw pointer past command name

Constraint-Aware

FunctionReturnsDescription
bp_cmd_positional(def, pos, out)bp_cmd_status_tParse + validate positional arg
bp_cmd_flag(def, flag, out)bp_cmd_status_tParse + validate flag; writes default if absent
bp_cmd_prompt(con, out)bp_cmd_status_tInteractive prompt driven by constraint
bp_cmd_help_check(def, help_flag)boolShow help if flag set; returns true if shown
bp_cmd_help_show(def)voidUnconditional help display