summaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/optcmd.h22
-rw-r--r--src/tests/optcmd_test.c27
2 files changed, 48 insertions, 1 deletions
diff --git a/src/optcmd.h b/src/optcmd.h
index e5f688e..48651bd 100644
--- a/src/optcmd.h
+++ b/src/optcmd.h
@@ -144,6 +144,28 @@ struct optcmd_cmd {
}
/*
+ * Defines a new command taking only flags and no positional arguments.
+ * @cmd_name - name of command used on the command line
+ * @cmd_func - pointer to the function to call for this command
+ * @flag - name of flag struct to pass @cmd_func as the last parameter
+ * @flags - list of flags in the form (("--flag-name", <struct field name>, parser, <default value>, "help text"), ...)
+ */
+#define OPTCMD_DEFINE_CMD_ONLY_FLAGS(cmd_name_, cmd_func_, flag_, flags_) \
+ int cmd_func_(struct flag_); \
+ int __##cmd_func_(void *args[], void *flags[]) \
+ { \
+ struct flag_ flag; \
+ CPPMAGIC_JOIN(, CPPMAGIC_MAP(OPTCMD_CAST_FLAGS, CPPMAGIC_EVAL flags_)) \
+ return cmd_func_(flag); \
+ } \
+ \
+ struct optcmd_cmd optcmd_##cmd_name_ = { \
+ .cmd = #cmd_name_, \
+ .cmdp = __##cmd_func_, \
+ .flags = { CPPMAGIC_MAP(OPTCMD_FLAG, CPPMAGIC_EVAL flags_), {NULL} }, \
+ }
+
+/*
* Defines a new command with arguments.
* @cmd_name - name of command used on the command line
* @cmd_func - pointer to the function to call for this command
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