public CommandOption Option(
     string template,
     string description,
     CommandOptionType optionType)
 {
     return(this.Option(template, description, optionType, (Action <CommandOption>)(_ => { }), false));
 }
        internal CommandOptionBuilder(List <CommandOption> parent, CommandOptionType type, string name, string description, IApplicationCommandBuilder builder)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(name));
            }
            if (string.IsNullOrEmpty(description))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(description));
            }

            if (type == CommandOptionType.SubCommand || type == CommandOptionType.SubCommandGroup)
            {
                throw new InvalidApplicationCommandException($"{type} is not allowed to be used here. Valid types are any non command type.");
            }

            _option = new CommandOption
            {
                Name        = name,
                Description = description,
                Type        = type
            };
            parent.Add(_option);
            _builder = builder;
        }
        public void RequiredOption_Pass(CommandOptionType type, string[] args, bool allowEmptyStrings)
        {
            var app = new CommandLineApplication(new TestConsole(_output));

            app.Option("-t", "Test", type).IsRequired(allowEmptyStrings);
            Assert.Equal(0, app.Execute(args));
        }
        private static CommandOption AddOption(CommandLineApplication app, string optionName, string description, CommandOptionType optionType)
        {
            string argSuffix = optionType == CommandOptionType.MultipleValue ? "..." : null;
            string argString = optionType == CommandOptionType.BoolValue ? null : $" <arg>{argSuffix}";

            return app.Option($"--{optionName}{argString}", description, optionType);
        }
Example #5
0
 private void ValidateArgs(string name, CommandOptionType arg, CommandOptionType requested)
 {
     if (arg != requested)
     {
         throw new Exception($"Attempted to parse {name} {arg} type to: {requested} which is not valid.");
     }
 }
        public void CanIdentifyArgumentInfoFromMethodsUsingParameterInfo(
            string parameterName,
            CommandOptionType commandOptionType,
            object defaultValue,
            string typeDisplayName,
            //string parameterDetails,
            string annotatedDescription,

            //string effectiveDescription,
            string template,
            Type type
            //bool showParameterDetails
            )
        {
            MethodInfo        methodInfo           = typeof(TestApplication).GetMethod("Execute");
            var               parameters           = methodInfo.GetParameters();
            var               parameter            = parameters.Single(p => p.Name == parameterName);
            CommandOptionInfo commandParameterInfo = new CommandOptionInfo(parameter,
                                                                           new AppSettings {
                MethodArgumentMode = ArgumentMode.Option
            });

            commandParameterInfo.CommandOptionType.Should().Be(commandOptionType, nameof(commandOptionType));
            commandParameterInfo.DefaultValue.Should().Be(defaultValue, nameof(defaultValue));
            commandParameterInfo.TypeDisplayName.Should().Be(typeDisplayName, nameof(typeDisplayName));
//            commandParameterInfo.Details.Should().Be(parameterDetails, nameof(parameterDetails));
            commandParameterInfo.AnnotatedDescription.Should().Be(annotatedDescription, nameof(annotatedDescription));

//            commandParameterInfo.EffectiveDescription.Should().Be(effectiveDescription, nameof(effectiveDescription));
            commandParameterInfo.Template.Should().Be(template, nameof(template));
            commandParameterInfo.Type.Should().Be(type, nameof(type));
        }
 public CommandOption Option(string template, string description, CommandOptionType optionType, Action<CommandOption> configuration)
 {
     var option = new CommandOption(template, optionType) { Description = description };
     Options.Add(option);
     configuration(option);
     return option;
 }
Example #8
0
        public CommandOption Option(string name, string Description, CommandOptionType multipleValues, params Func <CommandOption, int>[] validators)
        {
            var cmd = _config.Option(name, Description, CommandOptionType.SingleValue);

            Validate(cmd, validators);
            return(cmd);
        }
 public CommandOption Option(
     string template,
     string description,
     CommandOptionType optionType,
     Action <CommandOption> configuration)
 {
     return(this.Option(template, description, optionType, configuration, false));
 }
 public CommandOption Option(
     string template,
     string description,
     CommandOptionType optionType,
     bool inherited)
 {
     return(this.Option(template, description, optionType, (Action <CommandOption>)(_ => { }), inherited));
 }
