{"thread":{"id":"38429","subject":"[PATCH] parse-opts: add OPT_SUBCMD()","startedAt":"2015-01-22T11:48:58Z","lastAt":"2015-01-22T11:48:58Z","messageCount":1,"participants":["Alexander Kuleshov"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"255081","messageId":"1421927338-32276-1-git-send-email-kuleshovmail@gmail.com","threadId":"38429","inReplyTo":null,"subject":"[PATCH] parse-opts: add OPT_SUBCMD()","fromName":"Alexander Kuleshov","fromEmail":"kuleshovmail@gmail.com","sentAt":"2015-01-22T11:48:58Z","receivedAt":"2015-01-22T11:48:58Z","isPatch":true,"sender":{"key":"kuleshovmail@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2699235?v=4"},"body":"OPT_SUBCMD can be used for parsing git commands which has not only\nshort/long options, but subcomands. For example: git bundle (create,\nverify and etc...) or git remote (add, rename and etc...)\n\nSigned-off-by: Alexander Kuleshov <kuleshovmail@gmail.com>\n---\n parse-options.c          | 25 +++++++++++++++++++++++++\n parse-options.h          |  8 ++++++++\n t/t0040-parse-options.sh | 13 +++++++++++++\n test-parse-options.c     |  3 +++\n 4 files changed, 49 insertions(+)\n\ndiff --git a/parse-options.c b/parse-options.c\nindex 80106c0..79f971e 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -121,6 +121,10 @@ static int get_value(struct parse_opt_ctx_t *p,\n \t\t*(int *)opt->value = unset ? 0 : opt->defval;\n \t\treturn 0;\n \n+\tcase OPTION_SUBCMD:\n+\t\t*(int *)opt->value = 1;\n+\t\treturn 0;\n+\n \tcase OPTION_CMDMODE:\n \t\t/*\n \t\t * Giving the same mode option twice, although is unnecessary,\n@@ -314,6 +318,17 @@ is_abbreviated:\n \treturn -2;\n }\n \n+static int parse_subcmd_opt(struct parse_opt_ctx_t *p, const char *arg,\n+\t\t\t const struct option *options) {\n+\tconst struct option *all_opts = options;\n+\tfor (; options->type != OPTION_END; options++) {\n+\t\tif (options->long_name && !strcmp(options->long_name, arg))\n+\t\t\treturn get_value(p, options, all_opts, 0);\n+\t}\n+\n+\treturn -2;\n+}\n+\n static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,\n \t\t\t    const struct option *options)\n {\n@@ -417,6 +432,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\t       const struct option *options,\n \t\t       const char * const usagestr[])\n {\n+\tint argc = ctx->argc;\n \tint internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);\n \n \t/* we must reset ->opt, unknown short option leave it dangling */\n@@ -426,6 +442,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\tconst char *arg = ctx->argv[0];\n \n \t\tif (*arg != '-' || !arg[1]) {\n+\t\t\tif (ctx->argc == argc &&\n+\t\t\t    options->type == OPTION_SUBCMD &&\n+\t\t\t    parse_subcmd_opt(ctx, arg, options) == 0){\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (parse_nodash_opt(ctx, arg, options) == 0)\n \t\t\t\tcontinue;\n \t\t\tif (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)\n@@ -581,6 +602,10 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,\n \t\tsize_t pos;\n \t\tint pad;\n \n+\t\tif (opts->type == OPTION_SUBCMD) {\n+\t\t\tcontinue;\n+\t\t}\n+\n \t\tif (opts->type == OPTION_GROUP) {\n \t\t\tfputc('\\n', outfile);\n \t\t\tif (*opts->help)\ndiff --git a/parse-options.h b/parse-options.h\nindex 7940bc7..0e75a85 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -13,6 +13,8 @@ enum parse_opt_type {\n \tOPTION_COUNTUP,\n \tOPTION_SET_INT,\n \tOPTION_CMDMODE,\n+\tOPTION_SUBCMD,\n+\n \t/* options with arguments (usually) */\n \tOPTION_STRING,\n \tOPTION_INTEGER,\n@@ -115,6 +117,10 @@ struct option {\n #define OPT_END()                   { OPTION_END }\n #define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, \\\n \t\t\t\t      (h), PARSE_OPT_NOARG}\n+\n+#define OPT_SUBCMD(l, v, h)         { OPTION_SUBCMD, 0, (l), (v), NULL, \\\n+\t\t\t\t      (h), PARSE_OPT_NOARG}\n+\n #define OPT_GROUP(h)                { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }\n #define OPT_BIT(s, l, v, h, b)      { OPTION_BIT, (s), (l), (v), NULL, (h), \\\n \t\t\t\t      PARSE_OPT_NOARG, NULL, (b) }\n@@ -125,8 +131,10 @@ struct option {\n #define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, \\\n \t\t\t\t      (h), PARSE_OPT_NOARG, NULL, (i) }\n #define OPT_BOOL(s, l, v, h)        OPT_SET_INT(s, l, v, h, 1)\n+\n #define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \\\n \t\t\t\t      (h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}\n+\n #define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \\\n \t\t\t\t      (h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }\n #define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), N_(\"n\"), (h) }\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex a90c86b..7898a5a 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -65,6 +65,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n check() {\n@@ -142,6 +143,7 @@ verbose: 2\n quiet: no\n dry run: yes\n file: prefix/my.file\n+subcommand: 1\n EOF\n \n test_expect_success 'short options' '\n@@ -161,6 +163,7 @@ verbose: 2\n quiet: no\n dry run: no\n file: prefix/fi.le\n+subcommand: 1\n EOF\n \n test_expect_success 'long options' '\n@@ -190,6 +193,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n arg 00: a1\n arg 01: b1\n arg 02: --boolean\n@@ -212,6 +216,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'unambiguously abbreviated option' '\n@@ -241,6 +246,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'non ambiguous option (after two options it abbreviates)' '\n@@ -279,6 +285,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n arg 00: --quux\n EOF\n \n@@ -298,6 +305,7 @@ verbose: 0\n quiet: yes\n dry run: no\n file: (not set)\n+subcommand: 1\n arg 00: foo\n EOF\n \n@@ -319,6 +327,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '\n@@ -347,6 +356,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '\n@@ -371,6 +381,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'OPT_BIT() works' '\n@@ -401,6 +412,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'OPT_NUMBER_CALLBACK() works' '\n@@ -419,6 +431,7 @@ verbose: 0\n quiet: no\n dry run: no\n file: (not set)\n+subcommand: 1\n EOF\n \n test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '\ndiff --git a/test-parse-options.c b/test-parse-options.c\nindex 5dabce6..96e2a21 100644\n--- a/test-parse-options.c\n+++ b/test-parse-options.c\n@@ -11,6 +11,7 @@ static char *string = NULL;\n static char *file = NULL;\n static int ambiguous;\n static struct string_list list;\n+static int subcommand = 0;\n \n static int length_callback(const struct option *opt, const char *arg, int unset)\n {\n@@ -52,6 +53,7 @@ int main(int argc, char **argv)\n \t\tOPT_DATE('t', NULL, &timestamp, \"get timestamp of <time>\"),\n \t\tOPT_CALLBACK('L', \"length\", &integer, \"str\",\n \t\t\t\"get length of <str>\", length_callback),\n+\t\tOPT_SUBCMD(\"subcommand\", &subcommand, \"get a subcommand\"),\n \t\tOPT_FILENAME('F', \"file\", &file, \"set file to <file>\"),\n \t\tOPT_GROUP(\"String options\"),\n \t\tOPT_STRING('s', \"string\", &string, \"string\", \"get a string\"),\n@@ -90,6 +92,7 @@ int main(int argc, char **argv)\n \tprintf(\"quiet: %s\\n\", quiet ? \"yes\" : \"no\");\n \tprintf(\"dry run: %s\\n\", dry_run ? \"yes\" : \"no\");\n \tprintf(\"file: %s\\n\", file ? file : \"(not set)\");\n+\tprintf(\"subcommand: %d\\n\", 1);\n \n \tfor (i = 0; i < list.nr; i++)\n \t\tprintf(\"list: %s\\n\", list.items[i].string);\n-- \n2.3.0.rc1.275.g028c360.dirty\n"}]}