Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Config.namedArgChar to Config.namedArgPrefix #102

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1554,11 +1554,11 @@ enum cfg = {
assert(CLI!(cfg, T).parseArgs!((T t) { assert(t == T(["1","2","3","4","5"])); })(["-a","1,2,3","-a","4","5"]) == 0);
```

### Named argument character
### Named argument prefix character

`Config.namedArgChar` - the character that named arguments begin with.
`Config.namedArgPrefix` - the character that named arguments begin with.

Default is dash `-`.
Default is dash (`-`).

### End of arguments

Expand Down
4 changes: 2 additions & 2 deletions source/argparse/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ struct Config
char arraySep = char.init;

/**
The option character.
The prefix for argument name.
Defaults to '-'.
*/
char namedArgChar = '-';
char namedArgPrefix = '-';

/**
The string that conventionally marks the end of all options.
Expand Down
2 changes: 1 addition & 1 deletion source/argparse/internal/arguments.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import std.traits: getUDAs;
package(argparse) struct ArgumentInfo
{
string[] names;
string[] displayNames; // names prefixed with Config.namedArgChar
string[] displayNames; // names prefixed with Config.namedArgPrefix

string displayName() const
{
Expand Down
2 changes: 1 addition & 1 deletion source/argparse/internal/argumentuda.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private template getArgumentUDAImpl(Config config, MEMBERTYPE, string defaultNam
uda.info.displayNames = [ uda.info.placeholder ];
else
{
alias toDisplayName = _ => ( _.length == 1 ? text(config.namedArgChar, _) : text(config.namedArgChar, config.namedArgChar, _));
alias toDisplayName = _ => ( _.length == 1 ? text(config.namedArgPrefix, _) : text(config.namedArgPrefix, config.namedArgPrefix, _));

uda.info.displayNames = uda.info.names.map!toDisplayName.array;
}
Expand Down
2 changes: 1 addition & 1 deletion source/argparse/internal/command.d
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ package(argparse) Command createCommand(Config config, COMMAND_TYPE, CommandInfo
enum uda = getMemberArgumentUDA!(config, COMMAND_TYPE, symbol, NamedArgument);

static foreach(name; uda.info.names)
static assert(name[0] != config.namedArgChar, "Argument name should not begin with '"~config.namedArgChar~"': "~name);
static assert(name[0] != config.namedArgPrefix, "Argument name should not begin with '"~config.namedArgPrefix~"': "~name);

res.arguments.addArgument!(config, COMMAND_TYPE, symbol, uda.info);
}}
Expand Down
2 changes: 1 addition & 1 deletion source/argparse/internal/parsehelpers.d
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ package(argparse) template ValueInList(alias values, TYPE)
enum valuesAA = assocArray(values, false.repeat);
enum allowedValues = values.to!(string[]);

auto valueStyle = (param.name.length == 0 || param.name[0] != param.config.namedArgChar) ?
auto valueStyle = (param.name.length == 0 || param.name[0] != param.config.namedArgPrefix) ?
param.config.styling.positionalArgumentValue :
param.config.styling.namedArgumentValue;

Expand Down
12 changes: 6 additions & 6 deletions source/argparse/internal/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import argparse.internal.command: Command, createCommand;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private string[] consumeValuesFromCLI(ref string[] args, ulong minValuesCount, ulong maxValuesCount, char namedArgChar)
private string[] consumeValuesFromCLI(ref string[] args, ulong minValuesCount, ulong maxValuesCount, char namedArgPrefix)
{
import std.range: empty, front, popFront;

Expand All @@ -32,7 +32,7 @@ private string[] consumeValuesFromCLI(ref string[] args, ulong minValuesCount, u

while(!args.empty &&
values.length < maxValuesCount &&
(args.front.length == 0 || args.front[0] != namedArgChar))
(args.front.length == 0 || args.front[0] != namedArgPrefix))
{
values ~= args.front;
args.popFront();
Expand Down Expand Up @@ -83,18 +83,18 @@ private struct Parser(Config config)
if(arg == config.endOfArgs)
return Argument(EndOfArgs.init);

if(arg[0] != config.namedArgChar)
if(arg[0] != config.namedArgPrefix)
return Argument(Positional.init);

if(arg.length == 1 || arg.length == 2 && arg[1] == config.namedArgChar)
if(arg.length == 1 || arg.length == 2 && arg[1] == config.namedArgPrefix)
return Argument.init;

auto idxAssignChar = config.assignChar == char.init ? -1 : arg.indexOf(config.assignChar);

immutable string nameWithDash = idxAssignChar < 0 ? arg : arg[0 .. idxAssignChar];
immutable string value = idxAssignChar < 0 ? null : arg[idxAssignChar + 1 .. $];

return arg[1] == config.namedArgChar
return arg[1] == config.namedArgPrefix
? Argument(NamedLong (config.convertCase(nameWithDash[2..$]), nameWithDash, value))
: Argument(NamedShort(config.convertCase(nameWithDash[1..$]), nameWithDash, value));
}
Expand All @@ -103,7 +103,7 @@ private struct Parser(Config config)
{
scope(exit) idxParsedArgs[foundArg.index] = true;

auto rawValues = value !is null ? [value] : consumeValuesFromCLI(args, foundArg.arg.minValuesCount.get, foundArg.arg.maxValuesCount.get, config.namedArgChar);
auto rawValues = value !is null ? [value] : consumeValuesFromCLI(args, foundArg.arg.minValuesCount.get, foundArg.arg.maxValuesCount.get, config.namedArgPrefix);

static if(completionMode)
return cmd.completeArgument(cmdStack, foundArg.index, nameWithDash, rawValues);
Expand Down
Loading