Exemple #1
0
        public void EnsureDefaultCommandIsSetCorrectly()
        {
            var argumentInfo = ArgumentClassInfo.FromType(typeof(CommandClassWithDefault));

            argumentInfo.CommandInfos.Should().HaveCount(2);
            argumentInfo.DefaultCommand.Should().NotBeNull();
        }
        /// <summary>Executes the command that was specified in the command line arguments.
        /// If no argument was specified but the IsDefaultCommand property was set at one of the commands, a simulated command</summary>
        /// <param name="useDefaultCommand">if set to <c>true</c> [use default command].</param>
        /// <returns></returns>
        protected virtual bool ExecuteCommand(bool useDefaultCommand)
        {
            var applicationArguments = new ArgumentClassInfo(typeof(T));

            if (!applicationArguments.HasCommands)
            {
                return(false);
            }

            ICommand command = GetMappedCommand();

            if (command != null)
            {
                RunWithCommand(command);
                return(true);
            }

            if (useDefaultCommand)
            {
                var defaultCommand = GetDefaultCommand();
                if (defaultCommand != null)
                {
                    RunWithCommand(defaultCommand);
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        public override void InitializeFromString(T instance, string args)
        {
            try
            {
                CommandLineEngine.HandledCommandLineArgument   += OnMappedParameter;
                CommandLineEngine.UnhandledCommandLineArgument += OnUnmappedParameter;
                base.InitializeFromString(instance, args);

                var info = ArgumentClassInfo.FromType <T>();
                foreach (var property in info.Properties)
                {
                    application.Argument(property.ParameterName, property.PropertyInfo.GetValue(instance));
                }
            }
            finally
            {
                CommandLineEngine.HandledCommandLineArgument   -= OnMappedParameter;
                CommandLineEngine.UnhandledCommandLineArgument -= OnUnmappedParameter;
            }

            void OnMappedParameter(object sender, CommandLineArgumentEventArgs e)
            {
                var value = e.PropertyInfo.GetValue(e.Instance);

                application.MappedCommandLineParameter(e.PropertyInfo);
                application.MappedCommandLineParameter(e.PropertyInfo, value);
                application.MappedCommandLineParameter(e.PropertyInfo.Name, value);
            }

            void OnUnmappedParameter(object sender, CommandLineArgumentEventArgs e)
            {
                application.UnmappedCommandLineParameter(e.Argument.Name, e.Argument.Value);
            }
        }
Exemple #4
0
        public void EnsureHasCommandsIsComputedCorrectly()
        {
            var argumentInfo = ArgumentClassInfo.FromType(typeof(ApplicationArgs));

            argumentInfo.HasCommands.Should().BeTrue();

            argumentInfo = ArgumentClassInfo.FromType(typeof(ExecuteArgs));
            argumentInfo.HasCommands.Should().BeFalse();
        }
Exemple #5
0
        public override void InitializeArguments(T instance, string[] args)
        {
            base.InitializeArguments(instance, args);

            var info = new ArgumentClassInfo(typeof(T));

            foreach (var property in info.Properties)
            {
                application.Argument(property.ParameterName, property.PropertyInfo.GetValue(instance));
            }
        }
        private static CommandInfo GetDefaultCommand(bool withParameters)
        {
            var argumentClassInfo = new ArgumentClassInfo(typeof(T));

            if (withParameters)
            {
                return(argumentClassInfo.CommandInfos.FirstOrDefault(c => c.Attribute.IsDefaultCommand && c.ArgumentType != null));
            }

            return(argumentClassInfo.CommandInfos.FirstOrDefault(c => c.Attribute.IsDefaultCommand && c.ArgumentType == null));
        }
Exemple #7
0
        private static HelpCommandArguments GetArguments <T>(params string[] args)
        {
            var argumentDictionary = new Dictionary <string, CommandLineArgument>();
            int index = 0;

            foreach (var arg in args)
            {
                argumentDictionary[arg] = new CommandLineArgument {
                    Index = index, Name = arg
                };
                index++;
            }

            return(new HelpCommandArguments {
                ArgumentInfos = ArgumentClassInfo.FromType <T>(), ArgumentDictionary = argumentDictionary
            });
        }
        /// <summary>
        ///    Executes the command that was specified in the command line arguments. If no argument was specified but the IsDefaultCommand property was set at one of the commands, a
        ///    simulated command
        /// </summary>
        /// <returns></returns>
        protected virtual bool ExecuteCommand()
        {
            var applicationArguments = ArgumentClassInfo.FromType <T>();

            if (!applicationArguments.HasCommands)
            {
                return(false);
            }

            ICommand command = GetMappedCommand();

            if (command == null)
            {
                return(false);
            }

            RunWithCommand(command);
            return(true);
        }
Exemple #9
0
        public void EnsureCommandsAreSetCorrectly()
        {
            var argumentInfo = ArgumentClassInfo.FromType(typeof(CommandClassWithHelp));

            argumentInfo.CommandInfos.Should().HaveCount(2);
        }
Exemple #10
0
        public void EnsurePropertiesAreSetCorrectly()
        {
            var argumentInfo = ArgumentClassInfo.FromType(typeof(CommandClassWithHelp));

            argumentInfo.Properties.Should().HaveCount(4);
        }
Exemple #11
0
        public void EnsureHelpCommandIsSetCorrectly()
        {
            var argumentInfo = ArgumentClassInfo.FromType(typeof(CommandClassWithHelp));

            argumentInfo.HelpCommand.Should().NotBeNull();
        }