| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library args.src.arg_parser; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'arg_results.dart'; | |
| 10 import 'option.dart'; | |
| 11 import 'parser.dart'; | |
| 12 import 'usage.dart'; | |
| 13 | |
| 14 /// A class for taking a list of raw command line arguments and parsing out | |
| 15 /// options and flags from them. | |
| 16 class ArgParser { | |
| 17 final Map<String, Option> _options; | |
| 18 final Map<String, ArgParser> _commands; | |
| 19 | |
| 20 /// The options that have been defined for this parser. | |
| 21 final Map<String, Option> options; | |
| 22 | |
| 23 /// The commands that have been defined for this parser. | |
| 24 final Map<String, ArgParser> commands; | |
| 25 | |
| 26 /// A list of the [Option]s in [options] intermingled with [String] | |
| 27 /// separators. | |
| 28 final _optionsAndSeparators = []; | |
| 29 | |
| 30 /// Whether or not this parser parses options that appear after non-option | |
| 31 /// arguments. | |
| 32 final bool allowTrailingOptions; | |
| 33 | |
| 34 /// Creates a new ArgParser. | |
| 35 /// | |
| 36 /// If [allowTrailingOptions] is set, the parser will continue parsing even | |
| 37 /// after it finds an argument that is neither an option nor a command. | |
| 38 /// This allows options to be specified after regular arguments. Defaults to | |
| 39 /// `false`. | |
| 40 factory ArgParser({bool allowTrailingOptions}) => new ArgParser._( | |
| 41 <String, Option>{}, <String, ArgParser>{}, | |
| 42 allowTrailingOptions: allowTrailingOptions); | |
| 43 | |
| 44 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, | |
| 45 {bool allowTrailingOptions}) | |
| 46 : this._options = options, | |
| 47 this.options = new UnmodifiableMapView(options), | |
| 48 this._commands = commands, | |
| 49 this.commands = new UnmodifiableMapView(commands), | |
| 50 this.allowTrailingOptions = allowTrailingOptions != null | |
| 51 ? allowTrailingOptions | |
| 52 : false; | |
| 53 | |
| 54 /// Defines a command. | |
| 55 /// | |
| 56 /// A command is a named argument which may in turn define its own options and | |
| 57 /// subcommands using the given parser. If [parser] is omitted, implicitly | |
| 58 /// creates a new one. Returns the parser for the command. | |
| 59 ArgParser addCommand(String name, [ArgParser parser]) { | |
| 60 // Make sure the name isn't in use. | |
| 61 if (_commands.containsKey(name)) { | |
| 62 throw new ArgumentError('Duplicate command "$name".'); | |
| 63 } | |
| 64 | |
| 65 if (parser == null) parser = new ArgParser(); | |
| 66 _commands[name] = parser; | |
| 67 return parser; | |
| 68 } | |
| 69 | |
| 70 /// Defines a flag. Throws an [ArgumentError] if: | |
| 71 /// | |
| 72 /// * There is already an option named [name]. | |
| 73 /// * There is already an option using abbreviation [abbr]. | |
| 74 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | |
| 75 bool negatable: true, void callback(bool value), bool hide: false}) { | |
| 76 _addOption(name, abbr, help, null, null, null, defaultsTo, callback, | |
| 77 OptionType.FLAG, negatable: negatable, hide: hide); | |
| 78 } | |
| 79 | |
| 80 /// Defines a value-taking option. Throws an [ArgumentError] if: | |
| 81 /// | |
| 82 /// * There is already an option with name [name]. | |
| 83 /// * There is already an option using abbreviation [abbr]. | |
| 84 /// * [splitCommas] is passed but [allowMultiple] is `false`. | |
| 85 void addOption(String name, {String abbr, String help, String valueHelp, | |
| 86 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, | |
| 87 void callback(value), bool allowMultiple: false, bool splitCommas, | |
| 88 bool hide: false}) { | |
| 89 if (!allowMultiple && splitCommas != null) { | |
| 90 throw new ArgumentError( | |
| 91 'splitCommas may not be set if allowMultiple is false.'); | |
| 92 } | |
| 93 | |
| 94 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, | |
| 95 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE, | |
| 96 splitCommas: splitCommas, hide: hide); | |
| 97 } | |
| 98 | |
| 99 void _addOption(String name, String abbr, String help, String valueHelp, | |
| 100 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, | |
| 101 void callback(value), OptionType type, | |
| 102 {bool negatable: false, bool splitCommas, bool hide: false}) { | |
| 103 // Make sure the name isn't in use. | |
| 104 if (_options.containsKey(name)) { | |
| 105 throw new ArgumentError('Duplicate option "$name".'); | |
| 106 } | |
| 107 | |
| 108 // Make sure the abbreviation isn't too long or in use. | |
| 109 if (abbr != null) { | |
| 110 var existing = findByAbbreviation(abbr); | |
| 111 if (existing != null) { | |
| 112 throw new ArgumentError( | |
| 113 'Abbreviation "$abbr" is already used by "${existing.name}".'); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 var option = newOption(name, abbr, help, valueHelp, allowed, | |
| 118 allowedHelp, defaultsTo, callback, type, | |
| 119 negatable: negatable, splitCommas: splitCommas, hide: hide); | |
| 120 _options[name] = option; | |
| 121 _optionsAndSeparators.add(option); | |
| 122 } | |
| 123 | |
| 124 /// Adds a separator line to the usage. | |
| 125 /// | |
| 126 /// In the usage text for the parser, this will appear between any options | |
| 127 /// added before this call and ones added after it. | |
| 128 void addSeparator(String text) { | |
| 129 _optionsAndSeparators.add(text); | |
| 130 } | |
| 131 | |
| 132 /// Parses [args], a list of command-line arguments, matches them against the | |
| 133 /// flags and options defined by this parser, and returns the result. | |
| 134 ArgResults parse(List<String> args) => | |
| 135 new Parser(null, this, args.toList(), null, null).parse(); | |
| 136 | |
| 137 /// Generates a string displaying usage information for the defined options. | |
| 138 /// | |
| 139 /// This is basically the help text shown on the command line. | |
| 140 @Deprecated("Replaced with get usage. getUsage() will be removed in args 1.0") | |
| 141 String getUsage() => usage; | |
| 142 | |
| 143 /// Generates a string displaying usage information for the defined options. | |
| 144 /// | |
| 145 /// This is basically the help text shown on the command line. | |
| 146 String get usage => new Usage(_optionsAndSeparators).generate(); | |
| 147 | |
| 148 /// Get the default value for an option. Useful after parsing to test if the | |
| 149 /// user specified something other than the default. | |
| 150 getDefault(String option) { | |
| 151 if (!options.containsKey(option)) { | |
| 152 throw new ArgumentError('No option named $option'); | |
| 153 } | |
| 154 return options[option].defaultValue; | |
| 155 } | |
| 156 | |
| 157 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | |
| 158 /// that abbreviation. | |
| 159 Option findByAbbreviation(String abbr) { | |
| 160 return options.values.firstWhere((option) => option.abbreviation == abbr, | |
| 161 orElse: () => null); | |
| 162 } | |
| 163 } | |
| OLD | NEW |