OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library trailing_options_test; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'package:args/args.dart'; | |
9 | |
10 void main() { | |
11 test('allowTrailingOptions defaults to false', () { | |
12 var parser = new ArgParser(); | |
13 expect(parser.allowTrailingOptions, isFalse); | |
14 }); | |
15 | |
16 group('when trailing options are allowed', () { | |
17 var parser; | |
18 setUp(() { | |
19 parser = new ArgParser(allowTrailingOptions: true); | |
20 }); | |
21 | |
22 void expectThrows(List<String> args) => | |
23 expect(() => parser.parse(args), throwsFormatException, | |
24 reason: "with allowTrailingOptions: true"); | |
25 | |
26 test('collects non-options in rest', () { | |
27 parser.addFlag('flag'); | |
28 parser.addOption('opt', abbr: 'o'); | |
29 var results = parser.parse(['a', '--flag', 'b', '-o', 'value', 'c']); | |
30 expect(results['flag'], isTrue); | |
31 expect(results['opt'], equals('value')); | |
32 expect(results.rest, equals(['a', 'b', 'c'])); | |
33 }); | |
34 | |
35 test('stops parsing options at "--"', () { | |
36 parser.addFlag('flag'); | |
37 parser.addOption('opt', abbr: 'o'); | |
38 var results = parser.parse(['a', '--flag', '--', '-ovalue', 'c']); | |
39 expect(results['flag'], isTrue); | |
40 expect(results.rest, equals(['a', '-ovalue', 'c'])); | |
41 }); | |
42 | |
43 test('only consumes first "--"', () { | |
44 parser.addFlag('flag', abbr: 'f'); | |
45 parser.addOption('opt', abbr: 'o'); | |
46 var results = parser.parse(['a', '--', 'b', '--', 'c']); | |
47 expect(results.rest, equals(['a', 'b', '--', 'c'])); | |
48 }); | |
49 | |
50 test('parses a trailing flag', () { | |
51 parser.addFlag('flag'); | |
52 var results = parser.parse(['arg', '--flag']); | |
53 | |
54 expect(results['flag'], isTrue); | |
55 expect(results.rest, equals(['arg'])); | |
56 }); | |
57 | |
58 test('throws on a trailing option missing its value', () { | |
59 parser.addOption('opt'); | |
60 expectThrows(['arg', '--opt']); | |
61 }); | |
62 | |
63 test('parses a trailing option', () { | |
64 parser.addOption('opt'); | |
65 var results = parser.parse(['arg', '--opt', 'v']); | |
66 expect(results['opt'], equals('v')); | |
67 expect(results.rest, equals(['arg'])); | |
68 }); | |
69 | |
70 test('throws on a trailing unknown flag', () { | |
71 expectThrows(['arg', '--xflag']); | |
72 }); | |
73 | |
74 test('throws on a trailing unknown option and value', () { | |
75 expectThrows(['arg', '--xopt', 'v']); | |
76 }); | |
77 | |
78 test('throws on a command', () { | |
79 parser.addCommand('com'); | |
80 expectThrows(['arg', 'com']); | |
81 }); | |
82 }); | |
83 | |
84 test("uses the innermost command's trailing options behavior", () { | |
85 var parser = new ArgParser(allowTrailingOptions: true); | |
86 parser.addFlag('flag', abbr: 'f'); | |
87 var command = parser.addCommand('cmd', new ArgParser(allowTrailingOptions: f alse)); | |
nweiz
2014/05/05 19:52:45
long line
Bob Nystrom
2014/05/05 22:36:02
Done.
| |
88 command.addFlag('verbose', abbr: 'v'); | |
89 | |
90 var results = parser.parse(['a', '-f', 'b']); | |
91 expect(results['flag'], isTrue); | |
92 expect(results.rest, equals(['a', 'b'])); | |
93 | |
94 results = parser.parse(['cmd', '-f', 'a', '-v', '--unknown']); | |
95 expect(results['flag'], isTrue); // Not trailing. | |
96 expect(results.command['verbose'], isFalse); | |
97 expect(results.command.rest, equals(['a', '-v', '--unknown'])); | |
98 }); | |
99 } | |
OLD | NEW |