git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Boyd <bebarino@gmail.com>
To: git@vger.kernel.org
Cc: spearce@spearce.org, szeder@ira.uka.de,
	felipe.contreras@gmail.com, jrnieder@gmail.com
Subject: [PATCH 1/2] parse-options: Add support for dumping out long options
Date: Wed, 11 Apr 2012 03:29:24 -0700	[thread overview]
Message-ID: <1334140165-24958-2-git-send-email-bebarino@gmail.com> (raw)
In-Reply-To: <1334140165-24958-1-git-send-email-bebarino@gmail.com>

The bash completion script wants to know what the long options are for a
certain command at runtime. Add a magical long option that nobody could
possibly ever use (--dump-raw-long-options) to get this information.

Some example output:

 $ git clone --dump-raw-long-options
 --no-verbose --no-quiet --progress --no-progress --no-checkout
 --checkout --bare --no-bare --mirror --no-mirror --local --no-local
 --no-hardlinks --hardlinks --shared --no-shared --recursive
 --no-recursive --recurse-submodules --no-recurse-submodules --template=
 --no-template --reference= --no-reference --origin= --no-origin
 --branch= --no-branch --upload-pack= --no-upload-pack --depth=
 --no-depth --single-branch --no-single-branch --separate-git-dir=
 --no-separate-git-dir --config= --no-config

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---

The name can be anything. This seemed sufficiently obscure.

 parse-options.c |   41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/parse-options.c b/parse-options.c
index 850cfa7..6c37497 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -364,6 +364,45 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *,
 				       const char * const *,
 				       const struct option *, int, int);
 
+static int parse_options_raw(const struct option *opts)
+{
+	for (; opts->type != OPTION_END; opts++) {
+		if (opts->flags & PARSE_OPT_HIDDEN)
+			continue;
+		if (!opts->long_name)
+			continue;
+		switch (opts->type) {
+		case OPTION_BIT:
+		case OPTION_NEGBIT:
+		case OPTION_COUNTUP:
+		case OPTION_SET_INT:
+		case OPTION_SET_PTR:
+			fprintf(stdout, "--%s ", opts->long_name);
+			break;
+		case OPTION_LOWLEVEL_CALLBACK:
+		case OPTION_STRING:
+		case OPTION_FILENAME:
+		case OPTION_INTEGER:
+		case OPTION_CALLBACK:
+			if (opts->flags & PARSE_OPT_OPTARG)
+				fprintf(stdout, "--%s ", opts->long_name);
+			else if (!(opts->flags & PARSE_OPT_NOARG))
+				fprintf(stdout, "--%s= ", opts->long_name);
+			break;
+		default:
+			continue;
+		}
+		if (!(opts->flags & PARSE_OPT_NONEG)) {
+			if (!prefixcmp(opts->long_name, "no-"))
+				fprintf(stdout, "--%s ", opts->long_name + 3);
+			else
+				fprintf(stdout, "--no-%s ", opts->long_name);
+		}
+	}
+
+	return PARSE_OPT_HELP;
+}
+
 int parse_options_step(struct parse_opt_ctx_t *ctx,
 		       const struct option *options,
 		       const char * const usagestr[])
@@ -431,6 +470,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			return usage_with_options_internal(ctx, usagestr, options, 1, 0);
 		if (internal_help && !strcmp(arg + 2, "help"))
 			return parse_options_usage(ctx, usagestr, options, 0);
+		if (!strcmp(arg + 2, "dump-raw-long-options"))
+			return parse_options_raw(options);
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
 			return parse_options_usage(ctx, usagestr, options, 1);
-- 
1.7.10.128.g7945c.dirty

  reply	other threads:[~2012-04-11 10:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-11 10:29 [PATCH 0/2] Dynamic long options for bash completion Stephen Boyd
2012-04-11 10:29 ` Stephen Boyd [this message]
2012-04-11 10:51   ` [PATCH 1/2] parse-options: Add support for dumping out long options Felipe Contreras
2012-04-12  7:42     ` Stephen Boyd
2012-04-11 12:59   ` Jonathan Nieder
2012-04-12  7:02     ` Stephen Boyd
2012-04-11 14:06   ` SZEDER Gábor
2012-04-12  7:12     ` Stephen Boyd
2012-04-15 12:49       ` SZEDER Gábor
2012-04-15 19:23         ` Junio C Hamano
2012-04-11 10:29 ` [PATCH 2/2] completion: Use parse-options raw output for simple " Stephen Boyd
2012-04-11 13:09   ` Jonathan Nieder
2012-04-11 13:56   ` SZEDER Gábor
2012-04-17 10:44   ` SZEDER Gábor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1334140165-24958-2-git-send-email-bebarino@gmail.com \
    --to=bebarino@gmail.com \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    --cc=spearce@spearce.org \
    --cc=szeder@ira.uka.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).