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