-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Default subcommand * Update readme * Unit test
- Loading branch information
1 parent
bcfc961
commit 8b60bf9
Showing
5 changed files
with
120 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import argparse; | ||
import std.stdio: writeln; | ||
import std.sumtype: SumType, match; | ||
|
||
|
||
struct sum {} | ||
struct min {} | ||
struct max | ||
{ | ||
string foo; // --foo argument | ||
} | ||
|
||
struct Program | ||
{ | ||
int[] numbers; // --numbers argument | ||
|
||
// SumType indicates sub-command | ||
// Default!T marks T as default command | ||
SumType!(sum, min, Default!max) cmd; | ||
} | ||
|
||
// This mixin defines standard main function that parses command line and calls the provided function: | ||
mixin CLI!Program.main!((prog) | ||
{ | ||
static assert(is(typeof(prog) == Program)); | ||
|
||
prog.cmd.match!( | ||
(.max m) | ||
{ | ||
import std.algorithm: maxElement; | ||
writeln("max = ", prog.numbers.maxElement, " foo = ", m.foo); | ||
}, | ||
(.min) | ||
{ | ||
import std.algorithm: minElement; | ||
writeln("min = ", prog.numbers.minElement); | ||
}, | ||
(.sum) | ||
{ | ||
import std.algorithm: sum; | ||
writeln("sum = ", prog.numbers.sum); | ||
} | ||
); | ||
|
||
return 0; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"license": "BSL-1.0", | ||
"name": "sub_commands-default", | ||
"targetType":"executable", | ||
"sourcePaths":["."], | ||
"dependencies":{ "all:argparse":"*" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters