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); } } } } } }
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 is null) { throw new ArgumentNullException(nameof(symbols)); } if (!symbols.Any()) { throw new ArgumentException("You must specify at least one option or command."); } if (argumentDelimiters is null) { ArgumentDelimitersInternal = new HashSet <char> { ':', '=' }; } else { ArgumentDelimitersInternal = new HashSet <char>(argumentDelimiters); } 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?parentRootCommand = symbols.SelectMany(s => s.Parents) .OfType <RootCommand>() .FirstOrDefault(); if (parentRootCommand is null) { parentRootCommand = new RootCommand(); foreach (var symbol in symbols) { parentRootCommand.Add(symbol); } } RootCommand = rootCommand = parentRootCommand; } _symbols.Add(RootCommand); AddGlobalOptionsToChildren(rootCommand); EnablePosixBundling = enablePosixBundling; EnableDirectives = enableDirectives; ValidationMessages = validationMessages ?? ValidationMessages.Instance; ResponseFileHandling = responseFileHandling; _middlewarePipeline = middlewarePipeline ?? new List <InvocationMiddleware>(); _helpBuilderFactory = helpBuilderFactory ?? (context => new HelpBuilder(context.Console)); }
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); } } } } } }
/// <summary> /// Initializes a new instance of the CommandLineConfiguration class. /// </summary> /// <param name="symbols">The symbols to parse.</param> /// <param name="enablePosixBundling"><c>true</c> to enable POSIX bundling; otherwise, <c>false</c>.</param> /// <param name="enableDirectives"><c>true</c> to enable directive parsing; otherwise, <c>false</c>.</param> /// <param name="validationMessages">Provide custom validation messages.</param> /// <param name="responseFileHandling">One of the enumeration values that specifies how response files (.rsp) are handled.</param> /// <param name="middlewarePipeline">Provide a custom middleware pipeline.</param> /// <param name="helpBuilderFactory">Provide a custom help builder.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="symbols"/> is null.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="symbols"/> does not contain at least one option or command.</exception> public CommandLineConfiguration( IReadOnlyList <Symbol> symbols, bool enablePosixBundling = true, bool enableDirectives = true, Resources?validationMessages = null, ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated, IReadOnlyCollection <InvocationMiddleware>?middlewarePipeline = null, Func <BindingContext, IHelpBuilder>?helpBuilderFactory = null, Action <IHelpBuilder>?configureHelp = null ) { if (symbols is null) { throw new ArgumentNullException(nameof(symbols)); } if (symbols.Count == 0) { throw new ArgumentException("You must specify at least one option or command."); } if (symbols.Count == 1 && symbols[0] is Command rootCommand) { RootCommand = rootCommand; } else { // Reuse existing auto-generated root command, if one is present, to prevent repeated mutations RootCommand?parentRootCommand = symbols .SelectMany(s => s.Parents) .OfType <RootCommand>() .FirstOrDefault(); if (parentRootCommand is null) { parentRootCommand = new RootCommand(); foreach (var symbol in symbols) { parentRootCommand.Add(symbol); } } RootCommand = rootCommand = parentRootCommand; } _symbols.Add(RootCommand); AddGlobalOptionsToChildren(rootCommand); EnablePosixBundling = enablePosixBundling; EnableDirectives = enableDirectives; ValidationMessages = validationMessages ?? Resources.Instance; ResponseFileHandling = responseFileHandling; Middleware = middlewarePipeline ?? new List <InvocationMiddleware>(); HelpBuilderFactory = helpBuilderFactory ?? ( context => { int maxWidth = int.MaxValue; if (context.Console is SystemConsole systemConsole) { maxWidth = systemConsole.GetWindowWidth(); } return(new HelpBuilder(context.Console, maxWidth)); } ); if (configureHelp != null) { var factory = HelpBuilderFactory; HelpBuilderFactory = context => { IHelpBuilder helpBuilder = factory(context); configureHelp(helpBuilder); return(helpBuilder); }; } }