public void CommandLine_Validate_MultipleValue_SingleValue_Validator_Valid()
        {
            Logger.LogMessage("Testing valid agument with multiple value with single value validator 'argumentone argumenttwo'");

            m_application.SetupInputs = (CommandLineApplication commandLineApplication) =>
            {
                ConfiguredInputs configuredInputs = new ConfiguredInputs();

                // Setup arguments
                CommandArgument firstArgument = commandLineApplication.Argument(
                    "firstArgument",
                    "this is the first argument",
                    multipleValues: true);

                Action <string> firstArgumentValidator = (string data) =>
                {
                    m_numberOfElements++;
                };

                configuredInputs.Map["firstArgument"] = new ArgumentConfiguration(
                    firstArgument,
                    isRequired: false,
                    validationRoutine: firstArgumentValidator);

                return(configuredInputs);
            };

            // Validate Argument with multiple values is working with valid scenario with single value validator
            m_numberOfElements = 0;
            Assert.AreEqual(m_numberOfElements, 0);
            ValidateCommand(true, m_application, new string[] { "argumenone", "argumentwo" });
            Assert.AreEqual(m_numberOfElements, 2);
        }
        /// <summary>
        /// Sets up the inputs supported by this command.
        /// </summary>
        /// <param name="commandLineApplication">The Microsoft.Extensions.CommandLineUtils command object</param>
        /// <returns>The configured command inputs</returns>
        protected override ConfiguredInputs SetupInputs(CommandLineApplication commandLineApplication)
        {
            // This command supports no inputs
            ConfiguredInputs configuredInputs = new ConfiguredInputs();

            return(configuredInputs);
        }
        /// <summary>
        /// Sets up the inputs supported by this command.
        /// </summary>
        /// <param name="commandLineApplication">The Microsoft.Extensions.CommandLineUtils command object</param>
        /// <returns>The configured command inputs</returns>
        protected override ConfiguredInputs SetupInputs(CommandLineApplication commandLineApplication)
        {
            ConfiguredInputs configuredInputs = new ConfiguredInputs();

            // Setup --example option
            CommandOption exampleOption = commandLineApplication.Option(
                "--example",
                "--example description",
                CommandOptionType.SingleValue);

            Action <string> templateFileOptionValidator = (string data) =>
            {
                if ("invalid".Equals(data))
                {
                    throw new CommandParsingException(commandLineApplication, string.Format("Invalid value for option --example"));
                }
            };

            // --example don't declare any restriction
            configuredInputs.Map["--example"] = new OptionConfiguration(
                exampleOption,
                isRequired: false,
                validationRoutine: templateFileOptionValidator);

            // Setup --alone option
            CommandOption aloneOption = commandLineApplication.Option(
                "--alone",
                "--alone description",
                CommandOptionType.NoValue);

            // --alone cannot be declared with "--together" or "--example"
            configuredInputs.Map["--alone"] = new OptionConfiguration(
                aloneOption,
                isRequired: false,
                disallowedSwitches: new List <string>()
            {
                "--together", "--example"
            },
                validationRoutine: null);

            // Setup --together option
            CommandOption togheterOption = commandLineApplication.Option(
                "--together",
                "--together description",
                CommandOptionType.NoValue);

            // --together option require declare --example
            configuredInputs.Map["--together"] = new OptionConfiguration(
                togheterOption,
                isRequired: false,
                validationRoutine: null,
                requiredSwitches: new List <string>()
            {
                "--example"
            });

            return(configuredInputs);
        }
