git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/GSoC] parse-options: Add a new nousage opt

From
Chirayu Desai <chirayudesai1@gmail.com>
Date
Mar 20, 2016, 06:46 UTC
Message-ID
<1458456405-3519-1-git-send-email-chirayudesai1@gmail.com>
* To show only error text on an error instead of the full usage
* Currently used only by commands with options "--with" or "--contains",
  such as 'tag', 'branch', 'for-each-ref'.
It now prints only
  $ git tag --contains qq
  error: malformed object name qq
instead of the full usage text after the error text.
TODO: Add tests
---
 parse-options-cb.c | 12 ++++++++----
 parse-options.c    |  5 +++++
 parse-options.h    |  6 ++++--
 3 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/parse-options-cb.c b/parse-options-cb.c
index 239898d946..ac2ea4d674 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -85,11 +85,15 @@ int parse_opt_commits(const struct option *opt, const char *arg, int unset)
 
 	if (!arg)
 		return -1;
-	if (get_sha1(arg, sha1))
-		return error("malformed object name %s", arg);
+	if (get_sha1(arg, sha1)) {
+		error("malformed object name %s", arg);
+		return -3;
+	}
 	commit = lookup_commit_reference(sha1);
-	if (!commit)
-		return error("no such commit %s", arg);
+	if (!commit) {
+		error("no such commit %s", arg);
+		return -3;
+	}
 	commit_list_insert(commit, opt->value);
 	return 0;
 }
diff --git a/parse-options.c b/parse-options.c
index 47a9192060..d136c1afd0 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -158,6 +158,9 @@ static int get_value(struct parse_opt_ctx_t *p,
 			return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
 		if (get_arg(p, opt, flags, &arg))
 			return -1;
+		if (opt->flags & PARSE_OPT_NOUSAGE) {
+			return (*opt->callback)(opt, arg, 0);
+		}
 		return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
 
 	case OPTION_INTEGER:
@@ -504,6 +507,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			goto show_usage_error;
 		case -2:
 			goto unknown;
+		case -3:
+			return PARSE_OPT_DONE;
 		}
 		continue;
 unknown:
diff --git a/parse-options.h b/parse-options.h
index ea4af92a51..628e34c5af 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -38,7 +38,8 @@ enum parse_opt_option_flags {
 	PARSE_OPT_LASTARG_DEFAULT = 16,
 	PARSE_OPT_NODASH = 32,
 	PARSE_OPT_LITERAL_ARGHELP = 64,
-	PARSE_OPT_SHELL_EVAL = 256
+	PARSE_OPT_SHELL_EVAL = 256,
+	PARSE_OPT_NOUSAGE = 512
 };
 
 struct option;
@@ -89,6 +90,7 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
  *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
  *				(i.e. '<argh>') in the help message.
  *				Useful for options with multiple parameters.
+ *   PARSE_OPT_NOUSAGE: do not print usage / help on error.
  *
  * `callback`::
  *   pointer to the callback to use for OPTION_CALLBACK or
@@ -254,7 +256,7 @@ extern int parse_opt_passthru_argv(const struct option *, const char *, int);
 	{ OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv }
 #define _OPT_CONTAINS_OR_WITH(name, variable, help, flag) \
 	{ OPTION_CALLBACK, 0, name, (variable), N_("commit"), (help), \
-	  PARSE_OPT_LASTARG_DEFAULT | flag, \
+	  PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOUSAGE | flag, \
 	  parse_opt_commits, (intptr_t) "HEAD" \
 	}
 #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, 0)
-- 
2.7.4
Next: Chirayu Desai
Message 1 of 5 in “parse-options: Add a new nousage opt”
  1. parse-options: Add a new nousage optChirayu Desai, Mar 20, 2016
  2. Chirayu DesaiMar 20, 2016
  3. Jeff KingMar 23, 2016
  4. Chirayu DesaiMar 24, 2016
  5. Jeff KingMar 24, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.