Skip to content

Commit

Permalink
add a feature that are allowed to assign arguments on command line to…
Browse files Browse the repository at this point in the history
… 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
malyjacob committed Jul 6, 2024
1 parent 5d5251f commit 9a67792
Show file tree
Hide file tree
Showing 9 changed files with 1,110 additions and 344 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"description": "A cmd line tool library",
"license": "MIT",
"name": "cmdline",
"version": "1.0.2",
"version": "1.1.0",
"targetName": "cmdline",
"targetPath": "build",
"targetType": "library",
Expand Down
12 changes: 12 additions & 0 deletions examples/bin/config.config.json
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
}
}
45 changes: 45 additions & 0 deletions examples/config.d
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);
}
30 changes: 28 additions & 2 deletions examples/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"\"$DUB\" build -c greet",
"\"$DUB\" build -c variadic",
"\"$DUB\" build -c defaultval",
"\"$DUB\" build -c env"
"\"$DUB\" build -c env",
"\"$DUB\" build -c implies",
"\"$DUB\" build -c preset",
"\"$DUB\" build -c optx"
]
},
{
Expand All @@ -42,7 +45,10 @@
"\"$DUB\" build -c greet -b release",
"\"$DUB\" build -c variadic -b release",
"\"$DUB\" build -c defaultval -b release",
"\"$DUB\" build -c env -b release"
"\"$DUB\" build -c env -b release",
"\"$DUB\" build -c implies -b release",
"\"$DUB\" build -c preset -b release",
"\"$DUB\" build -c optx -b release"
]
},
{
Expand Down Expand Up @@ -94,6 +100,26 @@
"name": "env",
"targetName": "env",
"mainSourceFile": "./env.d"
},
{
"name": "implies",
"targetName": "implies",
"mainSourceFile": "./implies.d"
},
{
"name": "preset",
"targetName": "preset",
"mainSourceFile": "./preset.d"
},
{
"name": "config",
"targetName": "config",
"mainSourceFile": "./config.d"
},
{
"name": "optx",
"targetName": "optx",
"mainSourceFile": "./optx.d"
}
]
}
44 changes: 44 additions & 0 deletions examples/implies.d
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);
}
42 changes: 42 additions & 0 deletions examples/optx.d
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);
}
27 changes: 27 additions & 0 deletions examples/preset.d
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);
}
Loading

0 comments on commit 9a67792

Please sign in to comment.