Beispiel #4
0
        /// <summary>
        /// Sets up the inputs supported by this command.
        /// </summary>
        /// <param name="commandLineApplication">The Microsoft.Extensions.CommandLineUtils command object</param>
        /// <returns>The configured command inputs</returns>
        protected override ConfiguredInputs SetupInputs(CommandLineApplication commandLineApplication)
        {
            ConfiguredInputs configuredInputs = new ConfiguredInputs();

            // Setup --example option
            CommandOption exampleOption = commandLineApplication.Option(
                "--example",
                "--example description",
                CommandOptionType.SingleValue);

            Action <string> templateFileOptionValidator = (string data) =>
            {
                throw new FileNotFoundException(string.Format("File {0} not found", data));
            };

            // --example don't declare any restriction
            configuredInputs.Map["--example"] = new OptionConfiguration(
                exampleOption,
                isRequired: false,
                validationRoutine: templateFileOptionValidator);

            // Setup --example2 option
            CommandOption example2Option = commandLineApplication.Option(
                "--example2",
                "--example description",
                CommandOptionType.SingleValue);

            // --example don't declare any restriction
            configuredInputs.Map["--example2"] = new OptionConfiguration(
                example2Option,
                isRequired: false);

            configuredInputs.ValidateAllInputs = () =>
            {
                // Require at least one of the two switches to be present
                if (!configuredInputs.Map["--example"].HasValue() && !configuredInputs.Map["--example2"].HasValue())
                {
                    // InvalidOperationException has HRESULT = 0x80131509
                    throw new InvalidOperationException("Must specify at least one of --example and --example2");
                }
            };

            return(configuredInputs);
        }
