OLD | NEW |
1 // Copyright (c) 2014, 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 args.src.arg_parser; | 5 library args.src.arg_parser; |
6 | 6 |
7 import 'package:collection/wrappers.dart'; | 7 import 'package:collection/wrappers.dart'; |
8 | 8 |
9 import 'arg_results.dart'; | 9 import 'arg_results.dart'; |
10 import 'option.dart'; | 10 import 'option.dart'; |
(...skipping 15 matching lines...) Expand all Loading... |
26 /// Whether or not this parser parses options that appear after non-option | 26 /// Whether or not this parser parses options that appear after non-option |
27 /// arguments. | 27 /// arguments. |
28 final bool allowTrailingOptions; | 28 final bool allowTrailingOptions; |
29 | 29 |
30 /// Creates a new ArgParser. | 30 /// Creates a new ArgParser. |
31 /// | 31 /// |
32 /// If [allowTrailingOptions] is set, the parser will continue parsing even | 32 /// If [allowTrailingOptions] is set, the parser will continue parsing even |
33 /// after it finds an argument that is neither an option nor a command. | 33 /// after it finds an argument that is neither an option nor a command. |
34 /// This allows options to be specified after regular arguments. Defaults to | 34 /// This allows options to be specified after regular arguments. Defaults to |
35 /// `false`. | 35 /// `false`. |
36 factory ArgParser({bool allowTrailingOptions}) => | 36 factory ArgParser({bool allowTrailingOptions}) => new ArgParser._( |
37 new ArgParser._(<String, Option>{}, <String, ArgParser>{}, | 37 <String, Option>{}, <String, ArgParser>{}, |
38 allowTrailingOptions: allowTrailingOptions); | 38 allowTrailingOptions: allowTrailingOptions); |
39 | 39 |
40 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, | 40 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, |
41 {bool allowTrailingOptions}) : | 41 {bool allowTrailingOptions}) |
42 this._options = options, | 42 : this._options = options, |
43 this.options = new UnmodifiableMapView(options), | 43 this.options = new UnmodifiableMapView(options), |
44 this._commands = commands, | 44 this._commands = commands, |
45 this.commands = new UnmodifiableMapView(commands), | 45 this.commands = new UnmodifiableMapView(commands), |
46 this.allowTrailingOptions = allowTrailingOptions != null ? | 46 this.allowTrailingOptions = allowTrailingOptions != null |
47 allowTrailingOptions : false; | 47 ? allowTrailingOptions |
| 48 : false; |
48 | 49 |
49 /// Defines a command. | 50 /// Defines a command. |
50 /// | 51 /// |
51 /// A command is a named argument which may in turn define its own options and | 52 /// A command is a named argument which may in turn define its own options and |
52 /// subcommands using the given parser. If [parser] is omitted, implicitly | 53 /// subcommands using the given parser. If [parser] is omitted, implicitly |
53 /// creates a new one. Returns the parser for the command. | 54 /// creates a new one. Returns the parser for the command. |
54 ArgParser addCommand(String name, [ArgParser parser]) { | 55 ArgParser addCommand(String name, [ArgParser parser]) { |
55 // Make sure the name isn't in use. | 56 // Make sure the name isn't in use. |
56 if (_commands.containsKey(name)) { | 57 if (_commands.containsKey(name)) { |
57 throw new ArgumentError('Duplicate command "$name".'); | 58 throw new ArgumentError('Duplicate command "$name".'); |
(...skipping 21 matching lines...) Expand all Loading... |
79 void addOption(String name, {String abbr, String help, String valueHelp, | 80 void addOption(String name, {String abbr, String help, String valueHelp, |
80 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, | 81 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, |
81 void callback(value), bool allowMultiple: false, bool hide: false}) { | 82 void callback(value), bool allowMultiple: false, bool hide: false}) { |
82 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, | 83 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, |
83 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE, | 84 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE, |
84 hide: hide); | 85 hide: hide); |
85 } | 86 } |
86 | 87 |
87 void _addOption(String name, String abbr, String help, String valueHelp, | 88 void _addOption(String name, String abbr, String help, String valueHelp, |
88 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, | 89 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, |
89 void callback(value), OptionType type, {bool negatable: false, | 90 void callback(value), OptionType type, |
90 bool hide: false}) { | 91 {bool negatable: false, bool hide: false}) { |
91 // Make sure the name isn't in use. | 92 // Make sure the name isn't in use. |
92 if (_options.containsKey(name)) { | 93 if (_options.containsKey(name)) { |
93 throw new ArgumentError('Duplicate option "$name".'); | 94 throw new ArgumentError('Duplicate option "$name".'); |
94 } | 95 } |
95 | 96 |
96 // Make sure the abbreviation isn't too long or in use. | 97 // Make sure the abbreviation isn't too long or in use. |
97 if (abbr != null) { | 98 if (abbr != null) { |
98 var existing = findByAbbreviation(abbr); | 99 var existing = findByAbbreviation(abbr); |
99 if (existing != null) { | 100 if (existing != null) { |
100 throw new ArgumentError( | 101 throw new ArgumentError( |
101 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 102 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
102 } | 103 } |
103 } | 104 } |
104 | 105 |
105 _options[name] = newOption(name, abbr, help, valueHelp, allowed, | 106 _options[name] = newOption(name, abbr, help, valueHelp, allowed, |
106 allowedHelp, defaultsTo, callback, type, negatable: negatable, | 107 allowedHelp, defaultsTo, callback, type, |
107 hide: hide); | 108 negatable: negatable, hide: hide); |
108 } | 109 } |
109 | 110 |
110 /// Parses [args], a list of command-line arguments, matches them against the | 111 /// Parses [args], a list of command-line arguments, matches them against the |
111 /// flags and options defined by this parser, and returns the result. | 112 /// flags and options defined by this parser, and returns the result. |
112 ArgResults parse(List<String> args) => | 113 ArgResults parse(List<String> args) => |
113 new Parser(null, this, args.toList(), null, null).parse(); | 114 new Parser(null, this, args.toList(), null, null).parse(); |
114 | 115 |
115 /// Generates a string displaying usage information for the defined options. | 116 /// Generates a string displaying usage information for the defined options. |
116 /// | 117 /// |
117 /// This is basically the help text shown on the command line. | 118 /// This is basically the help text shown on the command line. |
(...skipping 14 matching lines...) Expand all Loading... |
132 return options[option].defaultValue; | 133 return options[option].defaultValue; |
133 } | 134 } |
134 | 135 |
135 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | 136 /// Finds the option whose abbreviation is [abbr], or `null` if no option has |
136 /// that abbreviation. | 137 /// that abbreviation. |
137 Option findByAbbreviation(String abbr) { | 138 Option findByAbbreviation(String abbr) { |
138 return options.values.firstWhere((option) => option.abbreviation == abbr, | 139 return options.values.firstWhere((option) => option.abbreviation == abbr, |
139 orElse: () => null); | 140 orElse: () => null); |
140 } | 141 } |
141 } | 142 } |
OLD | NEW |