public static CommandLineBuilder ParseResponseFileAs( this CommandLineBuilder builder, ResponseFileHandling responseFileHandling) { builder.ResponseFileHandling = responseFileHandling; return(builder); }
public static ParseOptions Create( string basePath = ".", ResponseFileHandling responseFileHandling = ResponseFileHandling.MultipleArgumentsPerLine, ArgumentHandler?argumentHandler = null) => new ParseOptions( basePath, responseFileHandling, argumentHandler);
ParseOptions( string basePath, ResponseFileHandling responseFileHandling, ArgumentHandler?argumentHandler) { BasePath = basePath ?? "."; ResponseFileHandling = responseFileHandling; ArgumentHandler = argumentHandler; }
public static IList <string> Parse(string filePath, ResponseFileHandling handling) { return(handling switch { ResponseFileHandling.Disabled => new[] { filePath }, ResponseFileHandling.ParseArgsAsSpaceSeparated => ParseAsSpaceSeparated(filePath), ResponseFileHandling.ParseArgsAsLineSeparated => ParseAsLineSeparated(filePath), _ => throw new ArgumentOutOfRangeException(nameof(handling)), });
/// <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, 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.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 ?? ValidationMessages.Instance; ResponseFileHandling = responseFileHandling; Middleware = middlewarePipeline ?? new List <InvocationMiddleware>(); HelpBuilderFactory = helpBuilderFactory ?? (context => new HelpBuilder(context.Console)); }
private List <string> ParseResponseFile(ResponseFileHandling options, params string[] responseFileLines) { var rsp = CreateResponseFile(responseFileLines); var app = new CommandLineApplication { ResponseFileHandling = options }; var args = app.Argument("Words", "more words", multipleValues: true); Assert.Equal(0, app.Execute("@" + rsp)); return(args.Values); }
public static IList <string> Parse(string filePath, ResponseFileHandling handling) { switch (handling) { case ResponseFileHandling.Disabled: return(new[] { filePath }); case ResponseFileHandling.ParseArgsAsSpaceSeparated: return(ParseAsSpaceSeparated(filePath)); case ResponseFileHandling.ParseArgsAsLineSeparated: return(ParseAsLineSeparated(filePath)); default: throw new ArgumentOutOfRangeException(nameof(handling)); } }
/// <summary> /// Initializes a new instance of the CommandLineConfiguration class. /// </summary> /// <param name="command">The root command for the parser.</param> /// <param name="enablePosixBundling"><see langword="true"/> to enable POSIX bundling; otherwise, <see langword="false"/>.</param> /// <param name="enableDirectives"><see langword="true"/> to enable directive parsing; otherwise, <see langword="false"/>.</param> /// <param name="enableLegacyDoubleDashBehavior">Enables the legacy behavior of the <c>--</c> token, which is to ignore parsing of subsequent tokens and place them in the <see cref="ParseResult.UnparsedTokens"/> list.</param> /// <param name="resources">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> public CommandLineConfiguration( Command command, bool enablePosixBundling = true, bool enableDirectives = true, bool enableLegacyDoubleDashBehavior = false, LocalizationResources?resources = null, ResponseFileHandling responseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated, IReadOnlyList <InvocationMiddleware>?middlewarePipeline = null, Func <BindingContext, HelpBuilder>?helpBuilderFactory = null) { RootCommand = command ?? throw new ArgumentNullException(nameof(command)); EnableLegacyDoubleDashBehavior = enableLegacyDoubleDashBehavior; EnablePosixBundling = enablePosixBundling; EnableDirectives = enableDirectives; LocalizationResources = resources ?? LocalizationResources.Instance; ResponseFileHandling = responseFileHandling; Middleware = middlewarePipeline ?? Array.Empty <InvocationMiddleware>(); _helpBuilderFactory = helpBuilderFactory; }
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 ParseOptions WithResponseFileHandling(ResponseFileHandling responseFileHandling) => new ParseOptions( BasePath, responseFileHandling, ArgumentHandler);
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); } } } } } }