Skip to content

Commit 0648bf6

Browse files
committed
scalar: accept -C and -c options before the subcommand
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 <[email protected]>
1 parent 52b7db1 commit 0648bf6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

contrib/scalar/scalar.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,25 @@ int cmd_main(int argc, const char **argv)
760760
struct strbuf scalar_usage = STRBUF_INIT;
761761
int i;
762762

763+
while (argc > 1 && *argv[1] == '-') {
764+
if (!strcmp(argv[1], "-C")) {
765+
if (argc < 3)
766+
die(_("-C requires a <directory>"));
767+
if (chdir(argv[2]) < 0)
768+
die_errno(_("could not change to '%s'"),
769+
argv[2]);
770+
argc -= 2;
771+
argv += 2;
772+
} else if (!strcmp(argv[1], "-c")) {
773+
if (argc < 3)
774+
die(_("-c requires a <key>=<value> argument"));
775+
git_config_push_parameter(argv[2]);
776+
argc -= 2;
777+
argv += 2;
778+
} else
779+
break;
780+
}
781+
763782
if (argc > 1) {
764783
argv++;
765784
argc--;
@@ -770,7 +789,8 @@ int cmd_main(int argc, const char **argv)
770789
}
771790

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

contrib/scalar/scalar.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ The command implements various subcommands, and different options depending
3434
on the subcommand. With the exception of `clone` and `list`, all subcommands
3535
expect to be run in an enlistment.
3636

37+
The following options can be specified _before_ the subcommand:
38+
39+
-C <directory>::
40+
Before running the subcommand, change the working directory. This
41+
option imitates the same option of linkgit:git[1].
42+
43+
-c <key>=<value>::
44+
For the duration of running the specified subcommand, configure this
45+
setting. This option imitates the same option of linkgit:git[1].
46+
3747
COMMANDS
3848
--------
3949

0 commit comments

Comments
 (0)