Commit graph

87 commits

Author SHA1 Message Date
Junio C Hamano d978ead4c3 Merge branch 'sb/parse-options-integer'
* sb/parse-options-integer:
  parse-options: simplify usage argh handling
  parse-options: make OPT_INTEGER's argh explicit
2009-06-18 10:36:22 -07:00
Stephen Boyd e169b97459 show-branch: don't use LASTARG_DEFAULT with OPTARG
5734365 (show-branch: migrate to parse-options API 2009-05-21)
incorrectly set the --more option's flags to be
PARSE_OPT_LASTARG_DEFAULT and PARSE_OPT_OPTARG. These two flags
shouldn't be used together. An option taking a default should just set
the default value desired and parse options will take care of the rest.

Update the header comment to better convey this information.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Acked-by: René Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-06-09 00:15:57 -07:00
Stephen Boyd 34aec9f573 parse-options: simplify usage argh handling
Simplify the argh printing by simply calling usage_argh() if the option
can take an argument. Update macros defined in parse-options.h to set
the PARSE_OPT_NOARG flag.

The only other user of custom non-argument taking options is git-apply
(in this case OPTION_BOOLEAN for deprecated options). Update it to set
the PARSE_OPT_NOARG flag.

Thanks to Ren辿 Scharfe for the suggestion and starter patch.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Reviewd-by: René Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-06-06 13:25:34 -07:00
Stephen Boyd e3a0ca8784 parse-options: make OPT_INTEGER's argh explicit
OPTION_INTEGER hardcodes its argh member to be "n", but the decision is
hidden deep in usage_with_options_internal(). Make "n" the default argh
for the OPT_INTEGER macro while leaving it undecided for the OPTION_INTEGER
enum.

This makes it less surprising to users that argh is "n" when using the
OPT_INTEGER macro.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Reviewed-by: René Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-06-06 13:25:01 -07:00
Christian Couder 451bb210f8 parse-options: add new function "usage_msg_opt"
This function can be used instead of "usage_with_options" when you
want to print an error message before the usage string.

It may be useful because:

if (condition)
	usage_msg_opt("condition is false", usage, opts);

is shorter than:

if (condition) {
	fprintf(stderr, "condition is false\n\n");
	usage_with_options(usage, opts);
}

and may be more consistent.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-31 17:03:40 -07:00
Stephen Boyd df217ed643 parse-opts: add OPT_FILENAME and transition builtins
Commit dbd0f5c (Files given on the command line are relative to $cwd,
2008-08-06) introduced parse_options_fix_filename() as a minimal fix.
OPT_FILENAME is intended to be a more robust fix for the same issue.
OPT_FILENAME and its associated enum OPTION_FILENAME are used to
represent filename options within the parse options API.

This option is similar to OPTION_STRING. If --no is prefixed to the
option the filename is unset. If no argument is given and the default
value is set, the filename is set to the default value. The difference
is that the filename is prefixed with the prefix passed to
parse_options() (or parse_options_start()).

Update git-apply, git-commit, git-fmt-merge-msg, and git-tag to use
OPT_FILENAME with their filename options. Also, rename
parse_options_fix_filename() to fix_filename() as it is no longer
extern.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-25 01:07:33 -07:00
Stephen Boyd 3778292017 parse-opts: prepare for OPT_FILENAME
To give OPT_FILENAME the prefix, we pass the prefix to parse_options()
which passes the prefix to parse_options_start() which sets the prefix
member of parse_opts_ctx accordingly. If there isn't a prefix in the
calling context, passing NULL will suffice.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-25 01:07:25 -07:00
Junio C Hamano b064e2fc3d Merge branch 'sb/show-branch-parse-options' into sb/opt-filename
* sb/show-branch-parse-options:
  show-branch: migrate to parse-options API
  parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's

Conflicts:
	parse-options.h
2009-05-25 00:59:29 -07:00
Stephen Boyd 29f25d493c parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's
Usually, the argh element in struct option points at a placeholder value
(e.g. "val"), and is shown in the usage message as

    --option=<val>

by enclosing the string inside of angle brackets.

When the option is more complex (e.g. optional arguments separated by a
comma), you would want to produce a usage message that looks like

    --option=<val1>[,<val2>]

