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

Commit

Permalink
address an issue when using ArgParser.allowAnything() with CommandRun…
Browse files Browse the repository at this point in the history
…ner (#132)

address an issue when using allowAnything
  • Loading branch information
devoncarew authored Feb 26, 2020
1 parent 0eed427 commit 28a5d2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 5 additions & 3 deletions lib/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class CommandRunner<T> {
commands = command._subcommands;
commandString += ' ${argResults.name}';

if (argResults['help']) {
if (argResults.options.contains('help') && argResults['help']) {
command.printUsage();
return null;
}
Expand Down Expand Up @@ -364,8 +364,10 @@ abstract class Command<T> {
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.
Expand Down
11 changes: 11 additions & 0 deletions test/allow_anything_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,5 +54,13 @@ void main() {
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']);
});
});
}
18 changes: 18 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ class AsyncCommand extends Command {
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);
}
Expand Down

0 comments on commit 28a5d2f

Please sign in to comment.