address an issue when using ArgParser.allowAnything() with CommandRunner (#132)
address an issue when using allowAnything
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7eac77d..b2f4c3e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,8 @@
## 1.5.3
-* Improve performance for large numbers of args.
+* Improve arg parsing performance for large numbers of args.
+* No longer automatically add a 'help' option to commands that don't validate
+ their arguments (fix #123).
## 1.5.2
diff --git a/lib/command_runner.dart b/lib/command_runner.dart
index c314abe..0fbf697 100644
--- a/lib/command_runner.dart
+++ b/lib/command_runner.dart
@@ -177,7 +177,7 @@
commands = command._subcommands;
commandString += ' ${argResults.name}';
- if (argResults['help']) {
+ if (argResults.options.contains('help') && argResults['help']) {
command.printUsage();
return null;
}
@@ -364,8 +364,10 @@
List<String> get aliases => const [];
Command() {
- argParser.addFlag('help',
- abbr: 'h', negatable: false, help: 'Print this usage information.');
+ if (!argParser.allowsAnything) {
+ argParser.addFlag('help',
+ abbr: 'h', negatable: false, help: 'Print this usage information.');
+ }
}
/// Runs this command.
diff --git a/test/allow_anything_test.dart b/test/allow_anything_test.dart
index efaf093..29038a7 100644
--- a/test/allow_anything_test.dart
+++ b/test/allow_anything_test.dart
@@ -3,8 +3,11 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:args/args.dart';
+import 'package:args/command_runner.dart';
import 'package:test/test.dart';
+import 'test_utils.dart';
+
void main() {
group('new ArgParser.allowAnything()', () {
ArgParser parser;
@@ -51,5 +54,13 @@
expect(results.command.arguments, equals(['--foo', '-abc', '--', 'bar']));
expect(results.command.name, equals('command'));
});
+
+ test('works as a subcommand in a CommandRunner', () async {
+ var commandRunner = CommandRunner('command', 'Description of command');
+ var command = AllowAnythingCommand();
+ commandRunner..addCommand(command);
+
+ await commandRunner.run([command.name, '--foo', '--bar', '-b', 'qux']);
+ });
});
}
diff --git a/test/test_utils.dart b/test/test_utils.dart
index 0aa566f..8c6373f 100644
--- a/test/test_utils.dart
+++ b/test/test_utils.dart
@@ -207,6 +207,24 @@
Future run() => Future.value().then((_) => hasRun = true);
}
+class AllowAnythingCommand extends Command {
+ var hasRun = false;
+
+ @override
+ final name = 'allowAnything';
+
+ @override
+ final description = 'A command using allowAnything.';
+
+ @override
+ final argParser = ArgParser.allowAnything();
+
+ @override
+ void run() {
+ hasRun = true;
+ }
+}
+
void throwsIllegalArg(function, {String reason}) {
expect(function, throwsArgumentError, reason: reason);
}