Skip to content

Commit ce7a036

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

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

source/dub/commandline.d

+13-19
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
@@ -448,7 +443,8 @@ int runDubCommandLine(string[] args)
448443

449444
auto common_args = new CommandArgs(args[1..$]);
450445

451-
try handler.prepareOptions(common_args);
446+
try
447+
args = handler.prepareOptions(common_args);
452448
catch (Exception e) {
453449
logError("Error processing arguments: %s", e.msg);
454450
logDiagnostic("Full exception: %s", e.toString().sanitize);
@@ -464,14 +460,12 @@ int runDubCommandLine(string[] args)
464460

465461
// extract the command
466462
args = common_args.extractAllRemainingArgs();
467-
468-
auto command_name_argument = extractCommandNameArgument(args);
469-
470-
auto command_args = new CommandArgs(command_name_argument.remaining);
463+
const command_name = commandNameArgument(args);
464+
auto command_args = new CommandArgs(args);
471465
Command cmd;
472466

473467
try {
474-
cmd = handler.prepareCommand(command_name_argument.value, command_args);
468+
cmd = handler.prepareCommand(command_name, command_args);
475469
} catch (Exception e) {
476470
logError("Error processing arguments: %s", e.msg);
477471
logDiagnostic("Full exception: %s", e.toString().sanitize);
@@ -482,14 +476,14 @@ int runDubCommandLine(string[] args)
482476
if (cmd is null) {
483477
logInfoNoTag("USAGE: dub [--version] [<command>] [<options...>] [-- [<application arguments...>]]");
484478
logInfoNoTag("");
485-
logError("Unknown command: %s", command_name_argument.value);
479+
logError("Unknown command: %s", command_name);
486480
import std.algorithm.iteration : filter;
487481
import std.uni : toUpper;
488482
foreach (CommandGroup key; handler.commandGroups)
489483
{
490484
foreach (Command command; key.commands)
491485
{
492-
if (levenshteinDistance(command_name_argument.value, command.name) < 4) {
486+
if (levenshteinDistance(command_name, command.name) < 4) {
493487
logInfo("Did you mean '%s'?", command.name);
494488
}
495489
}

0 commit comments

Comments
 (0)