Beispiel #5
0
        public void CommandLine_Validate_Argument_MultipleValue_MultipleValue_Validator_Valid()
        {
            Log.Comment("Testing valid argument with multiple value with multiple value validator 'argumentone argumenttwo'");
            Log.Comment("Initializing CommandLineTests test.");

            m_application.SetupInputs = (CommandLineApplication commandLineApplication) =>
            {
                ConfiguredInputs configuredInputs = new ConfiguredInputs();

                // Setup arguments
                CommandArgument firstArgument = commandLineApplication.Argument(
                    "firstArgument",
                    "this is the first argument",
                    multipleValues: true);

                Action <List <string> > firstArgumentValidator = (List <string> values) =>
                {
                    foreach (string value in values)
                    {
                        m_numberOfElements++;
                    }
                };

                configuredInputs.Map["firstArgument"] = new ArgumentConfiguration(
                    firstArgument,
                    isRequired: false,
                    validationRoutineMultipleValue: firstArgumentValidator);

                return(configuredInputs);
            };

            // Validate argument with multiple values is working with valid scenario with mutiple value validator
            m_numberOfElements = 0;
            Verify.AreEqual(m_numberOfElements, 0);
            ValidateCommand(true, m_application, new string[] { "argumenone", "argumentwo" });
            Verify.AreEqual(m_numberOfElements, 2);
        }
        public new void TestInitialize()
        {
            Logger.LogMessage("Initializing CommandLineTests test.");
            CLIApplication.HelpDescriptionText    = "Help description.";
            CLIApplication.VersionDescriptionText = "Version description";
            CLIApplication.HelpOptionSwitches     = "-h";
            CLIApplication.VersionOptionSwitch    = "-v";

            m_application = new CLIApplication(
                applicationName: "MyTool.exe",
                description: "Example for running the program",
                versionString: "Version 2.0",
                exampleCommands: new List <string>()
            {
                "MyTool.exe -wa a", "MyTool.exe -xa"
            },
                onCommandPreExecute: null);

            m_application.SetupInputs = (CommandLineApplication commandLineApplication) =>
            {
                ConfiguredInputs configuredInputs = new ConfiguredInputs();

                // Setup arguments
                CommandArgument firstArgument = commandLineApplication.Argument(
                    "firstArgument",
                    "this is the first argument");

                Action <string> firstArgumentValidator = (string data) =>
                {
                    if (data == "invalidFirstArgument")
                    {
                        throw new CommandParsingException(commandLineApplication, "Error in first argument invalidArgument.");
                    }
                };

                configuredInputs.Map["firstArgument"] = new ArgumentConfiguration(
                    firstArgument,
                    isRequired: false,
                    validationRoutine: firstArgumentValidator);

                CommandArgument secondArgument = commandLineApplication.Argument(
                    "secondArgument",
                    "this is the second argument");

                Action <string> secondArgumentValidator = (string data) =>
                {
                    if (data == "invalidSecondArgument")
                    {
                        throw new CommandParsingException(commandLineApplication, "Error in second argument invalidArgument.");
                    }
                };

                configuredInputs.Map["secondArgument"] = new ArgumentConfiguration(
                    secondArgument,
                    isRequired: false,
                    validationRoutine: secondArgumentValidator);

                // Setup -wa option
                CommandOption waOption = commandLineApplication.Option(
                    "-wa",
                    "MyTool.exe -wa something",
                    CommandOptionType.SingleValue);

                Action <string> waOptionValidator = (string data) =>
                {
                    if (data == "invalid")
                    {
                        throw new CommandParsingException(commandLineApplication, "Error in command wa.");
                    }
                };

                configuredInputs.Map["wa"] = new OptionConfiguration(
                    waOption,
                    isRequired: false,
                    validationRoutine: waOptionValidator);

                // Setup -bad option
                CommandOption badOption = commandLineApplication.Option(
                    "-bad",
                    "MyTool.exe -bad something",
                    CommandOptionType.SingleValue);

                Action <string> badOptionValidator = (string data) =>
                {
                    Logger.LogMessage("Running bad command.");
                    throw new FileNotFoundException(string.Format("File {0} not found", data));
                };

                configuredInputs.Map["bad"] = new OptionConfiguration(
                    badOption,
                    isRequired: false,
                    validationRoutine: badOptionValidator);

                // Setup -multipleValue option
                CommandOption multipleValueOption = commandLineApplication.Option(
                    "-multipleValue",
                    "MyTool.exe -multipleValue something1 -multipleValue something2",
                    CommandOptionType.MultipleValue);

                Action <List <string> > multipleValueValidator = (List <string> values) =>
                {
                    Logger.LogMessage("Running multipleValue.");
                    foreach (string value in values)
                    {
                        m_numberOfElements++;
                    }
                };

                configuredInputs.Map["multipleValue"] = new OptionConfiguration(
                    multipleValueOption,
                    isRequired: false,
                    validationRoutineMultipleValue: multipleValueValidator);

                // Setup -multipleValue2 option
                CommandOption multipleValue2Option = commandLineApplication.Option(
                    "-multipleValue2",
                    "MyTool.exe -multipleValue2 something1 -multipleValue2 something2",
                    CommandOptionType.MultipleValue);

                Action <string> multipleValue2Validator = (string values) =>
                {
                    Logger.LogMessage("Running multipleValue.");
                    m_numberOfElements++;
                };

                configuredInputs.Map["multipleValue2"] = new OptionConfiguration(
                    multipleValue2Option,
                    isRequired: false,
                    validationRoutine: multipleValue2Validator);

                // Setup -xa option
                CommandOption xaOption = commandLineApplication.Option(
                    "-xa",
                    "MyTool.exe -xa",
                    CommandOptionType.NoValue);

                configuredInputs.Map["xa"] = new OptionConfiguration(
                    xaOption,
                    isRequired: false,
                    validationRoutine: null);

                return(configuredInputs);
            };

            m_application.OnExecute = (ConfiguredInputs configuredInputs) =>
            {
                Logger.LogMessage("Running on excecute for options.");

                return(0);
            };

            m_application.ConfigureCommand <CleanupCommand>();
        }
 /// <summary>
 /// Method that gets invoked when this command is being executed with validated inputs.
 /// Note: This method must handle all exceptions and return appropriate exit code for the program.
 /// </summary>
 /// <param name="configuredInputs">The validated inputs</param>
 /// <returns>The program exit code</returns>
 protected override int OnExecute(ConfiguredInputs configuredInputs)
 {
     Log.Comment("Running OnExecute for command cleanup");
     return(0);
 }
 /// <summary>
 /// Method that gets invoked when this command is being executed with validated inputs.
 /// Note: This method must handle all exceptions and return appropriate exit code for the program.
 /// </summary>
 /// <param name="configuredInputs">The validated inputs</param>
 /// <returns>The program exit code</returns>
 protected override int OnExecute(ConfiguredInputs configuredInputs)
 {
     Logger.LogMessage("Running OnExecute for command cleanup");
     return(0);
 }