Skip to content

Commit

Permalink
CLI: Simplify extractCommandNameArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
Geod24 committed Feb 7, 2024
1 parent 55e3ad4 commit ce7a036
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,16 @@ CommandGroup[] getCommands() @safe pure nothrow
args = a list of string arguments that will be processed
Returns:
A structure with two members. `value` is the command name
`remaining` is a list of unprocessed arguments
The command name that was found (may be null).
*/
auto extractCommandNameArgument(string[] args)
string commandNameArgument(ref string[] args)
{
struct Result {
string value;
string[] remaining;
}

if (args.length >= 1 && !args[0].startsWith("-") && !args[0].canFind(":")) {
return Result(args[0], args[1 .. $]);
const result = args[0];
args = args[1 .. $];
return result;
}

return Result(null, args);
return null;
}

/// test extractCommandNameArgument usage
Expand Down Expand Up @@ -448,7 +443,8 @@ int runDubCommandLine(string[] args)

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

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

// extract the command
args = common_args.extractAllRemainingArgs();

auto command_name_argument = extractCommandNameArgument(args);

auto command_args = new CommandArgs(command_name_argument.remaining);
const command_name = commandNameArgument(args);
auto command_args = new CommandArgs(args);
Command cmd;

try {
cmd = handler.prepareCommand(command_name_argument.value, command_args);
cmd = handler.prepareCommand(command_name, command_args);
} catch (Exception e) {
logError("Error processing arguments: %s", e.msg);
logDiagnostic("Full exception: %s", e.toString().sanitize);
Expand All @@ -482,14 +476,14 @@ int runDubCommandLine(string[] args)
if (cmd is null) {
logInfoNoTag("USAGE: dub [--version] [<command>] [<options...>] [-- [<application arguments...>]]");
logInfoNoTag("");
logError("Unknown command: %s", command_name_argument.value);
logError("Unknown command: %s", command_name);
import std.algorithm.iteration : filter;
import std.uni : toUpper;
foreach (CommandGroup key; handler.commandGroups)
{
foreach (Command command; key.commands)
{
if (levenshteinDistance(command_name_argument.value, command.name) < 4) {
if (levenshteinDistance(command_name, command.name) < 4) {
logInfo("Did you mean '%s'?", command.name);
}
}
Expand Down

0 comments on commit ce7a036

Please sign in to comment.