Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 28a5d2f

Browse files
authored
address an issue when using ArgParser.allowAnything() with CommandRunner (#132)
address an issue when using allowAnything
1 parent 0eed427 commit 28a5d2f

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 1.5.3
22

3-
* Improve performance for large numbers of args.
3+
* Improve arg parsing performance for large numbers of args.
4+
* No longer automatically add a 'help' option to commands that don't validate
5+
their arguments (fix #123).
46

57
## 1.5.2
68

lib/command_runner.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class CommandRunner<T> {
177177
commands = command._subcommands;
178178
commandString += ' ${argResults.name}';
179179

180-
if (argResults['help']) {
180+
if (argResults.options.contains('help') && argResults['help']) {
181181
command.printUsage();
182182
return null;
183183
}
@@ -364,8 +364,10 @@ abstract class Command<T> {
364364
List<String> get aliases => const [];
365365

366366
Command() {
367-
argParser.addFlag('help',
368-
abbr: 'h', negatable: false, help: 'Print this usage information.');
367+
if (!argParser.allowsAnything) {
368+
argParser.addFlag('help',
369+
abbr: 'h', negatable: false, help: 'Print this usage information.');
370+
}
369371
}
370372

371373
/// Runs this command.

test/allow_anything_test.dart

+11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:args/args.dart';
6+
import 'package:args/command_runner.dart';
67
import 'package:test/test.dart';
78

9+
import 'test_utils.dart';
10+
811
void main() {
912
group('new ArgParser.allowAnything()', () {
1013
ArgParser parser;
@@ -51,5 +54,13 @@ void main() {
5154
expect(results.command.arguments, equals(['--foo', '-abc', '--', 'bar']));
5255
expect(results.command.name, equals('command'));
5356
});
57+
58+
test('works as a subcommand in a CommandRunner', () async {
59+
var commandRunner = CommandRunner('command', 'Description of command');
60+
var command = AllowAnythingCommand();
61+
commandRunner..addCommand(command);
62+
63+
await commandRunner.run([command.name, '--foo', '--bar', '-b', 'qux']);
64+
});
5465
});
5566
}

test/test_utils.dart

+18
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ class AsyncCommand extends Command {
207207
Future run() => Future.value().then((_) => hasRun = true);
208208
}
209209

210+
class AllowAnythingCommand extends Command {
211+
var hasRun = false;
212+
213+
@override
214+
final name = 'allowAnything';
215+
216+
@override
217+
final description = 'A command using allowAnything.';
218+
219+
@override
220+
final argParser = ArgParser.allowAnything();
221+
222+
@override
223+
void run() {
224+
hasRun = true;
225+
}
226+
}
227+
210228
void throwsIllegalArg(function, {String reason}) {
211229
expect(function, throwsArgumentError, reason: reason);
212230
}

0 commit comments

Comments
 (0)