summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorAlistair Popple <alistair@popple.id.au>2018-10-15 16:06:18 +1100
committerAlistair Popple <alistair@popple.id.au>2018-10-15 16:06:18 +1100
commitd150713c357c69247d95843717935097f5ee30b0 (patch)
tree863a5f132ed19cb0113eddfdb019280e4ac2c68c /src/tests
parent745ea68d9ee06e4fd3ca594636837ba561132c0c (diff)
downloadpdbg-d150713c357c69247d95843717935097f5ee30b0.tar.gz
pdbg-d150713c357c69247d95843717935097f5ee30b0.zip
optcmd: Add OPTCMD_DEFINE_CMD_ONLY_FLAGS
Defines a new macro allowing commands that only taking flags and no positional arguments to be defined. Also adds a couple of test cases for the new macro. Signed-off-by: Alistair Popple <alistair@popple.id.au> Signed-off-by: Joel Stanley <joel@jms.id.au>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/optcmd_test.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/tests/optcmd_test.c b/src/tests/optcmd_test.c
index bfa0667..ae1b1cd 100644
--- a/src/tests/optcmd_test.c
+++ b/src/tests/optcmd_test.c
@@ -72,11 +72,22 @@ static int test_flags(uint64_t num_arg, uint64_t opt_arg, struct flags flags)
OPTCMD_DEFINE_CMD_WITH_FLAGS(test_flags, test_flags, (ARG_NUM, ARG_NUM_OPT),
flags, (FLAG_TEST_BOOL, FLAG_TEST_NUM));
+static int test_only_flags(struct flags flags)
+{
+ flag = flags.test_num;
+ bool_flag = flags.test_bool;
+
+ return 0;
+}
+OPTCMD_DEFINE_CMD_ONLY_FLAGS(test_only_flags, test_only_flags, flags,
+ (FLAG_TEST_BOOL, FLAG_TEST_NUM));
+
int parse_argv(const char *argv[], int argc)
{
int i, rc;
void **args, **flags;
- struct optcmd_cmd *cmds[] = { &optcmd_test, &optcmd_test_args, &optcmd_test_flags };
+ struct optcmd_cmd *cmds[] = { &optcmd_test, &optcmd_test_args, &optcmd_test_flags,
+ &optcmd_test_only_flags };
optcmd_cmd_t *cmd;
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
@@ -157,5 +168,19 @@ int main(void)
const char *test10_argv[] = { "test_flags", "9", "10", "--test-blah", "--test-num=11" };
assert(parse_argv(test10_argv, ARRAY_SIZE(test10_argv)));
+ const char *test11_argv[] = { "test_only_flags" };
+ assert(!parse_argv(test11_argv, ARRAY_SIZE(test11_argv)));
+ assert(flag == 10);
+ assert(!bool_flag);
+
+ const char *test12_argv[] = { "test_only_flags", "--test-num=12" };
+ assert(!parse_argv(test12_argv, ARRAY_SIZE(test12_argv)));
+ assert(flag == 12);
+ assert(!bool_flag);
+
+ /* Should fail because we're passing a positional argument */
+ const char *test13_argv[] = { "test_only_flags", "--test-num=12", "13" };
+ assert(parse_argv(test13_argv, ARRAY_SIZE(test13_argv)));
+
return 0;
}
OpenPOWER on IntegriCloud