Skip to content

Commit 08708fe

Browse files
authored
Merge pull request #15 from katyukha/dev-unparsed-args
Save unparsed (rest) args on result
2 parents bfb7531 + 1aa0ce1 commit 08708fe

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ commandr-test-*
1212
*.obj
1313
*.lst
1414
/commandr
15-
source/app.d
15+
source/app.d
16+
libcommandr.a
17+
*.sw[npo]

source/commandr/args.d

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class ProgramArgs {
6262
int[string] _flags;
6363
string[][string] _options;
6464
string[][string] _args;
65+
string[] _args_rest;
6566
ProgramArgs _parent;
6667
ProgramArgs _command;
6768
}
@@ -222,6 +223,27 @@ public class ProgramArgs {
222223
/// ditto
223224
alias args = argAll;
224225

226+
/**
227+
* Rest (unparsed) arguments.
228+
*
229+
* Useful, if you need to access unparsed arguments,
230+
* usually supplied after '--'.
231+
*
232+
* Returns:
233+
* array of arguments that were not handled by parser
234+
*
235+
* Examples:
236+
* ---
237+
* auto args = ["my-command", "--opt", "arg", "--", "other-arg", "o-arg"];
238+
* auto res = parse(args);
239+
*
240+
* // Not we can access unparsed args as res.argsRest
241+
* assert(res.argsRest == ["other-arg", "o-arg"])
242+
* ---
243+
*/
244+
public string[] argsRest() {
245+
return _args_rest;
246+
}
225247

226248
/**
227249
* Gets subcommand arguments.

source/commandr/parser.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ private ProgramArgs parseArgs(
189189
}
190190
}
191191

192+
if (args.length > 0)
193+
result._args_rest = args.dup;
194+
192195
if (result.flag("help")) {
193196
program.printHelp(helpConfig);
194197
exit(0);
@@ -527,6 +530,7 @@ unittest {
527530
.defaultValue("reee"))
528531
.parseArgs(args);
529532
assert(a.args("test") == ["reee"]);
533+
assert(a.argsRest == ["bar"]);
530534
assert(args == ["bar"]);
531535
}
532536

@@ -576,6 +580,7 @@ unittest {
576580
assert(a.args("test") == ["cccc"]);
577581
assert(a.command !is null);
578582
assert(a.command.name == "a");
583+
assert(a.command.argsRest == ["c"]);
579584
assert(args == ["c"]);
580585

581586
assertThrown!InvalidArgumentsException(

0 commit comments

Comments
 (0)