@@ -10,6 +10,7 @@ import 'package:collection/iterable_zip.dart';
1010import 'package:mockable_filesystem/filesystem.dart' as filesystem;
1111
1212import '../unscripted.dart' ;
13+ import 'script_impl.dart' ;
1314import 'string_codecs.dart' ;
1415import 'usage.dart' ;
1516import 'invocation_maker.dart' ;
@@ -79,9 +80,10 @@ getDefaultPositionalName(Symbol symbol) {
7980 // return MirrorSystem.getName(symbol).toUpperCase();
8081}
8182
82- Usage getUsageFromFunction (MethodMirror methodMirror, {Usage usage}) {
83+ Usage getUsageFromFunction (MethodMirror methodMirror, DeclarationScript script, {Usage usage}) {
8384
8485 if (usage == null ) usage = new Usage ();
86+ script.usageOptionParameterMap[usage] = {};
8587
8688 usage.rest = getRestFromMethod (methodMirror);
8789
@@ -160,23 +162,26 @@ Usage getUsageFromFunction(MethodMirror methodMirror, {Usage usage}) {
160162 defaultValue = parameter.defaultValue.reflectee;
161163 }
162164
165+ var optionName = dashesToCamelCase.decode (option.name != null
166+ ? option.name : parameterName);
167+
163168 // Update option with any configuration detected in the parameter.
164169 // TODO: This is not very maintainable.
165170 // Use reflection instead to copy values over?
166171 option = option is Flag ?
167172 new Flag (help: option.help, abbr: option.abbr, hide: option.hide,
168- defaultsTo: defaultValue, negatable: option.negatable) :
173+ defaultsTo: defaultValue, negatable: option.negatable,
174+ name: optionName) :
169175 new Option (help: option.help, abbr: option.abbr,
170176 defaultsTo: defaultValue, allowed: option.allowed,
171177 allowMultiple: allowMultiple, hide: option.hide,
172- valueHelp: option.valueHelp, parser: parser);
173-
174- var optionName = dashesToCamelCase.decode (parameterName);
178+ valueHelp: option.valueHelp, parser: parser, name: optionName);
175179
176- usage.addOption (optionName, option);
180+ script.usageOptionParameterMap[usage][optionName] = parameterName;
181+ usage.addOption (option);
177182 });
178183
179- _addSubCommandsForClass (usage, methodMirror.returnType);
184+ _addSubCommandsForClass (usage, script, methodMirror.returnType);
180185
181186 return usage;
182187}
@@ -190,7 +195,7 @@ getParserFromType(TypeMirror typeMirror) {
190195 return null ;
191196}
192197
193- _addSubCommandsForClass (Usage usage, TypeMirror typeMirror) {
198+ _addSubCommandsForClass (Usage usage, DeclarationScript script, TypeMirror typeMirror) {
194199 if (typeMirror is ClassMirror ) {
195200
196201 var methods = typeMirror.instanceMembers.values;
@@ -214,6 +219,7 @@ _addSubCommandsForClass(Usage usage, TypeMirror typeMirror) {
214219 .decode (MirrorSystem .getName (methodMirror.simpleName));
215220 getUsageFromFunction (
216221 methodMirror,
222+ script,
217223 usage: usage.addCommand (commandName, subCommand));
218224 });
219225 }
@@ -241,7 +247,7 @@ getFirstMetadataMatch(DeclarationMirror declaration, bool match(metadata)) {
241247 .firstWhere (match, orElse: () => null );
242248}
243249
244- void addOptionToParser (ArgParser parser, String name, Option option) {
250+ void addOptionToParser (ArgParser parser, Option option) {
245251
246252 var suffix;
247253
@@ -282,7 +288,7 @@ void addOptionToParser(ArgParser parser, String name, Option option) {
282288
283289 var parserMethod = 'add$suffix ' ;
284290
285- reflect (parser).invoke (new Symbol (parserMethod), [name], namedParameters);
291+ reflect (parser).invoke (new Symbol (parserMethod), [option. name], namedParameters);
286292}
287293
288294// Returns a List whose elements are the required argument count, and whether
@@ -329,18 +335,20 @@ MethodMirror getUnnamedConstructor(ClassMirror classMirror) {
329335 constructor.constructorName == const Symbol ('' ), orElse: () => null );
330336}
331337
332- convertCommandInvocationToInvocation (CommandInvocation commandInvocation, MethodMirror method, {Symbol memberName: #call}) {
338+ convertCommandInvocationToInvocation (CommandInvocation commandInvocation, MethodMirror method, Map < String , String > optionParameterMap, {Symbol memberName: #call}) {
333339
334340 var positionals = commandInvocation.positionals;
335341
336342 var named = {};
337343
338344 commandInvocation.options.forEach ((option, value) {
339- var paramSymbol = new Symbol (dashesToCamelCase. encode ( option) );
345+ var paramSymbol = new Symbol (optionParameterMap[ option] );
340346 var paramExists = method.parameters.any ((param) =>
341347 param.simpleName == paramSymbol);
342- if (paramExists) {
348+ if (paramExists) {
343349 named[paramSymbol] = value;
350+ } else {
351+ // print('Param "${optionParameterMap[option]}" does not exist for option "$option"');
344352 }
345353 });
346354
0 commit comments