In such a case, the caller can pass a string to argh with placeholders
already enclosed in necessary angle brackets (e.g.  "<val1>[,<val2>]")
and set this flag.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-22 22:46:04 -07:00
René Scharfe 51a9949eda parseopt: add PARSE_OPT_NODASH
Add support for options that don't start with a dash.  Initially, they
don't accept arguments and can only be short options, i.e. consist of a
single character.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-09 00:29:50 -07:00
René Scharfe e0319ff5ed parseopt: add OPT_NUMBER_CALLBACK
Add a way to recognize numerical options.  The number is passed to
a callback function as a string.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-09 00:29:47 -07:00
René Scharfe 2f4b97f910 parseopt: add OPT_NEGBIT
Add OPTION_NEGBIT and OPT_NEGBIT, mirroring OPTION_BIT and OPT_BIT.
OPT_NEGBIT can be used together with OPT_BIT to define two options
that cancel each other out.

Note: this patch removes the reminder from the test script because
it adds a test for --no-or4 and there already was one for --or4.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-09 00:28:53 -07:00
Mike Ralphson 3ea3c215c0 Fix typos / spelling in comments
Signed-off-by: Mike Ralphson <mike@abacus.co.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-04-22 19:02:12 -07:00
René Scharfe b92891f978 parseopt: add PARSE_OPT_NO_INTERNAL_HELP
Add a parseopt flag, PARSE_OPT_NO_INTERNAL_HELP, that turns off internal
handling of -h, --help and --help-all.  This allows the implementation
of custom help option handlers or incremental parsers.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-03-08 13:36:27 -07:00
René Scharfe b5ce3a5430 parseopt: add PARSE_OPT_KEEP_UNKNOWN
Add a parseopt flag, PARSE_OPT_KEEP_UNKNOWN, that can be used to keep
unknown options in argv, similar to the existing KEEP flags.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-03-08 13:36:09 -07:00
Jake Goulding 269defdf30 Make opt_parse_with_commit() non-static
Moving opt_parse_with_commit() from branch to a common location, in
preparation for using it in tag. Rename it to match naming convention
of other option parsing functions.

Signed-off-by: Jake Goulding <goulding@vivisimo.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-01-28 11:32:27 -08:00
Tuncer Ayaz 7f87aff22c Teach/Fix pull/fetch -q/-v options
Implement git-pull --quiet and git-pull --verbose by
adding the options to git-pull and fixing verbosity
handling in git-fetch.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-11-14 17:18:32 -08:00
Junio C Hamano f44bc33c72 Sync with 1.5.6.5 2008-08-06 13:50:48 -07:00
Junio C Hamano dbd0f5c769 Files given on the command line are relative to $cwd
When running "git commit -F file" and "git tag -F file" from a
subdirectory, we should take it as relative to the directory we started
from, not relative to the top-level directory.

This adds a helper function "parse_options_fix_filename()" to make it more
convenient to fix this class of issues.  Ideally, parse_options() should
support a new type of option, "OPT_FILENAME", to do this uniformly, but
this patch is meant to go to 'maint' to fix it minimally.

