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', |
| 88 new ArgParser(allowTrailingOptions: false)); |
| 89 command.addFlag('verbose', abbr: 'v'); |
| 90 |
| 91 var results = parser.parse(['a', '-f', 'b']); |
| 92 expect(results['flag'], isTrue); |
| 93 expect(results.rest, equals(['a', 'b'])); |
| 94 |
| 95 results = parser.parse(['cmd', '-f', 'a', '-v', '--unknown']); |
| 96 expect(results['flag'], isTrue); // Not trailing. |
| 97 expect(results.command['verbose'], isFalse); |
| 98 expect(results.command.rest, equals(['a', '-v', '--unknown'])); |
| 99 }); |
| 100 } |
OLD | NEW |