Skip to content

Commit a72ab27

Browse files
committed
Fix bug in subcommand option parsing
Lookup options in the subcommand. d'oh!
1 parent c411843 commit a72ab27

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

command-line-parser.dylan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ define function process-tokens
740740
option.option-present? := #t;
741741
end;
742742
<short-option-token>, <long-option-token> =>
743-
let option = find-option(parser, value)
743+
let option = find-option(subcmd | parser, value)
744744
| usage-error("Unrecognized option: %s%s",
745745
if (value.size = 1) "-" else "--" end,
746746
value);

tests/command-line-parser-test-suite.lid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target-type: dll
33
files: command-line-parser-test-suite-library
44
options-test
55
command-line-parser-test-suite
6+
subcommands-test

tests/subcommands-test.dylan

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Module: command-line-parser-test-suite
2+
3+
4+
define class <subcommand-s1> (<subcommand>) end;
5+
6+
define test test-subcommand-parsing ()
7+
let global-a = make(<flag-option>, names: #("a"), help: "a help");
8+
let global-b = make(<parameter-option>, names: #("b"), help: "b help");
9+
let local-c = make(<flag-option>, names: #("c"), help: "c help");
10+
let positional-d = make(<positional-option>, names: #("d"), help: "d help");
11+
let positional-e = make(<positional-option>,
12+
repeated?: #t, names: #("e"), help: "e help");
13+
let s1 = make(<subcommand-s1>, name: "s1", help: "s1 help");
14+
add-option(s1, local-c);
15+
add-option(s1, positional-d);
16+
add-option(s1, positional-e);
17+
let p = make(<command-line-parser>,
18+
help: "main help",
19+
subcommands: list(s1));
20+
add-option(p, global-a);
21+
add-option(p, global-b);
22+
23+
// Done with setup
24+
25+
assert-no-errors(parse-command-line(p, #["-a", "s1", "-c", "d", "e", "e"]));
26+
assert-true(get-option-value(p, "a"));
27+
assert-false(get-option-value(p, "b"));
28+
assert-true(get-option-value(s1, "c"));
29+
assert-equal("d", get-option-value(s1, "d"));
30+
assert-equal(#["e", "e"], get-option-value(s1, "e"));
31+
end test;

0 commit comments

Comments
 (0)