コード例 #1
0
        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(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);
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
 public static TBuilder UseHelpBuilderFactory <TBuilder>(this TBuilder builder, IHelpBuilderFactory helpBuilderFactory)
     where TBuilder : CommandLineBuilder
 {
     if (builder == null)
     {
         throw new ArgumentNullException(nameof(builder));
     }
     builder.HelpBuilderFactory = helpBuilderFactory;
     return(builder);
 }