Skip to content

Commit

Permalink
scalar: accept -C and -c options before the subcommand
Browse files Browse the repository at this point in the history
The `git` executable has these two very useful options:

-C <directory>:
	switch to the specified directory before performing any actions

-c <key>=<value>:
	temporarily configure this setting for the duration of the
	specified scalar subcommand

With this commit, we teach the `scalar` executable the same trick.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Jun 7, 2021
1 parent 13fab8f commit b2c2c95
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion contrib/scalar/scalar.c
Expand Up @@ -1117,6 +1117,25 @@ int cmd_main(int argc, const char **argv)
struct strbuf scalar_usage = STRBUF_INIT;
int i;

while (argc > 1 && *argv[1] == '-') {
if (!strcmp(argv[1], "-C")) {
if (argc < 3)
die(_("-C requires a <directory>"));
if (chdir(argv[2]) < 0)
die_errno(_("could not change to '%s'"),
argv[2]);
argc -= 2;
argv += 2;
} else if (!strcmp(argv[1], "-c")) {
if (argc < 3)
die(_("-c requires a <key>=<value> argument"));
git_config_push_parameter(argv[2]);
argc -= 2;
argv += 2;
} else
break;
}

if (argc > 1) {
argv++;
argc--;
Expand All @@ -1130,7 +1149,8 @@ int cmd_main(int argc, const char **argv)
}

strbuf_addstr(&scalar_usage,
N_("scalar <command> [<options>]\n\nCommands:\n"));
N_("scalar [-C <directory>] [-c <key>=<value>] "
"<command> [<options>]\n\nCommands:\n"));
for (i = 0; builtins[i].name; i++)
strbuf_addf(&scalar_usage, "\t%s\n", builtins[i].name);

Expand Down

0 comments on commit b2c2c95

Please sign in to comment.