Exemple #1
0
        private void FillAllOptions(IEnumerable <string> args, Command command)
        {
            CommandOptionAttribute option = null;

            foreach (var arg in args)
            {
                var matchingOption = FindOptionProperty(arg, command);

                if (matchingOption.Key != null)
                {
                    if (matchingOption.Value.PropertyType == typeof(bool))
                    {
                        matchingOption.Key.SetValue(command, matchingOption.Value, true.ToString());
                    }
                    else
                    {
                        option = matchingOption.Key;
                    }
                }
                else
                {
                    option.SetValue(command, matchingOption.Value, arg);
                }
            }
        }
        public void Should_Parse_Requirement_Correctly(string value, bool expected)
        {
            // Given, When
            var option = new CommandOptionAttribute($"-o|--option {value}");

            // Then
            option.IsRequired.ShouldBe(expected);
        }
        public void Should_Parse_Only_Long_Name()
        {
            // Given, When
            var option = new CommandOptionAttribute("--option");

            // Then
            option.LongNames.ShouldContain("option");
        }
        public void Should_Parse_Long_Name_Correctly()
        {
            // Given, When
            var option = new CommandOptionAttribute("-o|--option <VALUE>");

            // Then
            option.LongNames.ShouldContain("option");
        }
        public void Should_Accept_Dash_And_Underscore_In_Value_Name(string template, string name)
        {
            // Given, When
            var result = new CommandOptionAttribute(template);

            // Then
            result.ValueName.ShouldBe(name);
        }
    public void Is_Not_Hidden_From_Help_By_Default()
    {
        // Given, When
        var result = new CommandOptionAttribute("--foo");

        // Then
        result.IsHidden.ShouldBeFalse();
    }
        public void Should_Parse_Value_Correctly(string value)
        {
            // Given, When
            var option = new CommandOptionAttribute($"-o|--option {value}");

            // Then
            option.ValueName.ShouldBe("VALUE");
        }
        public void Should_Parse_Only_Short_Name()
        {
            // Given, When
            var option = new CommandOptionAttribute("-o");

            // Then
            option.ShortNames.ShouldContain("o");
        }
        public void Should_Parse_Short_Name_Correctly()
        {
            // Given, When
            var option = new CommandOptionAttribute("-o|--option <VALUE>");

            // Then
            option.ShortName.ShouldBe("o");
        }
        public void Template_Parts_Can_Appear_In_Any_Order(string template)
        {
            // Given, When
            var result = new CommandOptionAttribute(template);

            // Then
            result.LongNames.ShouldContain("foo");
            result.ShortNames.ShouldContain("f");
            result.ValueName.ShouldBe("BAR");
        }
        public void Multiple_Long_Options_Are_Supported()
        {
            // Given, When
            var result = new CommandOptionAttribute("--foo|--bar");

            // Then
            result.LongNames.Count.ShouldBe(2);
            result.LongNames.ShouldContain("foo");
            result.LongNames.ShouldContain("bar");
        }
        public void Multiple_Short_Options_Are_Supported()
        {
            // Given, When
            var result = new CommandOptionAttribute("-f|-b");

            // Then
            result.ShortNames.Count.ShouldBe(2);
            result.ShortNames.ShouldContain("f");
            result.ShortNames.ShouldContain("b");
        }
 public CommandOption(
     Type parameterType, ParameterKind parameterKind, PropertyInfo property, string description,
     TypeConverterAttribute converter, CommandOptionAttribute optionAttribute, DefaultValueAttribute defaultValue)
     : base(parameterType, parameterKind, property, description, converter, optionAttribute.IsRequired)
 {
     LongName     = optionAttribute.LongName;
     ShortName    = optionAttribute.ShortName;
     ValueName    = optionAttribute.ValueName;
     DefaultValue = defaultValue;
 }
    public void Can_Indicate_That_It_Must_Be_Hidden_From_Help_Text()
    {
        // Given, When
        var result = new CommandOptionAttribute("--foo")
        {
            IsHidden = true
        };

        // Then
        result.IsHidden.ShouldBeTrue();
    }
 public CommandOption(
     Type parameterType, ParameterKind parameterKind, PropertyInfo property, string description,
     TypeConverterAttribute converter, CommandOptionAttribute optionAttribute,
     IEnumerable <ParameterValidationAttribute> validators, DefaultValueAttribute defaultValue)
     : base(parameterType, parameterKind, property, description, converter, validators, false)
 {
     LongNames    = optionAttribute.LongNames;
     ShortNames   = optionAttribute.ShortNames;
     ValueName    = optionAttribute.ValueName;
     DefaultValue = defaultValue;
 }
 public CommandOption(
     Type parameterType, ParameterKind parameterKind, PropertyInfo property, string?description,
     TypeConverterAttribute?converter, PairDeconstructorAttribute?deconstructor,
     CommandOptionAttribute optionAttribute, IEnumerable <ParameterValidationAttribute> validators,
     DefaultValueAttribute?defaultValue, bool valueIsOptional)
     : base(parameterType, parameterKind, property, description, converter,
            defaultValue, deconstructor, validators, false)
 {
     LongNames       = optionAttribute.LongNames;
     ShortNames      = optionAttribute.ShortNames;
     ValueName       = optionAttribute.ValueName;
     ValueIsOptional = valueIsOptional;
 }
