OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'package:args/args.dart'; |
| 6 |
| 7 /** |
| 8 * Commandline argument parser. |
| 9 * |
| 10 * TODO(pq): when the args package supports ignoring unrecognized options/flags, |
| 11 * this class can be replaced with a simple [ArgParser] instance. |
| 12 */ |
| 13 class CommandLineParser { |
| 14 static const String IGNORE_UNRECOGNIZED_FLAG = 'ignore-unrecognized-flags'; |
| 15 |
| 16 final List<String> _knownFlags; |
| 17 final bool _alwaysIgnoreUnrecognized; |
| 18 final ArgParser _parser; |
| 19 |
| 20 /// Creates a new command line parser. |
| 21 CommandLineParser({bool alwaysIgnoreUnrecognized: false}) |
| 22 : _knownFlags = <String>[], |
| 23 _alwaysIgnoreUnrecognized = alwaysIgnoreUnrecognized, |
| 24 _parser = new ArgParser(allowTrailingOptions: true) { |
| 25 addFlag(IGNORE_UNRECOGNIZED_FLAG, |
| 26 help: 'Ignore unrecognized command line flags.', |
| 27 defaultsTo: false, |
| 28 negatable: false); |
| 29 } |
| 30 |
| 31 ArgParser get parser => _parser; |
| 32 |
| 33 /// Defines a flag. |
| 34 /// See [ArgParser.addFlag()]. |
| 35 void addFlag(String name, |
| 36 {String abbr, |
| 37 String help, |
| 38 bool defaultsTo: false, |
| 39 bool negatable: true, |
| 40 void callback(bool value), |
| 41 bool hide: false}) { |
| 42 _knownFlags.add(name); |
| 43 _parser.addFlag(name, |
| 44 abbr: abbr, |
| 45 help: help, |
| 46 defaultsTo: defaultsTo, |
| 47 negatable: negatable, |
| 48 callback: callback, |
| 49 hide: hide); |
| 50 } |
| 51 |
| 52 /// Defines a value-taking option. |
| 53 /// See [ArgParser.addOption()]. |
| 54 void addOption(String name, |
| 55 {String abbr, |
| 56 String help, |
| 57 List<String> allowed, |
| 58 Map<String, String> allowedHelp, |
| 59 String defaultsTo, |
| 60 void callback(value), |
| 61 bool allowMultiple: false, |
| 62 bool splitCommas, |
| 63 bool hide: false}) { |
| 64 _knownFlags.add(name); |
| 65 _parser.addOption(name, |
| 66 abbr: abbr, |
| 67 help: help, |
| 68 allowed: allowed, |
| 69 allowedHelp: allowedHelp, |
| 70 defaultsTo: defaultsTo, |
| 71 callback: callback, |
| 72 allowMultiple: allowMultiple, |
| 73 splitCommas: splitCommas, |
| 74 hide: hide); |
| 75 } |
| 76 |
| 77 /// Parses [args], a list of command-line arguments, matches them against the |
| 78 /// flags and options defined by this parser, and returns the result. |
| 79 /// See [ArgParser]. |
| 80 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); |
| 81 |
| 82 List<String> _filterUnknowns(List<String> args) { |
| 83 if (!_alwaysIgnoreUnrecognized && |
| 84 !args.contains('--$IGNORE_UNRECOGNIZED_FLAG')) { |
| 85 return args; |
| 86 } |
| 87 |
| 88 //TODO(pquitslund): replace w/ the following once library skew issues are |
| 89 // sorted out |
| 90 //return args.where((arg) => !arg.startsWith('--') || |
| 91 // _knownFlags.contains(arg.substring(2))); |
| 92 |
| 93 // Filter all unrecognized flags and options. |
| 94 List<String> filtered = <String>[]; |
| 95 for (int i = 0; i < args.length; ++i) { |
| 96 String arg = args[i]; |
| 97 if (arg.startsWith('--') && arg.length > 2) { |
| 98 String option = arg.substring(2); |
| 99 // strip the last '=value' |
| 100 int equalsOffset = option.lastIndexOf('='); |
| 101 if (equalsOffset != -1) { |
| 102 option = option.substring(0, equalsOffset); |
| 103 } |
| 104 // Check the option |
| 105 if (!_knownFlags.contains(option)) { |
| 106 //"eat" params by advancing to the next flag/option |
| 107 i = _getNextFlagIndex(args, i); |
| 108 } else { |
| 109 filtered.add(arg); |
| 110 } |
| 111 } else { |
| 112 filtered.add(arg); |
| 113 } |
| 114 } |
| 115 |
| 116 return filtered; |
| 117 } |
| 118 |
| 119 int _getNextFlagIndex(args, i) { |
| 120 for (; i < args.length; ++i) { |
| 121 if (args[i].startsWith('--')) { |
| 122 return i; |
| 123 } |
| 124 } |
| 125 return i; |
| 126 } |
| 127 } |
OLD | NEW |