Пример #1
0
        internal static FailedArgumentConversionArityResult?Validate(
            SymbolResult symbolResult,
            IArgument argument,
            int minimumNumberOfValues,
            int maximumNumberOfValues
            )
        {
            var argumentResult = symbolResult switch
            {
                ArgumentResult a => a,
                _ => symbolResult.Root !.FindResultFor(argument)
            };

            var tokenCount = argumentResult?.Tokens.Count ?? 0;

            if (tokenCount < minimumNumberOfValues)
            {
                if (symbolResult !.UseDefaultValueFor(argument))
                {
                    return(null);
                }

                return(new MissingArgumentConversionResult(
                           argument,
                           symbolResult.ValidationMessages.RequiredArgumentMissing(symbolResult)
                           ));
            }

            if (tokenCount > maximumNumberOfValues)
            {
                return(new TooManyArgumentsConversionResult(
                           argument,
                           symbolResult !.ValidationMessages.ExpectsOneArgument(symbolResult)
                           ));
            }

            return(null);
        }
Пример #2
0
 public CommandLineConfiguration(
     RootCommand rootCommand,
     IReadOnlyCollection <char> argumentDelimiters = null,
     IReadOnlyCollection <string> prefixes         = null,
     bool enablePosixBundling                  = true,
     bool enablePositionalOptions              = false,
     ValidationMessages validationMessages     = null,
     ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
     IReadOnlyCollection <InvocationMiddleware> middlewarePipeline = null)
     : this(
         new[] { rootCommand },
         argumentDelimiters,
         prefixes,
         enablePosixBundling,
         enablePositionalOptions,
         validationMessages,
         responseFileHandling,
         middlewarePipeline)
 {
     if (rootCommand == null)
     {
         throw new ArgumentNullException(nameof(rootCommand));
     }
 }
        public CommandLineConfiguration(
            IReadOnlyCollection <Symbol> symbols,
            IReadOnlyCollection <char> argumentDelimiters = null,
            IReadOnlyCollection <string> prefixes         = null,
            bool enablePosixBundling = true,
            bool enableDirectives    = true,
            ValidationMessages validationMessages     = null,
            ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
            IReadOnlyCollection <InvocationMiddleware> middlewarePipeline = null,
            Func <BindingContext, IHelpBuilder> helpBuilderFactory        = null)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (!symbols.Any())
            {
                throw new ArgumentException("You must specify at least one option or command.");
            }

            ArgumentDelimiters = argumentDelimiters ?? new[] { ':', '=' };

            foreach (var symbol in symbols)
            {
                foreach (var alias in symbol.RawAliases)
                {
                    foreach (var delimiter in ArgumentDelimiters)
                    {
                        if (alias.Contains(delimiter))
                        {
                            throw new SymbolCannotContainDelimiterArgumentException(delimiter);
                        }
                    }
                }
            }

            if (symbols.Count == 1 &&
                symbols.Single() is Command rootCommand)
            {
                RootCommand = rootCommand;
            }
            else
            {
                // reuse existing auto-generated root command, if one is present, to prevent repeated mutations
                rootCommand = symbols.SelectMany(s => s.Parents)
                              .OfType <RootCommand>()
                              .FirstOrDefault();

                if (rootCommand == null)
                {
                    rootCommand = new RootCommand();

                    foreach (var symbol in symbols)
                    {
                        rootCommand.Add(symbol);
                    }
                }

                RootCommand = rootCommand;
            }

            _symbols.Add(RootCommand);

            EnablePosixBundling  = enablePosixBundling;
            EnableDirectives     = enableDirectives;
            ValidationMessages   = validationMessages ?? ValidationMessages.Instance;
            ResponseFileHandling = responseFileHandling;
            _middlewarePipeline  = middlewarePipeline;
            _helpBuilderFactory  = helpBuilderFactory;
            Prefixes             = prefixes;

            if (prefixes?.Count > 0)
            {
                foreach (var symbol in symbols)
                {
                    if (symbol is Option option)
                    {
                        foreach (var alias in option.RawAliases.ToList())
                        {
                            if (!prefixes.All(prefix => alias.StartsWith(prefix)))
                            {
                                foreach (var prefix in prefixes)
                                {
                                    option.AddAlias(prefix + alias);
                                }
                            }
                        }
                    }
                }
            }
        }
        public CommandLineConfiguration(
            IReadOnlyCollection <Symbol> symbols,
            IReadOnlyCollection <char> argumentDelimiters = null,
            IReadOnlyCollection <string> prefixes         = null,
            bool enablePosixBundling                  = true,
            bool enablePositionalOptions              = false,
            ValidationMessages validationMessages     = null,
            ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
            IReadOnlyCollection <InvocationMiddleware> middlewarePipeline = null,
            Func <BindingContext, IHelpBuilder> helpBuilderFactory        = null)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (!symbols.Any())
            {
                throw new ArgumentException("You must specify at least one option or command.");
            }

            ArgumentDelimiters = argumentDelimiters ?? new[] { ':', '=' };

            foreach (var symbol in symbols)
            {
                foreach (var alias in symbol.RawAliases)
                {
                    foreach (var delimiter in ArgumentDelimiters)
                    {
                        if (alias.Contains(delimiter))
                        {
                            throw new SymbolCannotContainDelimiterArgumentException(delimiter);
                        }
                    }
                }
            }

            if (symbols.Count == 1 &&
                symbols.Single() is Command rootCommand)
            {
                RootCommand = rootCommand;
            }
            else
            {
                RootCommand = new RootCommand(symbols: symbols);
            }

            _symbols.Add(RootCommand);

            EnablePosixBundling     = enablePosixBundling;
            EnablePositionalOptions = enablePositionalOptions;
            ValidationMessages      = validationMessages ?? ValidationMessages.Instance;
            ResponseFileHandling    = responseFileHandling;
            _middlewarePipeline     = middlewarePipeline;
            _helpBuilderFactory     = helpBuilderFactory;
            Prefixes = prefixes;

            if (prefixes?.Count > 0)
            {
                foreach (var symbol in symbols)
                {
                    foreach (var alias in symbol.RawAliases.ToList())
                    {
                        if (!prefixes.All(prefix => alias.StartsWith(prefix)))
                        {
                            foreach (var prefix in prefixes)
                            {
                                symbol.AddAlias(prefix + alias);
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
        public CommandLineConfiguration(
            IReadOnlyCollection <Symbol> symbols,
            IReadOnlyCollection <char> argumentDelimiters = null,
            bool enablePosixBundling = true,
            bool enableDirectives    = true,
            ValidationMessages validationMessages     = null,
            ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
            IReadOnlyCollection <InvocationMiddleware> middlewarePipeline = null,
            Func <BindingContext, IHelpBuilder> helpBuilderFactory        = null)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (!symbols.Any())
            {
                throw new ArgumentException("You must specify at least one option or command.");
            }

            ArgumentDelimiters = argumentDelimiters ?? new[] { ':', '=' };

            foreach (var symbol in symbols)
            {
                foreach (var alias in symbol.RawAliases)
                {
                    foreach (var delimiter in ArgumentDelimiters)
                    {
                        if (alias.Contains(delimiter))
                        {
                            throw new ArgumentException($"{symbol.GetType().Name} \"{alias}\" is not allowed to contain a delimiter but it contains \"{delimiter}\"");
                        }
                    }
                }
            }

            if (symbols.Count == 1 &&
                symbols.Single() is Command rootCommand)
            {
                RootCommand = rootCommand;
            }
            else
            {
                // reuse existing auto-generated root command, if one is present, to prevent repeated mutations
                rootCommand = symbols.SelectMany(s => s.Parents)
                              .OfType <RootCommand>()
                              .FirstOrDefault();

                if (rootCommand == null)
                {
                    rootCommand = new RootCommand();

                    foreach (var symbol in symbols)
                    {
                        rootCommand.Add(symbol);
                    }
                }

                RootCommand = rootCommand;
            }

            _symbols.Add(RootCommand);

            AddGlobalOptionsToChildren(rootCommand);

            EnablePosixBundling  = enablePosixBundling;
            EnableDirectives     = enableDirectives;
            ValidationMessages   = validationMessages ?? ValidationMessages.Instance;
            ResponseFileHandling = responseFileHandling;
            _middlewarePipeline  = middlewarePipeline;
            _helpBuilderFactory  = helpBuilderFactory;
        }
        public CommandLineConfiguration(
            IReadOnlyCollection <Symbol> symbols,
            IReadOnlyCollection <char> argumentDelimiters = null,
            IReadOnlyCollection <string> prefixes         = null,
            bool enablePosixBundling                  = true,
            bool enablePositionalOptions              = false,
            ValidationMessages validationMessages     = null,
            ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated,
            IReadOnlyCollection <InvocationMiddleware> middlewarePipeline = null,
            IHelpBuilderFactory helpBuilderFactory = null)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException(nameof(symbols));
            }

            if (!symbols.Any())
            {
                throw new ArgumentException("You must specify at least one option or command.");
            }

            ArgumentDelimiters = argumentDelimiters ?? new[] { ':', '=', ' ' };

            foreach (var symbol in symbols)
            {
                foreach (var childSymbol in symbol.Children.FlattenBreadthFirst <Symbol>(o => o.Children))
                {
                    if (childSymbol.Argument.Arity.MaximumNumberOfArguments != 0 && string.IsNullOrEmpty(childSymbol.Argument.Name))
                    {
                        throw new ArgumentException(
                                  ValidationMessages.RequiredArgumentNameMissing(childSymbol.Aliases.FirstOrDefault()));
                    }
                }

                foreach (var alias in symbol.RawAliases)
                {
                    foreach (var delimiter in ArgumentDelimiters)
                    {
                        if (alias.Contains(delimiter))
                        {
                            throw new SymbolCannotContainDelimiterArgumentException(delimiter);
                        }
                    }
                }
            }

            if (symbols.Count == 1 &&
                symbols.Single() is Command rootCommand)
            {
                RootCommand = rootCommand;
            }
            else
            {
                RootCommand = new RootCommand(symbols: symbols);
            }

            _symbols.Add(RootCommand);

            EnablePosixBundling     = enablePosixBundling;
            EnablePositionalOptions = enablePositionalOptions;
            ValidationMessages      = validationMessages ?? ValidationMessages.Instance;
            ResponseFileHandling    = responseFileHandling;
            _middlewarePipeline     = middlewarePipeline;
            _helpBuilderFactory     = helpBuilderFactory;
            Prefixes = prefixes;

            if (prefixes?.Count > 0)
            {
                foreach (var symbol in symbols)
                {
                    foreach (var alias in symbol.RawAliases.ToList())
                    {
                        if (!prefixes.All(prefix => alias.StartsWith(prefix)))
                        {
                            foreach (var prefix in prefixes)
                            {
                                symbol.AddAlias(prefix + alias);
                            }
                        }
                    }
                }
            }
        }