Exemple #17
0
    public CommandTreeParser(CommandModel configuration, ICommandAppSettings settings, ParsingMode?parsingMode = null)
    {
        if (settings is null)
        {
            throw new ArgumentNullException(nameof(settings));
        }

        _configuration = configuration;
        _parsingMode   = parsingMode ?? _configuration.ParsingMode;
        _help          = new CommandOptionAttribute("-h|--help");

        CaseSensitivity = settings.CaseSensitivity;
    }
        private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
        {
            var description  = property.GetCustomAttribute <DescriptionAttribute>();
            var converter    = property.GetCustomAttribute <TypeConverterAttribute>();
            var defaultValue = property.GetCustomAttribute <DefaultValueAttribute>();

            var kind = property.PropertyType == typeof(bool)
                ? ParameterKind.Flag
                : ParameterKind.Single;

            return(new CommandOption(property.PropertyType, kind,
                                     property, description?.Description, converter,
                                     attribute, defaultValue));
        }
        private static ParameterKind GetOptionKind(
            Type type,
            CommandOptionAttribute attribute,
            PairDeconstructorAttribute?deconstructor,
            TypeConverterAttribute?converter)
        {
            if (attribute.ValueIsOptional)
            {
                return(ParameterKind.FlagWithValue);
            }

            if (type.IsPairDeconstructable() && (deconstructor != null || converter == null))
            {
                return(ParameterKind.Pair);
            }

            return(GetParameterKind(type));
        }
        private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
        {
            var description  = property.GetCustomAttribute <DescriptionAttribute>();
            var converter    = property.GetCustomAttribute <TypeConverterAttribute>();
            var validators   = property.GetCustomAttributes <ParameterValidationAttribute>(true);
            var defaultValue = property.GetCustomAttribute <DefaultValueAttribute>();

            var kind = GetParameterKind(property.PropertyType);

            if (defaultValue == null && property.PropertyType == typeof(bool))
            {
                defaultValue = new DefaultValueAttribute(false);
            }

            return(new CommandOption(property.PropertyType, kind,
                                     property, description?.Description, converter,
                                     attribute, validators, defaultValue));
        }
 public CommandTreeParser(CommandModel configuration, ParsingMode?parsingMode = null)
 {
     _configuration = configuration;
     _parsingMode   = parsingMode ?? _configuration.ParsingMode;
     _help          = new CommandOptionAttribute("-h|--help");
 }
 public CommandTreeParser(CommandModel configuration, CommandOptionAttribute help = null)
 {
     _configuration = configuration;
     _help          = help;
 }
Exemple #23
0
        /// <summary>
        /// Parses user defined command line arguments above.
        /// </summary>
        /// <param name="args">Command line arguments compatible with Cake</param>
        /// <returns></returns>
        public static CommandLineArgs Parse(string[] args)
        {
            bool displaysHelp = false;
            var  newArgs      = new CommandLineArgs();
            var  app          = new CommandApp <ParseCommandLineArgs>();

            app.Configure((config) =>
            {
                FieldInfo fi = config.GetType().GetField("_registrar", BindingFlags.NonPublic | BindingFlags.Instance);
                var reg      = (ITypeRegistrar)fi.GetValue(config);

                reg.RegisterLazy(typeof(CommandLineArgs), () => { return(newArgs); });
                config.PropagateExceptions();

                // From src\Cake\Program.cs: Top level examples.
                string exeName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                config.SetApplicationName(exeName);
                //config.AddExample(new[] { string.Empty });
                //config.AddExample(new[] { "build.cake", "--verbosity", "quiet" });
                //config.AddExample(new[] { "build.cake", "--tree" });
            }
                          );
            try
            {
                var helpOpt = new CommandOptionAttribute("-h|--help");
                var isMatch = helpOpt.GetType().GetMethod("IsMatch", BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (var arg in args)
                {
                    if ((bool)isMatch.Invoke(helpOpt, new object[] { arg.TrimStart('-') }))
                    {
                        displaysHelp = true;
                    }
                }

                app.Run(args);
            }
            catch (CommandAppException ex)
            {
                MethodInfo mi          = typeof(CommandApp).GetMethod("GetRenderableErrorMessage", BindingFlags.NonPublic | BindingFlags.Static);
                var        renderables = (List <IRenderable>)mi.Invoke(null, new object[2] {
                    ex, true
                });
                renderables.ForEach((x) => AnsiConsole.Console.Write(x));
                Environment.Exit(-2);
            }

            foreach (var tasklist in newArgs.ListOfTasks)
            {
                foreach (var a in helpers.split(tasklist.ToLower()))
                {
                    switch (a.ToLower())
                    {
                    case nameof(all):
                        newArgs.OSS          = "win7,linux";
                        newArgs.r2r_targets  = "choco";
                        newArgs.r2r_build    = true;
                        newArgs.test         = true;
                        newArgs.codecoverage = true;
                        //newArgs.coverageFormats = "HtmlSummary,Cobertura";
                        newArgs.coverageFormats       = "Html,HtmlSummary,Cobertura";
                        newArgs.testsToRun            = "chocolatey.tests,chocolatey.tests.integration,chocolatey.tests2";
                        newArgs.uploadCoverageResults = true;
                        newArgs.ShowCommands          = true;
                        break;

                    case nameof(buildexe):
                        newArgs.r2r_build = true;
                        if (string.IsNullOrEmpty(newArgs.r2r_targets))
                        {
                            newArgs.r2r_targets = "choco";
                        }
                        if (string.IsNullOrEmpty(newArgs.OSS))
                        {
                            newArgs.OSS = "win7";
                        }
                        break;

                    case nameof(buildsolution):
                        newArgs.build = true;
                        break;

                    case nameof(pushexe):
                        newArgs.r2r_push = true;
                        break;
                    }
                }
            }

            if (displaysHelp)
            {
                newArgs.displaysHelp = true;
                Environment.Exit(0);
            }

            return(newArgs);
        }
Exemple #24
0
 public CommandTreeParser(CommandModel configuration)
 {
     _configuration = configuration;
     _help          = new CommandOptionAttribute("-h|--help");
 }