Skip to content

Commit ac78ae1

Browse files
Geod24dlang-bot
authored andcommitted
CLI: Simplify extractCommandNameArgument
1 parent 398eed8 commit ac78ae1

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

source/dub/commandline.d

+45-33
Original file line numberDiff line numberDiff line change
@@ -82,44 +82,58 @@ CommandGroup[] getCommands() @safe pure nothrow
8282
args = a list of string arguments that will be processed
8383
8484
Returns:
85-
A structure with two members. `value` is the command name
86-
`remaining` is a list of unprocessed arguments
85+
The command name that was found (may be null).
8786
*/
88-
auto extractCommandNameArgument(string[] args)
87+
string commandNameArgument(ref string[] args)
8988
{
90-
struct Result {
91-
string value;
92-
string[] remaining;
93-
}
94-
9589
if (args.length >= 1 && !args[0].startsWith("-") && !args[0].canFind(":")) {
96-
return Result(args[0], args[1 .. $]);
90+
const result = args[0];
91+
args = args[1 .. $];
92+
return result;
9793
}
98-
99-
return Result(null, args);
94+
return null;
10095
}
10196

10297
/// test extractCommandNameArgument usage
10398
unittest {
104-
/// It returns an empty string on when there are no args
105-
assert(extractCommandNameArgument([]).value == "");
106-
assert(extractCommandNameArgument([]).remaining == []);
107-
108-
/// It returns the first argument when it does not start with `-`
109-
assert(extractCommandNameArgument(["test"]).value == "test");
99+
{
100+
string[] args;
101+
/// It returns an empty string on when there are no args
102+
assert(commandNameArgument(args) is null);
103+
assert(!args.length);
104+
}
110105

111-
/// There is nothing to extract when the arguments only contain the `test` cmd
112-
assert(extractCommandNameArgument(["test"]).remaining == []);
106+
{
107+
string[] args = [ "test" ];
108+
/// It returns the first argument when it does not start with `-`
109+
assert(commandNameArgument(args) == "test");
110+
/// There is nothing to extract when the arguments only contain the `test` cmd
111+
assert(!args.length);
112+
}
113113

114-
/// It extracts two arguments when they are not a command
115-
assert(extractCommandNameArgument(["-a", "-b"]).remaining == ["-a", "-b"]);
114+
{
115+
string[] args = [ "-a", "-b" ];
116+
/// It extracts two arguments when they are not a command
117+
assert(commandNameArgument(args) is null);
118+
assert(args == ["-a", "-b"]);
119+
}
116120

117-
/// It returns the an empty string when it starts with `-`
118-
assert(extractCommandNameArgument(["-test"]).value == "");
121+
{
122+
string[] args = [ "-test" ];
123+
/// It returns the an empty string when it starts with `-`
124+
assert(commandNameArgument(args) is null);
125+
assert(args.length == 1);
126+
}
119127

120-
// Sub package names are ignored as command names
121-
assert(extractCommandNameArgument(["foo:bar"]).value == "");
122-
assert(extractCommandNameArgument([":foo"]).value == "");
128+
{
129+
string[] args = [ "foo:bar" ];
130+
// Sub package names are ignored as command names
131+
assert(commandNameArgument(args) is null);
132+
assert(args.length == 1);
133+
args[0] = ":foo";
134+
assert(commandNameArgument(args) is null);
135+
assert(args.length == 1);
136+
}
123137
}
124138

125139
/** Handles the Command Line options and commands.
@@ -464,14 +478,12 @@ int runDubCommandLine(string[] args)
464478

465479
// extract the command
466480
args = common_args.extractAllRemainingArgs();
467-
468-
auto command_name_argument = extractCommandNameArgument(args);
469-
470-
auto command_args = new CommandArgs(command_name_argument.remaining);
481+
const command_name = commandNameArgument(args);
482+
auto command_args = new CommandArgs(args);
471483
Command cmd;
472484

473485
try {
474-
cmd = handler.prepareCommand(command_name_argument.value, command_args);
486+
cmd = handler.prepareCommand(command_name, command_args);
475487
} catch (Exception e) {
476488
logError("Error processing arguments: %s", e.msg);
477489
logDiagnostic("Full exception: %s", e.toString().sanitize);
@@ -482,14 +494,14 @@ int runDubCommandLine(string[] args)
482494
if (cmd is null) {
483495
logInfoNoTag("USAGE: dub [--version] [<command>] [<options...>] [-- [<application arguments...>]]");
484496
logInfoNoTag("");
485-
logError("Unknown command: %s", command_name_argument.value);
497+
logError("Unknown command: %s", command_name);
486498
import std.algorithm.iteration : filter;
487499
import std.uni : toUpper;
488500
foreach (CommandGroup key; handler.commandGroups)
489501
{
490502
foreach (Command command; key.commands)
491503
{
492-
if (levenshteinDistance(command_name_argument.value, command.name) < 4) {
504+
if (levenshteinDistance(command_name, command.name) < 4) {
493505
logInfo("Did you mean '%s'?", command.name);
494506
}
495507
}

0 commit comments

Comments
 (0)