public T Map(IDictionary <string, CommandLineArgument> arguments, T instance) { HashSet <CommandLineArgument> sharedArguments = new HashSet <CommandLineArgument>(); foreach (var mapping in MappingList.FromType <T>()) { if (mapping.IsOption()) { var wasSet = SetOptionValue(instance, mapping, arguments); if (wasSet) { sharedArguments.Add(mapping.CommandLineArgument); ValidateProperty(instance, mapping.PropertyInfo); MappedCommandLineArgument?.Invoke(this, new MapperEventArgs(mapping.CommandLineArgument, mapping.PropertyInfo, instance)); } } else { var wasSet = SetArgumentValue(instance, mapping, arguments); if (wasSet) { sharedArguments.Add(mapping.CommandLineArgument); ValidateProperty(instance, mapping.PropertyInfo); MappedCommandLineArgument?.Invoke(this, new MapperEventArgs(mapping.CommandLineArgument, mapping.PropertyInfo, instance)); } } } CheckForUnmappedArguments(arguments, sharedArguments, instance); return(instance); }
private void MapHelpOnly(T instance, ArgumentClassInfo argumentInfo, IDictionary <string, CommandLineArgument> arguments, CommandLineArgument helpRequest) { var helpCommand = factory.CreateInstance <HelpCommand>(); helpCommand.Arguments = new HelpCommandArguments { ArgumentInfos = argumentInfo, ArgumentDictionary = arguments }; argumentInfo.HelpCommand.PropertyInfo.SetValue(instance, helpCommand); MappedCommandLineArgument?.Invoke(this, new MapperEventArgs(helpRequest ?? new CommandLineArgument(), argumentInfo.HelpCommand.PropertyInfo, instance)); }
/// <summary>Tries to map all the <see cref="arguments"/> to one of the specified commands.</summary> /// <param name="instance">The instance.</param> /// <param name="argumentInfo">The argument information.</param> /// <param name="arguments">The arguments.</param> private void MapArgumentsToCommand(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); // NOTE: if the argument class contains a command that has the IsDefaultCommand property set to true, // this call will always return a command! var commandToCreate = GetCommandByNameOrDefault(argumentInfo, firstArgument?.Name, arguments); // no mapper if we could not create a command but we try to map the arguments to the application arguments first. // if there are remaining or shared argument, we map them to the command arguments later ! MapApplicationArguments(instance, arguments); if (commandToCreate != null) { // Now that we found a command, we create it and map the remaining (and shared) parameters to the commands arguments class var command = CreateCommandInstance(commandToCreate, arguments); commandToCreate.PropertyInfo.SetValue(instance, command, null); var commandLineArgument = firstArgument ?? new CommandLineArgument(); MappedCommandLineArgument?.Invoke(this, new MapperEventArgs(commandLineArgument, commandToCreate.PropertyInfo, instance)); } }
private void OnMappedCommandLineArgument(object sender, MapperEventArgs e) { MappedCommandLineArgument?.Invoke(this, e); }