Skip to content

Commit

Permalink
Add Required to MutuallyExclusive and RequiredTogether (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-zherikov authored May 31, 2022
1 parent 4527410 commit e5f8ef8
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,27 @@ assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a","-b","b"]) != 0);

**Note that parenthesis are required in this UDA to work correctly.**

Set of mutually exclusive arguments can be marked as required in order to require exactly one of the arguments:

```d
struct T
{
@(MutuallyExclusive().Required())
{
string a;
string b;
}
}
// Either argument is allowed
assert(CLI!T.parseArgs!((T t) {})(["-a","a"]) == 0);
assert(CLI!T.parseArgs!((T t) {})(["-b","b"]) == 0);
// Both arguments or no argument is not allowed
assert(CLI!T.parseArgs!((T t) { assert(false); })([]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a","-b","b"]) != 0);
```

### Mutually required arguments

Mutually required arguments (i.e. those that require other arguments) can be declared using `RequiredTogether()` UDA:
Expand All @@ -669,6 +690,27 @@ assert(CLI!T.parseArgs!((T t) { assert(false); })(["-b","b"]) != 0);

**Note that parenthesis are required in this UDA to work correctly.**

Set of mutually required arguments can be marked as required in order to require all arguments:

```d
struct T
{
@(RequiredTogether().Required())
{
string a;
string b;
}
}
// Both arguments are allowed
assert(CLI!T.parseArgs!((T t) {})(["-a","a","-b","b"]) == 0);
// Single argument or no argument is not allowed
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a"]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-b","b"]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })([]) != 0);
```

## Commands

Sophisticated command-line tools, like `git`, have many subcommands (e.g., `commit`, `push` etc.), each with its own set
Expand Down
84 changes: 83 additions & 1 deletion source/argparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ private struct RestrictionGroup
Type type;

private size_t[] arguments;

private bool required;

auto ref Required()
{
required = true;
return this;
}
}

auto RequiredTogether(string file=__FILE__, uint line = __LINE__)()
Expand Down Expand Up @@ -517,6 +525,23 @@ private struct Restrictions

return true;
}

static bool RequiredAnyOf(in Config config,
in bool[size_t] cliArgs,
in size_t[] restrictionArgs,
in ArgumentInfo[] allArgs)
{
import std.algorithm: map;
import std.array: join;

foreach(index; restrictionArgs)
if(index in cliArgs)
return true;

config.onError("One of the following arguments is required: '", restrictionArgs.map!(_ => allArgs[_].names[0].getArgumentName(config)).join("', '"), "'");

return false;
}
}

private struct Arguments
Expand Down Expand Up @@ -640,6 +665,10 @@ private struct Arguments
return false;

foreach(restriction; restrictionGroups)
{
if(restriction.required && !Restrictions.RequiredAnyOf(config, cliArgs, restriction.arguments, arguments))
return false;

final switch(restriction.type)
{
case RestrictionGroup.Type.together:
Expand All @@ -651,6 +680,7 @@ private struct Arguments
return false;
break;
}
}

return true;
}
Expand Down Expand Up @@ -4630,6 +4660,32 @@ unittest
assert(parseCLIArgs!T([], (T t) {}) == 0);
}

unittest
{
@Command("MYPROG")
struct T
{
@(MutuallyExclusive().Required())
{
string a;
string b;
}
}

// Either argument is allowed
assert(CLI!T.parseArgs!((T t) {})(["-a","a"]) == 0);
assert(CLI!T.parseArgs!((T t) {})(["-b","b"]) == 0);

// Both arguments or no argument is not allowed
assert(CLI!T.parseArgs!((T t) { assert(false); })([]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a","-b","b"]) != 0);

assert(parseCLIArgs!T(["-a","a","-b","b"], (T t) { assert(false); }) != 0);
assert(parseCLIArgs!T(["-a","a"], (T t) {}) == 0);
assert(parseCLIArgs!T(["-b","b"], (T t) {}) == 0);
assert(parseCLIArgs!T([], (T t) { assert(false); }) != 0);
}

unittest
{
@Command("MYPROG")
Expand All @@ -4646,7 +4702,7 @@ unittest
assert(CLI!T.parseArgs!((T t) {})(["-a","a","-b","b"]) == 0);
assert(CLI!T.parseArgs!((T t) {})([]) == 0);

// Only one argument is not allowed
// Single argument is not allowed
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a"]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-b","b"]) != 0);

Expand All @@ -4656,6 +4712,32 @@ unittest
assert(parseCLIArgs!T([], (T t) {}) == 0);
}

unittest
{
@Command("MYPROG")
struct T
{
@(RequiredTogether().Required())
{
string a;
string b;
}
}

// Both arguments are allowed
assert(CLI!T.parseArgs!((T t) {})(["-a","a","-b","b"]) == 0);

// Single argument or no argument is not allowed
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-a","a"]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })(["-b","b"]) != 0);
assert(CLI!T.parseArgs!((T t) { assert(false); })([]) != 0);

assert(parseCLIArgs!T(["-a","a","-b","b"], (T t) {}) == 0);
assert(parseCLIArgs!T(["-a","a"], (T t) { assert(false); }) != 0);
assert(parseCLIArgs!T(["-b","b"], (T t) { assert(false); }) != 0);
assert(parseCLIArgs!T([], (T t) { assert(false); }) != 0);
}


private void wrapMutiLine(Output, S)(auto ref Output output,
S s,
Expand Down

0 comments on commit e5f8ef8

Please sign in to comment.