Skip to content

Commit 6720b40

Browse files
committed
CLI: Simplify extractCommandNameArgument
1 parent 55e3ad4 commit 6720b40

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

source/dub/commandline.d

+11-18
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,16 @@ 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
@@ -464,14 +459,12 @@ int runDubCommandLine(string[] args)
464459

465460
// extract the command
466461
args = common_args.extractAllRemainingArgs();
467-
468-
auto command_name_argument = extractCommandNameArgument(args);
469-
470-
auto command_args = new CommandArgs(command_name_argument.remaining);
462+
const command_name = commandNameArgument(args);
463+
auto command_args = new CommandArgs(args);
471464
Command cmd;
472465

473466
try {
474-
cmd = handler.prepareCommand(command_name_argument.value, command_args);
467+
cmd = handler.prepareCommand(command_name, command_args);
475468
} catch (Exception e) {
476469
logError("Error processing arguments: %s", e.msg);
477470
logDiagnostic("Full exception: %s", e.toString().sanitize);
@@ -482,14 +475,14 @@ int runDubCommandLine(string[] args)
482475
if (cmd is null) {
483476
logInfoNoTag("USAGE: dub [--version] [<command>] [<options...>] [-- [<application arguments...>]]");
484477
logInfoNoTag("");
485-
logError("Unknown command: %s", command_name_argument.value);
478+
logError("Unknown command: %s", command_name);
486479
import std.algorithm.iteration : filter;
487480
import std.uni : toUpper;
488481
foreach (CommandGroup key; handler.commandGroups)
489482
{
490483
foreach (Command command; key.commands)
491484
{
492-
if (levenshteinDistance(command_name_argument.value, command.name) < 4) {
485+
if (levenshteinDistance(command_name, command.name) < 4) {
493486
logInfo("Did you mean '%s'?", command.name);
494487
}
495488
}

0 commit comments

Comments
 (0)