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_results; |
| 6 |
| 7 import 'package:collection/wrappers.dart'; |
| 8 |
| 9 import 'arg_parser.dart'; |
| 10 |
| 11 /// Creates a new [ArgResults]. |
| 12 /// |
| 13 /// Since [ArgResults] doesn't have a public constructor, this lets [Parser] |
| 14 /// get to it. This function isn't exported to the public API of the package. |
| 15 ArgResults newArgResults(ArgParser parser, Map<String, dynamic> parsed, |
| 16 String name, ArgResults command, List<String> rest) { |
| 17 return new ArgResults._(parser, parsed, name, command, rest); |
| 18 } |
| 19 |
| 20 /// The results of parsing a series of command line arguments using |
| 21 /// [ArgParser.parse()]. |
| 22 /// |
| 23 /// Includes the parsed options and any remaining unparsed command line |
| 24 /// arguments. |
| 25 class ArgResults { |
| 26 /// The [ArgParser] whose options were parsed for these results. |
| 27 final ArgParser _parser; |
| 28 |
| 29 /// The option values that were parsed from arguments. |
| 30 final Map<String, dynamic> _parsed; |
| 31 |
| 32 /// If these are the results for parsing a command's options, this will be the |
| 33 /// name of the command. For top-level results, this returns `null`. |
| 34 final String name; |
| 35 |
| 36 /// The command that was selected, or `null` if none was. |
| 37 /// |
| 38 /// This will contain the options that were selected for that command. |
| 39 final ArgResults command; |
| 40 |
| 41 /// The remaining command-line arguments that were not parsed as options or |
| 42 /// flags. |
| 43 /// |
| 44 /// If `--` was used to separate the options from the remaining arguments, |
| 45 /// it will not be included in this list unless parsing stopped before the |
| 46 /// `--` was reached. |
| 47 final List<String> rest; |
| 48 |
| 49 /// Creates a new [ArgResults]. |
| 50 ArgResults._(this._parser, this._parsed, this.name, this.command, |
| 51 List<String> rest) |
| 52 : this.rest = new UnmodifiableListView(rest); |
| 53 |
| 54 /// Gets the parsed command-line option named [name]. |
| 55 operator [](String name) { |
| 56 if (!_parser.options.containsKey(name)) { |
| 57 throw new ArgumentError('Could not find an option named "$name".'); |
| 58 } |
| 59 |
| 60 return _parser.options[name].getOrDefault(_parsed[name]); |
| 61 } |
| 62 |
| 63 /// Get the names of the available options as an [Iterable]. |
| 64 /// |
| 65 /// This includes the options whose values were parsed or that have defaults. |
| 66 /// Options that weren't present and have no default will be omitted. |
| 67 Iterable<String> get options { |
| 68 var result = new Set.from(_parsed.keys); |
| 69 |
| 70 // Include the options that have defaults. |
| 71 _parser.options.forEach((name, option) { |
| 72 if (option.defaultValue != null) result.add(name); |
| 73 }); |
| 74 |
| 75 return result; |
| 76 } |
| 77 |
| 78 /// Returns `true` if the option with [name] was parsed from an actual |
| 79 /// argument. |
| 80 /// |
| 81 /// Returns `false` if it wasn't provided and the default value or no default |
| 82 /// value would be used instead. |
| 83 bool wasParsed(String name) { |
| 84 var option = _parser.options[name]; |
| 85 if (option == null) { |
| 86 throw new ArgumentError('Could not find an option named "$name".'); |
| 87 } |
| 88 |
| 89 return _parsed.containsKey(name); |
| 90 } |
| 91 } |
OLD | NEW |