@@ -82,44 +82,58 @@ CommandGroup[] getCommands() @safe pure nothrow
82
82
args = a list of string arguments that will be processed
83
83
84
84
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).
87
86
*/
88
- auto extractCommandNameArgument ( string [] args)
87
+ string commandNameArgument ( ref string [] args)
89
88
{
90
- struct Result {
91
- string value;
92
- string [] remaining;
93
- }
94
-
95
89
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;
97
93
}
98
-
99
- return Result (null , args);
94
+ return null ;
100
95
}
101
96
102
97
// / test extractCommandNameArgument usage
103
98
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
+ }
110
105
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
+ }
113
113
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
+ }
116
120
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
+ }
119
127
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
+ }
123
137
}
124
138
125
139
/* * Handles the Command Line options and commands.
@@ -464,14 +478,12 @@ int runDubCommandLine(string[] args)
464
478
465
479
// extract the command
466
480
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);
471
483
Command cmd;
472
484
473
485
try {
474
- cmd = handler.prepareCommand(command_name_argument.value , command_args);
486
+ cmd = handler.prepareCommand(command_name , command_args);
475
487
} catch (Exception e) {
476
488
logError(" Error processing arguments: %s" , e.msg);
477
489
logDiagnostic(" Full exception: %s" , e.toString().sanitize);
@@ -482,14 +494,14 @@ int runDubCommandLine(string[] args)
482
494
if (cmd is null ) {
483
495
logInfoNoTag(" USAGE: dub [--version] [<command>] [<options...>] [-- [<application arguments...>]]" );
484
496
logInfoNoTag(" " );
485
- logError(" Unknown command: %s" , command_name_argument.value );
497
+ logError(" Unknown command: %s" , command_name );
486
498
import std.algorithm.iteration : filter;
487
499
import std.uni : toUpper;
488
500
foreach (CommandGroup key; handler.commandGroups)
489
501
{
490
502
foreach (Command command; key.commands)
491
503
{
492
- if (levenshteinDistance(command_name_argument.value , command.name) < 4 ) {
504
+ if (levenshteinDistance(command_name , command.name) < 4 ) {
493
505
logInfo(" Did you mean '%s'?" , command.name);
494
506
}
495
507
}
0 commit comments