Example #11
0
        /// <summary>
        /// Adds a command option.
        /// </summary>
        /// <param name="type">The type of option. Cannot be SubCommand or SubCommandGroup</param>
        /// <param name="name">Name of the option</param>
        /// <param name="description">Description for the option</param>
        /// <returns><see cref="CommandOptionBuilder"/></returns>
        public CommandOptionBuilder AddOption(CommandOptionType type, string name, string description)
        {
            if (_chosenType.HasValue && (_chosenType.Value == CommandOptionType.SubCommandGroup || _chosenType.Value == CommandOptionType.SubCommand))
            {
                throw new Exception("Cannot mix sub command / sub command groups with command options");
            }

            return(new CommandOptionBuilder(_options, type, name, description, this));
        }
Example #12
0
        public CommandOption Option(string template, string description, CommandOptionType optionType, Action <CommandOption> configuration)
        {
            var option = new CommandOption(template, optionType)
            {
                Description = description
            };

            Options.Add(option);
            configuration(option);
            return(option);
        }
Example #13
0
        public void ShouldCreateOptionProperly(
            string optionName, string description, CommandOptionType optionType
            )
        {
            var command = new CommandLineApplication();

            var option = command.CreateOption(optionName, description, optionType);

            Assert.Equal(optionName, option.LongName);
            Assert.Equal(description, option.Description);
            Assert.Equal(optionType, option.OptionType);
        }
        public IOptionsBuilder Option <T>(string template, string description, CommandOptionType type, bool inherited)
        {
            _options.Add(app =>
            {
                var option         = app.Option <T>(template, string.Empty, type);
                option.Inherited   = inherited;
                option.Description = description ?? _descriptionProvider.Value(CreateResourceKey(option.LongName));
                return(option);
            });

            return(this);
        }
        public virtual CommandOption Option(string template, string description, CommandOptionType optionType, Action <CommandOption> configuration, bool inherited)
        {
            var option = new CommandOption(template, optionType)
            {
                Description = description,
                Inherited   = inherited
            };

            Options.Add(option);
            configuration(option);
            return(option);
        }
Example #16
0
 public bool TryGetOptionType(Type clrType, out CommandOptionType optionType)
 {
     try
     {
         optionType = GetOptionType(clrType);
         return(true);
     }
     catch
     {
         optionType = default;
         return(false);
     }
 }
Example #17
0
        public static CommandOption CreateOption(
            this CommandLineApplication command,
            string name,
            string description,
            CommandOptionType type,
            string template = DefaultTemplate
            )
        {
            string optionName       = name.Dashrialize();
            string formatedTemplate = string.Format(template, optionName);

            return(command.Option(formatedTemplate, description, type));
        }
