Skip to content

Commit

Permalink
Fix bug in subcommand option parsing
Browse files Browse the repository at this point in the history
Lookup options in the subcommand. d'oh!
  • Loading branch information
cgay committed Apr 20, 2021
1 parent c411843 commit a72ab27
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command-line-parser.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ define function process-tokens
option.option-present? := #t;
end;
<short-option-token>, <long-option-token> =>
let option = find-option(parser, value)
let option = find-option(subcmd | parser, value)
| usage-error("Unrecognized option: %s%s",
if (value.size = 1) "-" else "--" end,
value);
Expand Down
1 change: 1 addition & 0 deletions tests/command-line-parser-test-suite.lid
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target-type: dll
files: command-line-parser-test-suite-library
options-test
command-line-parser-test-suite
subcommands-test
31 changes: 31 additions & 0 deletions tests/subcommands-test.dylan
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Module: command-line-parser-test-suite


define class <subcommand-s1> (<subcommand>) end;

define test test-subcommand-parsing ()
let global-a = make(<flag-option>, names: #("a"), help: "a help");
let global-b = make(<parameter-option>, names: #("b"), help: "b help");
let local-c = make(<flag-option>, names: #("c"), help: "c help");
let positional-d = make(<positional-option>, names: #("d"), help: "d help");
let positional-e = make(<positional-option>,
repeated?: #t, names: #("e"), help: "e help");
let s1 = make(<subcommand-s1>, name: "s1", help: "s1 help");
add-option(s1, local-c);
add-option(s1, positional-d);
add-option(s1, positional-e);
let p = make(<command-line-parser>,
help: "main help",
subcommands: list(s1));
add-option(p, global-a);
add-option(p, global-b);

// Done with setup

assert-no-errors(parse-command-line(p, #["-a", "s1", "-c", "d", "e", "e"]));
assert-true(get-option-value(p, "a"));
assert-false(get-option-value(p, "b"));
assert-true(get-option-value(s1, "c"));
assert-equal("d", get-option-value(s1, "d"));
assert-equal(#["e", "e"], get-option-value(s1, "e"));
end test;

0 comments on commit a72ab27

Please sign in to comment.