git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Pranit Bauva <pranit.bauva@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] api-parse-options.txt: document OPT_CMDMODE()
Date: Thu, 24 Mar 2016 09:07:09 -0700	[thread overview]
Message-ID: <xmqqtwjvamhe.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <01020153a7e08100-cf66fc9f-7293-4d8c-98c6-f322721c741f-000000@eu-west-1.amazonses.com> (Pranit Bauva's message of "Thu, 24 Mar 2016 09:07:01 +0000")

Pranit Bauva <pranit.bauva@gmail.com> writes:

> OPT_CMDMODE() was introduced in the release of 1.8.5 which makes the use
> of subcommands in the form of arguments a lot cleaner and easier.
> ---

Sign-off?

>  Documentation/technical/api-parse-options.txt | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/Documentation/technical/api-parse-options.txt b/Documentation/technical/api-parse-options.txt
> index 5f0757d..8130d26 100644
> --- a/Documentation/technical/api-parse-options.txt
> +++ b/Documentation/technical/api-parse-options.txt
> @@ -231,6 +231,12 @@ There are some macros to easily define options:
>  	pass the command-line option, which can be specified multiple times,
>  	to another command.
>  
> +`OPT_CMDMODE(short, long, &int_var, description, enum_val)`::
> +	Introduce an option for subcommands. It is useful when you want to use
> +	the command with a particular sub command only and ignore other sub
> +	commands it has. It will set `int_var` to enum_val if the argument is
> +	invoked.
> +

Sorry, but I do not get what "when you want to... ignore other sub
command it has" wants to say.

CMDMODE is a mechanism to actively notice when multiple "operation
mode" options that specify mutually incompatible operation modes are
given and error out without the user of parse_options() to implement
that mutual exclusion herself.  That is, if you have 'add', 'remove'
and 'edit' operation modes, with OPT_BOOL(), you would have to say:

	options[] = {
                OPT_BOOL('a', "add", &add, ...),
                OPT_BOOL('r', "remove", &remove, ...),
                OPT_BOOL('e', "edit", &edit, ...),
                ...
	};
        parse_options(ac, av, prefix, options, ...);

	if (!!add + !!remove + !!edit > 1)
        	die("at most one add/remove/edit can be used at a time");

	if (add)
        	do_add();
	if (remove)
        	do_remove();
	if (edit)
        	do_edit();

but with CMDMODE, you can do:

	options[] = {
                OPT_BOOL('a', "add", &mode, ...),
                OPT_BOOL('r', "remove", &mode, ...),
                OPT_BOOL('e', "edit", &mode, ...),
                ...
	};
        parse_options(ac, av, prefix, options, ...);

        switch (mode) {
        case 'a': do_add(); break;
        case 'r': do_remove(); break;
        case 'e': do_edit(); break;
		...
	}

and parse_options notices that "mode" is shared across these three
options, and implements the mutual-exclusion itself.

  reply	other threads:[~2016-03-24 16:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-24  9:07 [PATCH] api-parse-options.txt: document OPT_CMDMODE() Pranit Bauva
2016-03-24 16:07 ` Junio C Hamano [this message]
2016-03-24 16:55   ` Pranit Bauva
2016-03-24 17:16     ` Junio C Hamano
2016-03-24 18:04       ` Pranit Bauva
2016-03-25 18:58 ` [PATCH v2] " Pranit Bauva
2016-03-25 19:08   ` Pranit Bauva
2016-03-25 21:24     ` Junio C Hamano
2016-03-25 20:14   ` [PATCH v3] " Pranit Bauva

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=xmqqtwjvamhe.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=pranit.bauva@gmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).