Ejemplo n.º 1
0
        private IArgumentMapper <T> CreateMapper <T>()
            where T : class
        {
            var info = ArgumentClassInfo.FromType <T>();

            return(info.HasCommands ? (IArgumentMapper <T>)ObjectFactory.CreateInstance <CommandMapper <T> >() : ObjectFactory.CreateInstance <ArgumentMapper <T> >());
        }
Ejemplo n.º 2
0
        private void PrintCommandHelp(CommandInfo commandInfo, string argumentName)
        {
            if (!string.IsNullOrEmpty(argumentName))
            {
                if (commandInfo.ArgumentType != null)
                {
                    var classInfo     = ArgumentClassInfo.FromType(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);
            }
        }
Ejemplo n.º 3
0
        private static CommandInfo GetCommandByNameOrDefault(ArgumentClassInfo argumentInfo, string commandName, IDictionary <string, CommandLineArgument> arguments)
        {
            if (commandName == null)
            {
                return(argumentInfo.DefaultCommand);
            }

            foreach (var commandInfo in argumentInfo.CommandInfos)
            {
                if (IsEqual(commandInfo.ParameterName, commandName))
                {
                    arguments.Remove(commandName);
                    DecreaseIndices(arguments);
                    return(commandInfo);
                }

                foreach (var alias in commandInfo.Attribute.GetIdentifiers())
                {
                    if (IsEqual(alias, commandName))
                    {
                        arguments.Remove(commandName);
                        DecreaseIndices(arguments);
                        return(commandInfo);
                    }
                }
            }

            return(argumentInfo.DefaultCommand);
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        /// <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 = ArgumentClassInfo.FromType <T>();
            var helpRequest  = GetHelpRequest(arguments, argumentInfo);

            if (helpRequest != null)
            {
                MapHelpOnly(instance, argumentInfo, arguments, helpRequest);
                return(instance);
            }

            if (argumentInfo.HasCommands)
            {
                MapArgumentsToCommand(instance, argumentInfo, arguments);
            }

            foreach (var argument in arguments.Values.Where(x => !x.Mapped))
            {
                UnmappedCommandLineArgument?.Invoke(this, new MapperEventArgs(argument, null, instance));
            }

            return(instance);
        }
Ejemplo n.º 6
0
        /// <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));
            }
        }
Ejemplo n.º 7
0
        private static CommandLineArgument GetHelpRequest(IDictionary <string, CommandLineArgument> arguments, ArgumentClassInfo argumentInfo)
        {
            if (argumentInfo.HelpCommand == null)
            {
                return(null);
            }

            foreach (var identifier in argumentInfo.HelpCommand.Attribute.GetIdentifiers())
            {
                if (arguments.TryGetValue(identifier, out var commandLineArgument))
                {
                    arguments.Remove(identifier);
                    return(commandLineArgument);
                }
            }

            return(null);
        }