-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a feature that are allowed to assign arguments on command line to…
… options that are registered by Command.argToOpt as from Source.Cli. its priority is only behind the Source.Cli which is explicitily specified on command line
- Loading branch information
Showing
9 changed files
with
1,110 additions
and
344 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"sub": { | ||
"options": { | ||
"first": 12, | ||
"second": 13 | ||
} | ||
}, | ||
"arguments": [12, 13], | ||
"options": { | ||
"multi": 4 | ||
} | ||
} |
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,45 @@ | ||
module examples.config; | ||
|
||
import std.stdio; | ||
|
||
import cmdline; | ||
|
||
void main(in string[] argv) { | ||
|
||
program | ||
.name("config") | ||
.description("test the feature of config option") | ||
.setConfigOption | ||
.arguments!(int, int)("<first> <second>") | ||
.argumentDesc("first", "the first num") | ||
.argumentDesc("second", "the second num") | ||
.option("-m|--multi <num>", "the multi num", 12) | ||
.option("-N, --negate", "decide to negate", false) | ||
.action((opts, fst, snd) { | ||
int first = fst.get!int; | ||
int second = snd.get!int; | ||
int multi = opts("multi").get!int; | ||
bool negate = opts("negate").get!bool; | ||
multi = negate ? -multi : multi; | ||
writefln("%d * (%d + %d) = %d", multi, first, second, | ||
multi * (first + second)); | ||
}); | ||
|
||
auto farg = program.findArgument("first"); | ||
auto sarg = program.findArgument("second"); | ||
farg.defaultVal(65); | ||
sarg.defaultVal(35); | ||
|
||
program | ||
.command("sub") | ||
.description("sub the two numbers") | ||
.option("-f, --first <int>", "", 65) | ||
.option("-s, --second <int>", "", 35) | ||
.action((opts) { | ||
int first = opts("first").get!int; | ||
int second = opts("second").get!int; | ||
writefln("sub:\t%d - %d = %d", first, second, first - second); | ||
}); | ||
|
||
program.parse(argv); | ||
} |
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,44 @@ | ||
module examples.implies; | ||
|
||
import std.stdio; | ||
import std.format; | ||
import std.conv; | ||
import cmdline; | ||
|
||
void main(in string[] argv) { | ||
program.name("implies"); | ||
program.option("--foo", "foo bool to be implied"); | ||
program.option("--bar <int-num>", "bar for int implied", 0); | ||
|
||
auto im_opt = createOption!int("--imply <int-num>", "implier with int num"); | ||
im_opt.choices(0, 1); | ||
|
||
im_opt.implies(["foo", "fox"]); | ||
im_opt.implies("bar", 13); | ||
im_opt.implies("baz", false); | ||
|
||
program.addOption(im_opt); | ||
|
||
program.parse(argv); | ||
|
||
OptsWrap opts = program.getOpts; | ||
auto im_raw = opts("imply"); | ||
auto foo_raw = opts("foo"); | ||
auto fox_raw = opts("fox"); | ||
auto baz_raw = opts("baz"); | ||
|
||
string bar_info = format!"set bar <%d>"(opts("bar").get!int); | ||
string im_info = im_raw.isValid ? | ||
format!"set imply <%d>"(im_raw.get!int) : ("unset imply"); | ||
string foo_info = foo_raw.isValid ? | ||
format!"set foo `%s`"(foo_raw.get!bool | ||
.to!string) : ("unset foo"); | ||
string fox_info = fox_raw.isValid ? | ||
format!"set fox `%s`"(fox_raw.get!bool | ||
.to!string) : ("unset fox"); | ||
string baz_info = baz_raw.isValid ? | ||
format!"set baz %s"(baz_raw.get!bool | ||
.to!string) : ("unset baz"); | ||
|
||
writefln("%s, %s, %s, %s, %s.", bar_info, im_info, foo_info, fox_info, baz_info); | ||
} |
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,42 @@ | ||
module examples.optx; | ||
|
||
import std.stdio; | ||
import std.conv; | ||
import cmdline; | ||
|
||
void main(in string[] argv) { | ||
program | ||
.name("optx") | ||
.description("test the value trait of options"); | ||
|
||
|
||
auto range_opt = createOption!int("-r, --rgnum <num>"); | ||
range_opt.rangeOf(0, 9); | ||
range_opt.defaultVal(0); | ||
range_opt.parser!((string s) => s.to!int); | ||
range_opt.processor!((int v) => cast(int) v + 1); | ||
|
||
auto choice_opt = createOption!string("-c, --chstr <str>"); | ||
choice_opt.choices("a", "b", "c", "d", "e", "f", "g", "h"); | ||
choice_opt.defaultVal("a"); | ||
choice_opt.processor!((string v) => v ~ "+"); | ||
|
||
auto reduce_opt = createOption!int("-n, --nums <numbers...>"); | ||
reduce_opt.defaultVal(12, 13, 14); | ||
reduce_opt.processReducer!((int a, int b) => a + b); | ||
|
||
program | ||
.addOption(range_opt) | ||
.addOption(choice_opt) | ||
.addOption(reduce_opt); | ||
|
||
program.argToOpt("-c", "-n"); | ||
|
||
program.parse(argv); | ||
|
||
int range_int = program.getOptVal!int("-r"); | ||
string choice_str = program.getOptVal!string("-c"); | ||
int reduce_int = program.getOptVal!int("nums"); | ||
|
||
writefln("%d, %s, %d", range_int, choice_str, reduce_int); | ||
} |
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,27 @@ | ||
module examples.preset; | ||
|
||
import std.stdio; | ||
|
||
import cmdline; | ||
|
||
void main(in string[] argv) { | ||
program.name("preset"); | ||
|
||
Option pre_opt = createOption!string("-p,--pre [name]"); | ||
pre_opt.defaultVal("dmd"); | ||
pre_opt.preset("dub"); | ||
program.addOption(pre_opt); | ||
|
||
program.addHelpText(AddHelpPos.Before, ` | ||
Try the following: | ||
$ preset | ||
$ preset --pre | ||
$ preset -prdmd | ||
`); | ||
|
||
program.parse(argv); | ||
|
||
OptsWrap opts = program.getOpts; | ||
string pre_info = opts("pre").get!string; | ||
writefln("--pre [%s]", pre_info); | ||
} |
Oops, something went wrong.