Skip to content

Commit

Permalink
Proper support of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-zherikov committed Jun 28, 2021
1 parent 660b061 commit 4dd83c8
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 7 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,42 @@ assert(["-a","foo=3,boo=7"].parseCLIArgs!T(cfg).get.a == ["foo":3,"boo":7]);

In general, the keys and values can be of any parsable types.

### Callback

An argument can be bound to a function with one of the following signatures
(return value, if any, is ignored):

- `... function()`

In this case, the argument is treated as a flag and the function is called every time when
the argument is seen in command line.

- `... function(string)`

In this case, the argument has exactly one value and the function is called every time when
the argument is seen in command line and the value specified in command line is provided into `string` parameter.

- `... function(string[])`

In this case, the argument has zero or more values and the function is called every time when
the argument is seen in command line and the set of values specified in command line is provided into `string[]` parameter.

- `... function(RawParam)`

In this case, the argument has one or more values and the function is called every time when
the argument is seen in command line and the set of values specified in command line is provided into parameter.

```d
static struct T
{
int a;
@(NamedArgument("a")) void foo() { a++; }
}
static assert(["-a","-a","-a","-a"].parseCLIArgs!T.get.a == 4);
```

## Parsing customization

Some time the functionality provided out of the box is not enough and it needs to be tuned.
Expand Down
25 changes: 23 additions & 2 deletions examples/hello_world.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ unittest
{
import argparse;

struct Params
static struct Params
{
// Positional arguments are required by default
@PositionalArgument(0)
Expand All @@ -30,10 +30,31 @@ unittest
// Use array to store multiple values
@NamedArgument("array")
int[] array;

// Callback with no args (flag)
@NamedArgument("cb")
void callback() {}

// Callback with single value
@NamedArgument("cb1")
void callback1(string value) { assert(value == "cb-value"); }

// Callback with zero or more values
@NamedArgument("cb2")
void callback2(string[] value) { assert(value == ["cb-v1","cb-v2"]); }
}

// Can even work at compile time
enum params = (["--flag","--num","100","Jake","--array","1","2","3","--enum","foo"].parseCLIArgs!Params).get;
enum params = ([
"--flag",
"--num","100",
"Jake",
"--array","1","2","3",
"--enum","foo",
"--cb",
"--cb1","cb-value",
"--cb2","cb-v1","cb-v2",
].parseCLIArgs!Params).get;

static assert(params.name == "Jake");
static assert(params.unused == Params.init.unused);
Expand Down
113 changes: 108 additions & 5 deletions source/argparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ if(!is(T == void))
enum min = 1;
enum max = ulong.max;
}
else static if(is(T == function))
{
// ... function()
static if(__traits(compiles, { T(); }))
{
enum min = 0;
enum max = 0;
}
// ... function(string value)
else static if(__traits(compiles, { T(string.init); }))
{
enum min = 1;
enum max = 1;
}
// ... function(string[] value)
else static if(__traits(compiles, { T([string.init]); }))
{
enum min = 0;
enum max = ulong.max;
}
// ... function(RawParam param)
else static if(__traits(compiles, { T(RawParam.init); }))
{
enum min = 1;
enum max = ulong.max;
}
else
static assert(false, "Unsupported callback: " ~ T.stringof);
}
else
static assert(false, "Type is not supported: " ~ T.stringof);
}
Expand Down Expand Up @@ -413,12 +442,10 @@ private struct Arguments(T)
static if(is(typeof(__traits(getMember, receiver, __traits(identifier, sym))) == function))
{
auto target = &__traits(getMember, receiver, __traits(identifier, sym));
return argUDA.parsingFunc.parse(config, argName, target, rawValues);
return argUDA.parsingFunc.parse(target, param);
}
else
//return argUDA.parsingFunc.parse(config, argName, __traits(getMember, receiver, __traits(identifier, sym)), rawValues);
return argUDA.parsingFunc.parse(__traits(getMember, receiver, __traits(identifier, sym)),
param);
return argUDA.parsingFunc.parse(__traits(getMember, receiver, __traits(identifier, sym)), param);
}
);

Expand Down Expand Up @@ -1008,6 +1035,59 @@ private struct Actions
{
param ~= value;
}

static auto CallFunction(F)(ref F func, RawParam param)
{
// ... func()
static if(__traits(compiles, { func(); }))
{
func();
}
// ... func(string value)
else static if(__traits(compiles, { func(param.value[0]); }))
{
foreach(value; param.value)
func(value);
}
// ... func(string[] value)
else static if(__traits(compiles, { func(param.value); }))
{
func(param.value);
}
// ... func(RawParam param)
else static if(__traits(compiles, { func(param); }))
{
func(param);
}
else
static assert(false, "Unsupported callback: " ~ F.stringof);
}

static auto CallFunctionNoParam(F)(ref F func, Param!void param)
{
// ... func()
static if(__traits(compiles, { func(); }))
{
func();
}
// ... func(string value)
else static if(__traits(compiles, { func(string.init); }))
{
func(string.init);
}
// ... func(string[] value)
else static if(__traits(compiles, { func([]); }))
{
func([]);
}
// ... func(Param!void param)
else static if(__traits(compiles, { func(param); }))
{
func(param);
}
else
static assert(false, "Unsupported callback: " ~ F.stringof);
}
}

unittest
Expand Down Expand Up @@ -1701,7 +1781,18 @@ if(!is(T == void))
(ref T param) {} // no-value action
);
}
else
else static if(is(T == delegate))
{
alias DefaultValueParseFunctions = ValueParseFunctions!(
void, // pre process
void, // pre validate
Parsers.PassThrough, // parse
void, // validate
Actions.CallFunction!T, // action
Actions.CallFunctionNoParam!T // no-value action
);
}
else
static assert(false, "Type is not supported: " ~ T.stringof);
}

Expand Down Expand Up @@ -2162,3 +2253,15 @@ unittest

static assert(["-a","!4"].parseCLIArgs!T.get.a == 4);
}

unittest
{
static struct T
{
int a;

@(NamedArgument("a")) void foo() { a++; }
}

static assert(["-a","-a","-a","-a"].parseCLIArgs!T.get.a == 4);
}

0 comments on commit 4dd83c8

Please sign in to comment.