Example #18
0
        public CommandOption(string template, CommandOptionType optionType)
        {
            Template   = template;
            OptionType = optionType;
            Values     = new List <string?>();

            foreach (var part in Template.Split(new[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (part.StartsWith("--", StringComparison.Ordinal))
                {
                    LongName = part.Substring(2);
                }
                else if (part.StartsWith("-", StringComparison.Ordinal))
                {
                    var optName = part.Substring(1);

                    // If there is only one char and it is not an English letter, it is a symbol option (e.g. "-?")
                    if (optName.Length == 1 &&
                        !IsEnglishLetter(optName[0]))
                    {
                        SymbolName = optName;
                    }
                    else
                    {
                        ShortName = optName;
                    }
                }
                else if (part.StartsWith("<", StringComparison.Ordinal) &&
                         part.EndsWith(">", StringComparison.Ordinal))
                {
                    ValueName = part.Substring(1, part.Length - 2);
                }
                else if (optionType == CommandOptionType.MultipleValue &&
                         part.StartsWith("<", StringComparison.Ordinal) &&
                         part.EndsWith(">...", StringComparison.Ordinal))
                {
                    ValueName = part.Substring(1, part.Length - 5);
                }
                else
                {
                    throw new ArgumentException(Resources.InvalidTemplatePattern(template), nameof(template));
                }
            }

            if (string.IsNullOrEmpty(LongName) &&
                string.IsNullOrEmpty(ShortName) &&
                string.IsNullOrEmpty(SymbolName))
            {
                throw new ArgumentException(Resources.InvalidTemplatePattern(template), nameof(template));
            }
        }
 public static CommandOption <T> OptionalOption <T>(
     this CommandLineApplication application,
     string template,
     string description,
     object defaultValue,
     CommandOptionType optionType
     )
 {
     return(application.Option <T>(
                template,
                $"Optional: {description}, Default: {defaultValue}",
                optionType
                ));
 }
 public bool TryGetOptionType(
     Type clrType,
     ValueParserProvider valueParsers,
     out CommandOptionType optionType)
 {
     try
     {
         optionType = GetOptionType(clrType, valueParsers);
         return(true);
     }
     catch
     {
         optionType = default;
         return(false);
     }
 }
        public CommandOption Option(
            string template,
            string description,
            CommandOptionType optionType,
            Action <CommandOption> configuration,
            bool inherited)
        {
            CommandOption commandOption = new CommandOption(template, optionType)
            {
                Description = description,
                Inherited   = inherited
            };

            this.Options.Add(commandOption);
            configuration(commandOption);
            return(commandOption);
        }
Example #22
0
        public CommandOption(string template, CommandOptionType optionType)
        {
            Template = template;
            OptionType = optionType;
            Values = new List<string>();

            foreach (var part in Template.Split(new[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (part.StartsWith("--"))
                {
                    LongName = part.Substring(2);
                }
                else if (part.StartsWith("-"))
                {
                    var optName = part.Substring(1);

                    // If there is only one char and it is not an English letter, it is a symbol option (e.g. "-?")
                    if (optName.Length == 1 && !IsEnglishLetter(optName[0]))
                    {
                        SymbolName = optName;
                    }
                    else
                    {
                        ShortName = optName;
                    }
                }
                else if (part.StartsWith("<") && part.EndsWith(">"))
                {
                    ValueName = part.Substring(1, part.Length - 2);
                }
                else if (optionType == CommandOptionType.MultipleValue && part.StartsWith("<") && part.EndsWith(">..."))
                {
                    ValueName = part.Substring(1, part.Length - 5);
                }
                else
                { 
                    throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
                }
            }

            if (string.IsNullOrEmpty(LongName) && string.IsNullOrEmpty(ShortName) && string.IsNullOrEmpty(SymbolName))
            {
                throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
            }
        }
Example #23
0
        public CommandOption(string template, CommandOptionType optionType)
        {
#pragma warning disable 618
            Template = template;
#pragma warning restore 618
            OptionType = optionType;

            var separators = (optionType == CommandOptionType.SingleOrNoValue)
                ? new[] { ' ', '|', ':', '=', '[', ']' }
                : new[] { ' ', '|', ':', '=' };
            foreach (var part in template.Split(separators, StringSplitOptions.RemoveEmptyEntries))
            {
                if (part.StartsWith("--"))
                {
                    LongName = part.Substring(2);
                }
                else if (part.StartsWith("-"))
                {
                    var optName = part.Substring(1);

                    // If there is only one char and it is not an English letter, it is a symbol option (e.g. "-?")
                    if (optName.Length == 1 && !IsEnglishLetter(optName[0]))
                    {
                        SymbolName = optName;
                    }
                    else
                    {
                        ShortName = optName;
                    }
                }
                else if (part.StartsWith("<") && part.EndsWith(">"))
                {
                    ValueName = part.Substring(1, part.Length - 2);
                }
                else
                {
                    throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
                }
            }

            if (string.IsNullOrEmpty(LongName) && string.IsNullOrEmpty(ShortName) && string.IsNullOrEmpty(SymbolName))
            {
                throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
            }
        }
        private static void SetValueInfo(ArgumentInfo argumentInfo, CommandOptionType commandOptionType)
        {
            //set value
            IParameter option = null;

            switch (argumentInfo)
            {
            case CommandOptionInfo optionInfo:
                option = new CommandOption("--test", commandOptionType);
                break;

            case CommandParameterInfo parameterInfo:
                option = new CommandArgument();
                break;
            }

            argumentInfo.SetValue(option);
        }
Example #25
0
        public void Option(string template, string description, CommandOptionType optionType, Action <CommandOption> callback, Action <CommandOption> configuration = null)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            CommandOption option;

            if (configuration != null)
            {
                option = _app.Option(template, description, optionType, configuration);
            }
            else
            {
                option = _app.Option(template, description, optionType);
            }
            _callbacks.Add(() => callback(option));
        }
        public CommandOption(string template, CommandOptionType optionType)
        {
            Template   = template;
            OptionType = optionType;
            Values     = new List <string>();

            foreach (var part in Template.Split(new[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (part.StartsWith("--"))
                {
                    LongName = part.Substring(2);
                }
                else if (part.StartsWith("-"))
                {
                    var optName = part.Substring(1);
                    if (optName.Length == 1 && !IsEnglishLetter(optName[0]))
                    {
                        SymbolName = optName;
                    }
                    else
                    {
                        ShortName = optName;
                    }
                }
                else if (part.StartsWith("<") && part.EndsWith(">"))
                {
                    ValueName = part.Substring(1, part.Length - 2);
                }
                else if (optionType == CommandOptionType.MultipleValue && part.StartsWith("<") && part.EndsWith(">..."))
                {
                    ValueName = part.Substring(1, part.Length - 5);
                }
                else
                {
                    throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
                }
            }

            if (string.IsNullOrEmpty(LongName) && string.IsNullOrEmpty(ShortName) && string.IsNullOrEmpty(SymbolName))
            {
                throw new ArgumentException($"Invalid template pattern '{template}'", nameof(template));
            }
        }
Example #27
0
        public CommandOption(string template, CommandOptionType optionType)
        {
            this.Template   = template;
            this.OptionType = optionType;
            this.Values     = new List <string>();
            string template1 = this.Template;

            char[] separator = new char[2] {
                ' ', '|'
            };
            foreach (string str1 in template1.Split(separator, StringSplitOptions.RemoveEmptyEntries))
            {
                if (str1.StartsWith("--"))
                {
                    this.LongName = str1.Substring(2);
                }
                else if (str1.StartsWith("-"))
                {
                    string str2 = str1.Substring(1);
                    if (str2.Length == 1 && !this.IsEnglishLetter(str2[0]))
                    {
                        this.SymbolName = str2;
                    }
                    else
                    {
                        this.ShortName = str2;
                    }
                }
                else
                {
                    if (!str1.StartsWith("<") || !str1.EndsWith(">"))
                    {
                        throw new ArgumentException(string.Format("Invalid template pattern '{0}'", (object)template), nameof(template));
                    }
                    this.ValueName = str1.Substring(1, str1.Length - 2);
                }
            }
            if (string.IsNullOrEmpty(this.LongName) && string.IsNullOrEmpty(this.ShortName) && string.IsNullOrEmpty(this.SymbolName))
            {
                throw new ArgumentException(string.Format("Invalid template pattern '{0}'", (object)template), nameof(template));
            }
        }
Example #28
0
        public void TestParsing(string expected, string template, string fullName, CommandOptionType type, params string[] values)
        {
            // Arrange
            var option = new CommandOption(template, type);

            option.Values.AddRange(values.ToList());
            var optionList = new List <CommandOption> {
                option
            };

            // Act
            var provider = new CommandLineOptionsProvider(optionList);
            var builder  = new ConfigurationBuilder();
            var config   = provider.Build(builder);

            config.TryGet(fullName, out var actual);

            // Assert
            Assert.Equal(expected, actual);
        }
Example #29
0
        private InteractionDataOption GetArg(string name, CommandOptionType requested)
        {
            InteractionDataOption arg = _args[name];

            if (arg == null)
            {
                return(null);
            }

            if (arg.Type != CommandOptionType.Mentionable)
            {
                ValidateArgs(name, arg.Type, requested);
            }
            else if (requested != CommandOptionType.Role && requested != CommandOptionType.User)
            {
                throw new Exception($"Attempted to parse {name} role/user type to: {requested} which is not valid.");
            }

            return(arg);
        }
Example #30
0
        public CommandOption(string template, CommandOptionType optionType)
        {
            Template   = template;
            OptionType = optionType;
            Values     = new List <string>();

            foreach (var part in Template.Split(new[] { ' ', '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (part.StartsWith("--"))
                {
                    LongName = part.Substring(2);
                }
                else if (part.StartsWith("-"))
                {
                    var optName = part.Substring(1);

                    // If there is only one char and it is not an English letter, it is a symbol option (e.g. "-?")
                    if (optName.Length == 1 && !IsEnglishLetter(optName[0]))
                    {
                        SymbolName = optName;
                    }
                    else
                    {
                        ShortName = optName;
                    }
                }
                else if (part.StartsWith("<") && part.EndsWith(">"))
                {
                    ValueName = part.Substring(1, part.Length - 2);
                }
                else
                {
                    throw new Exception("TODO: invalid template pattern " + template);
                }
            }

            if (string.IsNullOrEmpty(LongName) && string.IsNullOrEmpty(ShortName) && string.IsNullOrEmpty(SymbolName))
            {
                throw new Exception("TODO: invalid template pattern " + template);
            }
        }
        internal CommandOption Option(string template, string description, CommandOptionType optionType,
                                      Action <CommandOption> configuration, bool inherited,
                                      string typeDisplayName, object defaultValue, bool multiple, List <string> allowedValues
                                      )
        {
            var option = new CommandOption(template, optionType)
            {
                Description     = description,
                Inherited       = inherited,
                DefaultValue    = defaultValue,
                TypeDisplayName = typeDisplayName,
                Multiple        = multiple,
                AllowedValues   = allowedValues
            };
            bool optionAdded = Options.Add(option);

            if (!optionAdded)
            {
                throw new AppRunnerException($"Option with template `{option.Template}` already added");
            }
            configuration(option);
            return(option);
        }
        /// <summary>
        /// Shortcut to create commands
        /// </summary>
        /// <param name="app">Command line application object</param>
        /// <param name="optionName">Name for option</param>
        /// <param name="shortName">Short name for option</param>
        /// <param name="description">Description for help</param>
        /// <param name="optionType">Type of option</param>
        /// <returns></returns>
        internal static CommandOption AddOption(this CommandLineApplication app, string optionName, string shortName,
                                                string description, CommandOptionType optionType)
        {
            var argSuffix = optionType == CommandOptionType.MultipleValue ? "..." : null;
            var argString = optionType == CommandOptionType.SingleValue ? null : $" <arg>{argSuffix}";

            switch (optionType)
            {
            case CommandOptionType.MultipleValue:
            case CommandOptionType.SingleValue:
                return
                    (app.Option(string.IsNullOrWhiteSpace(shortName) ? $"-{shortName}|--{optionName}{argString}" : $"--{optionName}{argString}",
                                description, optionType));

            case CommandOptionType.NoValue:
                return
                    (app.Option(string.IsNullOrWhiteSpace(shortName) ? $"-{shortName}|--{optionName}" : $"--{optionName}",
                                description, optionType));

            default:
                return(null);
            }
        }
 public CommandOption Option(string template, string description, CommandOptionType optionType)
 {
     return Option(template, description, optionType, _ => { });
 }
Example #34
0
 public CommandOption Option(string template, string description, CommandOptionType optionType)
 {
     return(Option(template, description, optionType, _ => { }));
 }