| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library command_test; | 5 library command_test; |
| 6 | 6 |
| 7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
| 9 import 'utils.dart'; | 9 import 'utils.dart'; |
| 10 | 10 |
| 11 void main() { | 11 void main() { |
| 12 group('ArgParser.addCommand()', () { | 12 var foo; |
| 13 test('creates a new ArgParser if none is given', () { | 13 setUp(() { |
| 14 var parser = new ArgParser(); | 14 foo = new FooCommand(); |
| 15 var command = parser.addCommand('install'); | 15 |
| 16 expect(parser.commands['install'], equals(command)); | 16 // Make sure [Command.runner] is set up. |
| 17 expect(command is ArgParser, isTrue); | 17 new CommandRunner("test", "A test command runner.").addCommand(foo); |
| 18 }); |
| 19 |
| 20 group(".usageTemplate has a sane default", () { |
| 21 test("without subcommands", () { |
| 22 expect(foo.usageTemplate, |
| 23 equals("test foo <arguments>")); |
| 18 }); | 24 }); |
| 19 | 25 |
| 20 test('uses the command parser if given one', () { | 26 test("with subcommands", () { |
| 21 var parser = new ArgParser(); | 27 foo.addSubcommand(new AsyncCommand()); |
| 22 var command = new ArgParser(); | 28 expect(foo.usageTemplate, |
| 23 var result = parser.addCommand('install', command); | 29 equals("test foo [subcommand] <arguments>")); |
| 24 expect(parser.commands['install'], equals(command)); | |
| 25 expect(result, equals(command)); | |
| 26 }); | 30 }); |
| 27 | 31 |
| 28 test('throws on a duplicate command name', () { | 32 test("for a subcommand", () { |
| 29 var parser = new ArgParser(); | 33 var async = new AsyncCommand(); |
| 30 parser.addCommand('install'); | 34 foo.addSubcommand(async); |
| 31 throwsIllegalArg(() => parser.addCommand('install')); | 35 |
| 36 expect(async.usageTemplate, |
| 37 equals("test foo async <arguments>")); |
| 32 }); | 38 }); |
| 33 }); | 39 }); |
| 34 | 40 |
| 35 group('ArgParser.parse()', () { | 41 group(".usage", () { |
| 36 test('parses a command', () { | 42 test("returns the usage string", () { |
| 37 var parser = new ArgParser(); | 43 expect(foo.usage, equals(""" |
| 38 var command = parser.addCommand('install'); | 44 Set a value. |
| 39 | 45 |
| 40 var args = parser.parse(['install']); | 46 Usage: test foo <arguments> |
| 47 -h, --help Print this usage information. |
| 41 | 48 |
| 42 expect(args.command.name, equals('install')); | 49 Run "test help" to see global options.""")); |
| 43 expect(args.rest, isEmpty); | |
| 44 }); | 50 }); |
| 45 | 51 |
| 46 test('parses a command option', () { | 52 test("contains custom options", () { |
| 47 var parser = new ArgParser(); | 53 foo.argParser.addFlag("flag", help: "Do something."); |
| 48 var command = parser.addCommand('install'); | |
| 49 command.addOption('path'); | |
| 50 | 54 |
| 51 var args = parser.parse(['install', '--path', 'some/path']); | 55 expect(foo.usage, equals(""" |
| 52 expect(args.command['path'], equals('some/path')); | 56 Set a value. |
| 57 |
| 58 Usage: test foo <arguments> |
| 59 -h, --help Print this usage information. |
| 60 --[no-]flag Do something. |
| 61 |
| 62 Run "test help" to see global options.""")); |
| 53 }); | 63 }); |
| 54 | 64 |
| 55 test('parses a parent solo option before the command', () { | 65 test("doesn't print hidden subcommands", () { |
| 56 var parser = new ArgParser(); | 66 foo.addSubcommand(new AsyncCommand()); |
| 57 parser.addOption('mode', abbr: 'm'); | 67 foo.addSubcommand(new HiddenCommand()); |
| 58 var command = parser.addCommand('install'); | |
| 59 | 68 |
| 60 var args = parser.parse(['-m', 'debug', 'install']); | 69 expect(foo.usage, equals(""" |
| 61 expect(args['mode'], equals('debug')); | 70 Set a value. |
| 62 expect(args.command.name, equals('install')); | 71 |
| 72 Usage: test foo [subcommand] <arguments> |
| 73 -h, --help Print this usage information. |
| 74 |
| 75 Available subcommands: |
| 76 async Set a value asynchronously. |
| 77 |
| 78 Run "test help" to see global options.""")); |
| 63 }); | 79 }); |
| 64 | 80 |
| 65 test('parses a parent solo option after the command', () { | 81 test("doesn't print subcommand aliases", () { |
| 66 var parser = new ArgParser(); | 82 foo.addSubcommand(new AliasedCommand()); |
| 67 parser.addOption('mode', abbr: 'm'); | |
| 68 var command = parser.addCommand('install'); | |
| 69 | 83 |
| 70 var args = parser.parse(['install', '-m', 'debug']); | 84 expect(foo.usage, equals(""" |
| 71 expect(args['mode'], equals('debug')); | 85 Set a value. |
| 72 expect(args.command.name, equals('install')); | |
| 73 }); | |
| 74 | 86 |
| 75 test('parses a parent option before the command', () { | 87 Usage: test foo [subcommand] <arguments> |
| 76 var parser = new ArgParser(); | 88 -h, --help Print this usage information. |
| 77 parser.addFlag('verbose'); | |
| 78 var command = parser.addCommand('install'); | |
| 79 | 89 |
| 80 var args = parser.parse(['--verbose', 'install']); | 90 Available subcommands: |
| 81 expect(args['verbose'], isTrue); | 91 aliased Set a value. |
| 82 expect(args.command.name, equals('install')); | |
| 83 }); | |
| 84 | 92 |
| 85 test('parses a parent option after the command', () { | 93 Run "test help" to see global options.""")); |
| 86 var parser = new ArgParser(); | |
| 87 parser.addFlag('verbose'); | |
| 88 var command = parser.addCommand('install'); | |
| 89 | |
| 90 var args = parser.parse(['install', '--verbose']); | |
| 91 expect(args['verbose'], isTrue); | |
| 92 expect(args.command.name, equals('install')); | |
| 93 }); | |
| 94 | |
| 95 test('parses a parent negated option before the command', () { | |
| 96 var parser = new ArgParser(); | |
| 97 parser.addFlag('verbose', defaultsTo: true); | |
| 98 var command = parser.addCommand('install'); | |
| 99 | |
| 100 var args = parser.parse(['--no-verbose', 'install']); | |
| 101 expect(args['verbose'], isFalse); | |
| 102 expect(args.command.name, equals('install')); | |
| 103 }); | |
| 104 | |
| 105 test('parses a parent negated option after the command', () { | |
| 106 var parser = new ArgParser(); | |
| 107 parser.addFlag('verbose', defaultsTo: true); | |
| 108 var command = parser.addCommand('install'); | |
| 109 | |
| 110 var args = parser.parse(['install', '--no-verbose']); | |
| 111 expect(args['verbose'], isFalse); | |
| 112 expect(args.command.name, equals('install')); | |
| 113 }); | |
| 114 | |
| 115 test('parses a parent abbreviation before the command', () { | |
| 116 var parser = new ArgParser(); | |
| 117 parser.addFlag('debug', abbr: 'd'); | |
| 118 parser.addFlag('verbose', abbr: 'v'); | |
| 119 var command = parser.addCommand('install'); | |
| 120 | |
| 121 var args = parser.parse(['-dv', 'install']); | |
| 122 expect(args['debug'], isTrue); | |
| 123 expect(args['verbose'], isTrue); | |
| 124 expect(args.command.name, equals('install')); | |
| 125 }); | |
| 126 | |
| 127 test('parses a parent abbreviation after the command', () { | |
| 128 var parser = new ArgParser(); | |
| 129 parser.addFlag('debug', abbr: 'd'); | |
| 130 parser.addFlag('verbose', abbr: 'v'); | |
| 131 var command = parser.addCommand('install'); | |
| 132 | |
| 133 var args = parser.parse(['install', '-dv']); | |
| 134 expect(args['debug'], isTrue); | |
| 135 expect(args['verbose'], isTrue); | |
| 136 expect(args.command.name, equals('install')); | |
| 137 }); | |
| 138 | |
| 139 test('does not parse a solo command option before the command', () { | |
| 140 var parser = new ArgParser(); | |
| 141 var command = parser.addCommand('install'); | |
| 142 command.addOption('path', abbr: 'p'); | |
| 143 | |
| 144 throwsFormat(parser, ['-p', 'foo', 'install']); | |
| 145 }); | |
| 146 | |
| 147 test('does not parse a command option before the command', () { | |
| 148 var parser = new ArgParser(); | |
| 149 var command = parser.addCommand('install'); | |
| 150 command.addOption('path'); | |
| 151 | |
| 152 throwsFormat(parser, ['--path', 'foo', 'install']); | |
| 153 }); | |
| 154 | |
| 155 test('does not parse a command abbreviation before the command', () { | |
| 156 var parser = new ArgParser(); | |
| 157 var command = parser.addCommand('install'); | |
| 158 command.addFlag('debug', abbr: 'd'); | |
| 159 command.addFlag('verbose', abbr: 'v'); | |
| 160 | |
| 161 throwsFormat(parser, ['-dv', 'install']); | |
| 162 }); | |
| 163 | |
| 164 test('assigns collapsed options to the proper command', () { | |
| 165 var parser = new ArgParser(); | |
| 166 parser.addFlag('apple', abbr: 'a'); | |
| 167 var command = parser.addCommand('cmd'); | |
| 168 command.addFlag('banana', abbr: 'b'); | |
| 169 var subcommand = command.addCommand('subcmd'); | |
| 170 subcommand.addFlag('cherry', abbr: 'c'); | |
| 171 | |
| 172 var args = parser.parse(['cmd', 'subcmd', '-abc']); | |
| 173 expect(args['apple'], isTrue); | |
| 174 expect(args.command.name, equals('cmd')); | |
| 175 expect(args.command['banana'], isTrue); | |
| 176 expect(args.command.command.name, equals('subcmd')); | |
| 177 expect(args.command.command['cherry'], isTrue); | |
| 178 }); | |
| 179 | |
| 180 test('option is given to innermost command that can take it', () { | |
| 181 var parser = new ArgParser(); | |
| 182 parser.addFlag('verbose'); | |
| 183 var command = parser.addCommand('cmd'); | |
| 184 command.addFlag('verbose'); | |
| 185 var subcommand = command.addCommand('subcmd'); | |
| 186 | |
| 187 var args = parser.parse(['cmd', 'subcmd', '--verbose']); | |
| 188 expect(args['verbose'], isFalse); | |
| 189 expect(args.command.name, equals('cmd')); | |
| 190 expect(args.command['verbose'], isTrue); | |
| 191 expect(args.command.command.name, equals('subcmd')); | |
| 192 }); | |
| 193 | |
| 194 test('remaining arguments are given to the innermost command', () { | |
| 195 var parser = new ArgParser(); | |
| 196 var command = parser.addCommand('cmd'); | |
| 197 var subcommand = command.addCommand('subcmd'); | |
| 198 | |
| 199 var args = parser.parse(['cmd', 'subcmd', 'other', 'stuff']); | |
| 200 expect(args.command.name, equals('cmd')); | |
| 201 expect(args.rest, isEmpty); | |
| 202 expect(args.command.command.name, equals('subcmd')); | |
| 203 expect(args.command.rest, isEmpty); | |
| 204 expect(args.command.command.rest, equals(['other', 'stuff'])); | |
| 205 }); | 94 }); |
| 206 }); | 95 }); |
| 96 |
| 97 test("usageError splits up the message and usage", () { |
| 98 expect(() => foo.usageError("message"), throwsUsageError("message", """ |
| 99 Usage: test foo <arguments> |
| 100 -h, --help Print this usage information. |
| 101 |
| 102 Run "test help" to see global options.""")); |
| 103 }); |
| 104 |
| 105 test("considers a command hidden if all its subcommands are hidden", () { |
| 106 foo.addSubcommand(new HiddenCommand()); |
| 107 expect(foo.hidden, isTrue); |
| 108 }); |
| 207 } | 109 } |
| OLD | NEW |