Skip to content

Commit

Permalink
New way of parsing multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-zherikov committed Nov 9, 2024
1 parent 70ad448 commit f7433ec
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 315 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
* `./prog -b -sabc`
* `./prog -bsabc`
* `./prog -bs=abc`
* Fixes for parsing of multiple values. Only these formats are supported:
* `./prog --arg value1 value2 value3`
* `./prog --arg=value1,value2,value3`
* Removed support for delegate in `Config.errorHandler`, `Description`, `ShortDescription`, `Usage` and `Epilog` because of compiler's `closures are not yet supported in CTFE`.

### Other changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ struct T
}

T t1;
assert(CLI!T.parseArgs(t1, ["-a","1:2:3","-a","4","5"]));
assert(CLI!T.parseArgs(t1, ["-a=1:2:3","-a","4","5"]));
assert(t1 == T(["1:2:3","4","5"]));

enum Config cfg = { valueSep: ':' };

T t2;
assert(CLI!(cfg, T).parseArgs(t2, ["-a","1:2:3","-a","4","5"]));
assert(CLI!(cfg, T).parseArgs(t2, ["-a=1:2:3","-a","4","5"]));
assert(t2 == T(["1","2","3","4","5"]));
4 changes: 1 addition & 3 deletions docs/code_snippets/types_array_comma.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ struct T
int[] a;
}

enum Config cfg = { valueSep: ',' };

T t;
assert(CLI!(cfg, T).parseArgs(t, ["-a=1,2,3","-a","4,5"]));
assert(CLI!T.parseArgs(t, ["-a=1,2,3","-a","4","5"]));
assert(t == T([1,2,3,4,5]));
4 changes: 1 addition & 3 deletions docs/code_snippets/types_assoc_array_comma.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ struct T
int[string] a;
}

enum Config cfg = { valueSep: ',' };

T t;
assert(CLI!(cfg, T).parseArgs(t, ["-a=foo=3,boo=7","-a","bar=4,baz=9"]));
assert(CLI!T.parseArgs(t, ["-a=foo=3,boo=7","-a","bar=4","baz=9"]));
assert(t == T(["foo":3,"boo":7,"bar":4,"baz":9]));
11 changes: 9 additions & 2 deletions docs/topics/Arity.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Arity

Sometimes an argument might accept more than one value. This is especially a case when a data member is an array.
Sometimes an argument might accept more than one value. This is especially a case when a data member is an array or associative array.
In this case `argparse` supports two ways of specifying multiple values for an argument:
- `--arg value1 value2 ...`
- `--arg=value1,value2,...`
> Note that `=` is a value of [`Config.assignChar`](Config.md#assignChar) and `,` is a value of [`Config.valueSep`](Config.md#valueSep)
>
{style="note"}

`argparse` supports these use cases:

`argparse` supports these use cases for arity:
- Exact number of values.
- Limited range of minimum-maximum number of values.
- Unlimited range where only minimum number of values is provided (e.g. argument accepts _any number_ of values).
Expand Down
11 changes: 4 additions & 7 deletions docs/topics/reference/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Assign character {id="assignChar"}

`Config.assignChar` is an assignment character used in arguments with value: `-a=5`, `-b=foo`.
`Config.assignChar` is an assignment character used in arguments with value: `-a=5`, `-boo=foo`.

Default is equal sign `=`.

Expand All @@ -15,16 +15,13 @@ Example:

## Value separator {id="valueSep"}

When `Config.valueSep` is set to `char.init`, values to array and associative-array receivers are treated as an individual
value. That is, only one argument is appended/inserted per appearance of the argument. If `valueSep` is set to something
else, then each value is first split by the separator, and the individual pieces are treated as values to the same
argument.
`Config.valueSep` is a separator that is used to extract argument values: `-a=5,6,7`, `--boo=foo,far,zoo`.

Default is `char.init`.
Default is `,`.

Example:

<code-block src="code_snippets/config_arraySep.d" lang="c++"/>
<code-block src="code_snippets/config_valueSep.d" lang="c++"/>

## Named argument prefix {id="namedArgPrefix"}

Expand Down
Loading

0 comments on commit f7433ec

Please sign in to comment.