commandline
commandline copied to clipboard
Parsing via reflection and executing a named method
I would like to be able to parse a large number of verb commands within an assembly and execute a default method in a single line of code
example:
Parser.Default.ParseArguments(args, Assembly.GetExecutingAssembly(), "Run");
This would be similar to
// Get all classes with command line Verb Attribute
var verbTypes = (from type in Assembly.GetExecutingAssembly().GetTypes()
let testAttribute = Attribute.GetCustomAttribute(type, typeof(VerbAttribute))
where testAttribute != null
select type);
// Parser the command line
var results = Parser.Default.ParseArguments(args, verbTypes.ToArray());
results.WithParsed<VerbA>(action => action.Run())
.WithParsed<VerbB>(action => action.Run())
.WithParsed<VerbC>(action => action.Run());
In one console program I have 26 Verbs, mostly test actions.
The only problem I couldn't work out is how to run an action on the results without having to Parse with a Type. Of course, there should be checks that the method exists, and possibly an execution flag in the ParsedResults class to indicate the method has ben run.