One thing to note is that value for "commit template file" that comes from
the command line is taken as relative to $cwd just like other parameters,
but when it comes from the configuration varilable 'commit.template', it
is taken as relative to the working tree root as before.  I think this
difference actually is sensible (not that I particularly think
commit.template itself is sensible).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-08-06 13:38:18 -07:00
Pierre Habouzit 1cc6985ca7 parse-options: add PARSE_OPT_LASTARG_DEFAULT flag
If you set this for a given option, and the optoin appears without an
argument on the command line, then the `defval' is used as its argument.

Note that this flag is meaningless in presence of OPTARG or NOARG flags.
(in the current implementation it will be ignored, but don't rely on it).

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-07-09 01:33:24 -07:00
Pierre Habouzit a32a4eaa36 parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.
This way, argv[0] isn't clobbered when parse-options filters argv[].

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-06-30 14:51:13 -07:00
Pierre Habouzit 26141b5b60 parse-opt: fake short strings for callers to believe in.
If we begin to parse -abc and that the parser knew about -a and -b, it
will fake a -c switch for the caller to deal with.

Of course in the case of -acb (supposing -c is not taking an argument) the
caller will have to be especially clever to do the same thing. We could
think about exposing an API to do so if it's really needed, but oh well...

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-06-30 14:51:13 -07:00
Pierre Habouzit ff43ec3e2d parse-opt: create parse_options_step.
For now it's unable to stop at unknown options, this commit merely
reorganize some code around.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-06-30 14:51:12 -07:00
Pierre Habouzit ee68b87a62 parse-opt: Export a non NORETURN usage dumper.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-06-30 14:51:12 -07:00
Pierre Habouzit 7e7bbcb4b3 parse-opt: have parse_options_{start,end}.
Make the struct optparse_t public under the better name parse_opt_ctx_t.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-06-30 14:51:12 -07:00
Michele Ballabio 1f4a711a05 parse-options.c: introduce OPT_DATE
There are quite a few places that will need to call approxidate(),
when they'll adopt the parse-options system, so this patch adds the
function parse_opt_approxidate_cb(), used by OPT_DATE.

Signed-off-by: Michele Ballabio <barra_cuda@katamail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-03-27 13:55:15 -07:00
Junio C Hamano ae90e16a3a Merge branch 'js/remote'
* js/remote:
  "remote update": print remote name being fetched from
  builtin remote rm: remove symbolic refs, too
  remote: fix "update [group...]"
  remote show: Clean up connection correctly if object fetch wasn't done
  builtin-remote: prune remotes correctly that were added with --mirror
  Make git-remote a builtin
  Test "git remote show" and "git remote prune"
  parseopt: add flag to stop on first non option
  path-list: add functions to work with unsorted lists

Conflicts:

	parse-options.c
2008-03-11 22:33:51 -07:00
Pierre Habouzit 580d5bffde parse-options: new option type to treat an option-like parameter as an argument.
This is meant to be used to keep --not and --all during revision parsing.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-03-02 14:07:47 -08:00
Johannes Schindelin a0ec9d25d9 parseopt: add flag to stop on first non option
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-03-01 01:51:44 -08:00
Pierre Habouzit dd3bf0f4a5 parse-options: Allow to hide options from the default usage.
This is useful for backward-compatibility aliases, or very advanced command
line switches introduced for internal git usages and have no real use for a
user.

parse-options still shows them if the user asks for --help-all.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-11-22 22:11:28 -08:00
Pierre Habouzit db7244bd5b parse-options new features.
options flags:
~~~~~~~~~~~~~
  PARSE_OPT_NONEG allow the caller to disallow the negated option to exists.

option types:
~~~~~~~~~~~~
  OPTION_BIT: ORs (or NANDs) a mask.
  OPTION_SET_INT: force the value to be set to this integer.
  OPTION_SET_PTR: force the value to be set to this pointer.

helper:
~~~~~~
  HAS_MULTI_BITS (in git-compat-util.h) is a bit-hack to check if an
  unsigned integer has more than one bit set, useful to check if conflicting
  options have been used.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-11-11 16:54:15 -08:00
Pierre Habouzit 9b3beb5812 Some better parse-options documentation.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-11-05 18:47:01 -08:00
Pierre Habouzit f481e22a14 parse-options: allow callbacks to take no arguments at all.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-10-29 21:03:31 -07:00
Pierre Habouzit 0ce865b134 Add shortcuts for very often used options.
It helps with consistency of the help strings, for example.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-10-29 21:03:30 -07:00
Pierre Habouzit ffe659f94d parse-options: make some arguments optional, add callbacks.
* add the possibility to use callbacks to parse some options, this can
  help implementing new options kinds with great flexibility. struct option
  gains a callback pointer and a `defval' where callbacks user can put
  either integers or pointers. callbacks also can use the `value' pointer
  for anything, preferably to the pointer to the final storage for the value
  though.

* add a `flag' member to struct option to make explicit that this option may
  have an optional argument. The semantics depends on the option type. For
  INTEGERS, it means that if the switch is not used in its
  --long-form=<value> form, and that there is no token after it or that the
  token does not starts with a digit, then it's assumed that the switch has
  no argument. For STRING or CALLBACK it works the same, except that the
  condition is that the next atom starts with a dash. This is needed to
  implement backward compatible behaviour with existing ways to parse the
  command line. Its use for new options is discouraged.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-10-29 21:03:30 -07:00
Pierre Habouzit d7a38c54a6 parse-options: be able to generate usages automatically
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-10-29 21:03:30 -07:00
Pierre Habouzit 4a59fd1312 Add a simple option parser.
The option parser takes argc, argv, an array of struct option
and a usage string.  Each of the struct option elements in the array
describes a valid option, its type and a pointer to the location where the
value is written.  The entry point is parse_options(), which scans through
the given argv, and matches each option there against the list of valid
options.  During the scan, argv is rewritten to only contain the
non-option command line arguments and the number of these is returned.

Aggregation of single switches is allowed:
  -rC0 is the same as -r -C 0 (supposing that -C wants an arg).

Every long option automatically support the option with the same name,
prefixed with 'no-' to unset the switch. It assumes that initial value for
strings are "NULL" and for integers is "0".

Long options are supported either with '=' or without:
  --some-option=foo is the same as --some-option foo

Acked-by: Kristian Høgsberg <krh@redhat.com>
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2007-10-29 21:03:30 -07:00