private static CommandInfo GetCommandByName(ArgumentClassInfo argumentInfo, string commandName, IDictionary <string, CommandLineArgument> arguments) { if (commandName != null) { foreach (var command in argumentInfo.CommandInfos) { var commandAttribute = command.Attribute; if (IsEqual(commandAttribute.Name, commandName)) { arguments.Remove(commandName); return(command); } foreach (var alias in commandAttribute.Aliases) { if (IsEqual(alias, commandName)) { arguments.Remove(commandName); return(command); } } } } return(null); }
private IArgumentMapper <T> CreateMapper <T>() where T : class { var info = new ArgumentClassInfo(typeof(T)); return(info.HasCommands ? (IArgumentMapper <T>)ObjectFactory.CreateInstance <CommandMapper <T> >() : ObjectFactory.CreateInstance <ArgumentMapper <T> >()); }
private void PrintCommandHelp(CommandInfo commandInfo, string argumentName) { if (!string.IsNullOrEmpty(argumentName)) { if (commandInfo.ArgumentType != null) { var classInfo = new ArgumentClassInfo(commandInfo.ArgumentType); var parameterInfo = classInfo.GetParameterInfo(argumentName); if (parameterInfo != null) { engine.PrintHelp(parameterInfo.PropertyInfo, resourceManager); return; } } else { // TODO: Forward to help provider ??? Console.WriteLine("The command does not take any parameters. So no help could be found"); return; } } if (commandInfo.ArgumentType != null) { engine.PrintHelp(commandInfo.ArgumentType, resourceManager); } else { engine.PrintHelp(commandInfo.PropertyInfo.PropertyType, resourceManager); } }
private void MapHelpOnly(T instance, ArgumentClassInfo argumentInfo, IDictionary <string, CommandLineArgument> arguments) { var helpCommand = factory.CreateInstance <HelpCommand>(); helpCommand.Arguments = new HelpCommandArguments { ArgumentInfos = argumentInfo, ArgumentDictionary = arguments }; argumentInfo.HelpCommand.PropertyInfo.SetValue(instance, helpCommand); }
private void MapCommands(T instance, ArgumentClassInfo argumentInfo, IDictionary <string, CommandLineArgument> arguments) { // Note: per definition the help command has to be the first command line argument var firstArgument = GetFirstArgument(arguments); // TODO make some checks here ??? var commandToCreate = GetCommandByName(argumentInfo, firstArgument?.Name, arguments); if (commandToCreate == null) { return; } var command = CreateCommandInstance(commandToCreate, arguments); commandToCreate.PropertyInfo.SetValue(instance, command, null); }
/// <summary>Maps the give argument dictionary to the given instance.</summary> /// <param name="arguments">The arguments to map.</param> /// <param name="instance">The instance to map the arguments to.</param> /// <returns>The instance of the class, the command line argument were mapped to</returns> public T Map(IDictionary <string, CommandLineArgument> arguments, T instance) { var argumentInfo = new ArgumentClassInfo(typeof(T)); if (ArgumentsContainHelpRequest(arguments, argumentInfo)) { MapHelpOnly(instance, argumentInfo, arguments); return(instance); } if (argumentInfo.HasCommands) { MapCommands(instance, argumentInfo, arguments); } if (arguments.Any()) { var defaultMapper = new ArgumentMapper <T>(factory); defaultMapper.Map(arguments, instance); } return(instance); }
private static bool ArgumentsContainHelpRequest(IDictionary <string, CommandLineArgument> arguments, ArgumentClassInfo argumentInfo) { if (argumentInfo.HelpCommand == null) { return(false); } foreach (var identifier in argumentInfo.HelpCommand.Attribute.GetIdentifiers()) { if (arguments.ContainsKey(identifier)) { arguments.Remove(identifier); return(true); } } return(false); }