/// <summary><see cref="IArgumentArity.Minimum"/> gt; 0</summary>
 public static bool RequiresAtLeastOne(this IArgumentArity arity) => arity.Minimum > 0;
 /// <summary><see cref="IArgumentArity.Maximum"/> == 0</summary>
 public static bool AllowsNone(this IArgumentArity arity) => arity.Maximum == 0;
 /// <summary><see cref="IArgumentArity.Maximum"/> gt; 1</summary>
 public static bool AllowsMany(this IArgumentArity arity) => arity.Maximum > 1;
 /// <summary><see cref="IArgumentArity.Minimum"/> == 1 == <see cref="IArgumentArity.Maximum"/></summary>
 public static bool RequiresExactlyOne(this IArgumentArity arity) => arity.Minimum == 1 && arity.Maximum == 1;
Ejemplo n.º 5
0
 /// <summary><see cref="IArgumentArity.Maximum"/> &gt;= 1</summary>
 public static bool AllowsOneOrMore(this IArgumentArity arity) => arity.Maximum >= 1;
Ejemplo n.º 6
0
 /// <summary>
 /// <see cref="IArgumentArity.Maximum"/> == <see cref="ArgumentArity.Unlimited"/> (<see cref="int.MaxValue"/>).
 /// e.g. <see cref="ArgumentArity.ZeroOrMore"/>, <see cref="ArgumentArity.OneOrMore"/>
 /// </summary>
 public static bool AllowsUnlimited(this IArgumentArity arity) => arity.Maximum == ArgumentArity.Unlimited;
Ejemplo n.º 7
0
 /// <summary>
 /// <see cref="IArgumentArity.Maximum"/> == 0.
 /// e.g. <see cref="ArgumentArity.Zero"/>
 /// </summary>
 public static bool RequiresNone(this IArgumentArity arity) => arity.Maximum == 0;
Ejemplo n.º 8
0
        private void BuildCommands(CommandLineBuilder rootBuilder, Type type)
        {
            Command command = null;

            var baseAttributes = (BaseAttribute[])type.GetCustomAttributes(typeof(BaseAttribute), inherit: false);

            foreach (BaseAttribute baseAttribute in baseAttributes)
            {
                if (baseAttribute is CommandAttribute commandAttribute && IsValidPlatform(commandAttribute))
                {
                    command = new Command(commandAttribute.Name, commandAttribute.Help);
                    var properties = new List <(PropertyInfo, Option)>();
                    var arguments  = new List <(PropertyInfo, Argument)>();

                    foreach (PropertyInfo property in type.GetProperties().Where(p => p.CanWrite))
                    {
                        var argumentAttribute = (ArgumentAttribute)property.GetCustomAttributes(typeof(ArgumentAttribute), inherit: false).SingleOrDefault();
                        if (argumentAttribute != null)
                        {
                            IArgumentArity arity = property.PropertyType.IsArray ? ArgumentArity.ZeroOrMore : ArgumentArity.ZeroOrOne;

                            var argument = new Argument {
                                Name         = argumentAttribute.Name ?? property.Name.ToLowerInvariant(),
                                Description  = argumentAttribute.Help,
                                ArgumentType = property.PropertyType,
                                Arity        = arity
                            };
                            command.AddArgument(argument);
                            arguments.Add((property, argument));
                        }
                        else
                        {
                            var optionAttribute = (OptionAttribute)property.GetCustomAttributes(typeof(OptionAttribute), inherit: false).SingleOrDefault();
                            if (optionAttribute != null)
                            {
                                var option = new Option(optionAttribute.Name ?? BuildAlias(property.Name), optionAttribute.Help)
                                {
                                    Argument = new Argument {
                                        ArgumentType = property.PropertyType
                                    }
                                };
                                command.AddOption(option);
                                properties.Add((property, option));

                                foreach (var optionAliasAttribute in (OptionAliasAttribute[])property.GetCustomAttributes(typeof(OptionAliasAttribute), inherit: false))
                                {
                                    option.AddAlias(optionAliasAttribute.Name);
                                }
                            }
                            else
                            {
                                // If not an option, add as just a settable properties
                                properties.Add((property, null));
                            }
                        }
                    }

                    var handler = new Handler(this, commandAttribute.AliasExpansion, arguments, properties, type);
                    _commandHandlers.Add(command.Name, handler);
                    command.Handler = handler;
                    rootBuilder.AddCommand(command);
                }

                if (baseAttribute is CommandAliasAttribute commandAliasAttribute && IsValidPlatform(commandAliasAttribute))
                {
                    if (command == null)
                    {
                        throw new ArgumentException($"No previous CommandAttribute for this CommandAliasAttribute: {type.Name}");
                    }
                    command.AddAlias(commandAliasAttribute.Name);
                }
            }
        }