[PATCH 6/7] parse-options: have a `use default value` wildcard.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
Documentation/gitcli.txt | 20 +++++++++++++++-----
parse-options.c | 10 ++++++++--
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/Documentation/gitcli.txt b/Documentation/gitcli.txt
index b7dcf9c..a304072 100644
--- a/Documentation/gitcli.txt
+++ b/Documentation/gitcli.txt
@@ -95,14 +95,24 @@ $ git foo -oArg
$ git foo -o Arg
----------------------------
-However, this is *NOT* allowed for switches with an optionnal value, where the
-'sticked' form must be used:
+However, this may become ambiguous for switches with an optional value. The
+enhanced option parser provides a placeholder `{}` that tells to the option
+parser that it should not try to find an argument to this switch. Though if
+you use '{}' sticked to the option, `{}` is passed as the value.
----------------------------
-$ git describe --abbrev HEAD # correct
-$ git describe --abbrev=10 HEAD # correct
-$ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT
+# all the following uses work
+$ git describe --abbrev HEAD
+$ git describe --abbrev {} HEAD
+$ git describe --abbrev=10 HEAD
+$ git describe --abbrev 10 HEAD
+
+# doesn't work
+$ git describe --abbrev={} HEAD
----------------------------
+Note that an optional switch will never try to use the next token as an
+argument if it starts with a dash and is not `-`.
+
Documentation
-------------
diff --git a/parse-options.c b/parse-options.c
index 679a963..8734bb1 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -69,8 +69,14 @@ static int run_callback(struct optparse_t *p, parse_opt_cb *cb,
return (*cb)(opt, NULL, flags);
if (!may_ign && !arg)
return opterror(opt, "requires a value", flags);
- if (may_ign && arg && arg[0] == '-' && arg[1])
- return (*cb)(opt, NULL, flags);
+ if (may_ign && arg) {
+ if (arg[0] == '-' && arg[1])
+ return (*cb)(opt, NULL, flags);
+ if (!strcmp(arg, "{}")) {
+ use_arg(p);
+ return (*cb)(opt, NULL, flags);
+ }
+ }
switch ((*cb)(opt, arg, flags | may_ign)) {
case PARSE_OPT_OK:
use_arg(p);
--
1.5.4.rc0.1148.ga3ab1-dirty