#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters /// <summary> /// Constructor that requires an explicit implementation of /// <see cref="ILoopClient"/>. /// </summary> /// <param name="commandType">Type that defines syntax for commands.</param> /// <param name="loopClient">The client to use.</param> /// <param name="argSetAttribute">Optionally provides attribute info /// for the argument set that will be dynamically created for this loop.</param> /// <param name="options">Optionally provides additional options for /// this loop's execution.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="commandType" /> /// is null.</exception> public Loop(Type commandType, ILoopClient loopClient, ArgumentSetAttribute argSetAttribute = null, LoopOptions options = null) { if (commandType == null) { throw new ArgumentNullException(nameof(commandType)); } _client = loopClient ?? throw new ArgumentNullException(nameof(loopClient)); _client.TokenCompleter = new TokenCompleter(this); _options = options?.DeepClone() ?? new LoopOptions(); _options.ParserOptions.DisplayUsageInfoOnError = false; _options.ParserOptions.Reporter = error => _client.OnError(error.ToString().TrimEnd()); var inputConfigurer = _options.ParserOptions.ServiceConfigurer; _options.ParserOptions.ServiceConfigurer = collection => ConfigureServices(collection, inputConfigurer); var constructedType = ConstructCommandTypeFactory(commandType, out _objectFactory); _argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( constructedType, attribute: argSetAttribute, serviceConfigurer: _options.ParserOptions.ServiceConfigurer); }
/// <summary> /// Formats a parsed set of arguments back into tokenized string form. /// </summary> /// <typeparam name="T">Type of the parsed arguments object.</typeparam> /// <param name="value">The parsed argument set.</param> /// <param name="options">Optionally provides parser options.</param> /// <returns>The tokenized string.</returns> public static IEnumerable <string> Format <T>(T value, CommandLineParserOptions options) { var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( typeof(T), attribute: null, serviceConfigurer: options?.ServiceConfigurer); // N.B. We intentionally convert the arguments enumeration to a list, // as we're expecting to mutate it in the loop. foreach (var arg in argSet.AllArguments.ToList()) { if (arg.GetValue(value) is IArgumentProvider argProvider) { var definingType = argProvider.GetTypeDefiningArguments(); if (definingType != null) { AttributeBasedArgumentDefinitionFactory.AddToArgumentSet(argSet, definingType, fixedDestination: argProvider.GetDestinationObject(), containingArgument: arg, serviceConfigurer: options?.ServiceConfigurer); } } } return(OrderArgumentsByContainer(argSet.AllArguments) .Select(arg => new { Argument = arg, Value = arg.GetValue(value) }) .Where(argAndValue => (argAndValue.Value != null) && !argAndValue.Value.Equals(argAndValue.Argument.DefaultValue)) .SelectMany(argAndValue => argAndValue.Argument.Format(argAndValue.Value)) .Where(formattedValue => !string.IsNullOrWhiteSpace(formattedValue))); }
/// <summary> /// Generate possible completions for the specified set of command-line /// tokens. /// </summary> /// <param name="type">Type of the parsed arguments object.</param> /// <param name="tokens">The tokens.</param> /// <param name="indexOfTokenToComplete">Index of the token to complete. /// </param> /// <param name="options">Parsing options.</param> /// <param name="destObjectFactory">If non-null, provides a factory /// function that can be used to create an object suitable to being /// filled out by this parser instance.</param> /// <returns>The candidate completions for the specified token. /// </returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="type"/> /// or <paramref name="tokens"/> is null.</exception> public static IEnumerable <string> GetCompletions(Type type, IEnumerable <string> tokens, int indexOfTokenToComplete, CommandLineParserOptions options, Func <object> destObjectFactory) { var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( type, attribute: null, serviceConfigurer: options?.ServiceConfigurer); return(GetCompletions(argSet, tokens, indexOfTokenToComplete, options, destObjectFactory)); }
public static ColoredMultistring GetUsageInfo( Type type, ArgumentSetHelpOptions options = null, object defaultValues = null, ServiceConfigurer serviceConfigurer = null) { var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( type, attribute: null, defaultValues: defaultValues, serviceConfigurer: serviceConfigurer); return(GetUsageInfo(argSet, options, null)); }
/// <summary> /// Tries to parse the given string arguments into the provided instance of <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">Type of the destination object; this type should use /// appropriate Neuralia.NClap attributes to annotate and define options.</typeparam> /// <param name="arguments">The string arguments to parse.</param> /// <param name="destination">The object to parse into.</param> /// <param name="options">Options describing how to parse.</param> /// <returns>True on success; false otherwise.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="arguments"/> or /// <paramref name="destination" /> is null.</exception> public static bool TryParse <T>(IEnumerable <string> arguments, T destination, CommandLineParserOptions options) where T : class { if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } if (destination == null) { throw new ArgumentNullException(nameof(arguments)); } var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( destination.GetType(), attribute: null, defaultValues: destination, serviceConfigurer: options?.ServiceConfigurer); return(TryParse(argSet, arguments, options, destination)); }
/// <summary> /// Tries to parse the given string arguments into a new instance of <typeparamref name="T"/>. /// </summary> /// <typeparam name="T">Type of the destination object; this type should use /// appropriate Neuralia.NClap attributes to annotate and define options.</typeparam> /// <param name="arguments">The string arguments to parse.</param> /// <param name="options">Options describing how to parse.</param> /// <param name="result">On success, returns the constructed result object.</param> /// <returns>True on success; false otherwise.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="arguments"/> /// is null.</exception> public static bool TryParse <T>(IEnumerable <string> arguments, CommandLineParserOptions options, out T result) where T : class, new() { if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } var destination = new T(); var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( destination.GetType(), attribute: null, defaultValues: destination, serviceConfigurer: options?.ServiceConfigurer); if (!TryParse(argSet, arguments, options, destination)) { result = null; return(false); } result = destination; return(true); }
private ArgumentSetDefinition CreateArgSet() => AttributeBasedArgumentDefinitionFactory.CreateArgumentSet( typeof(CommandGroup <TCommandType>), _argSetAttrib, serviceConfigurer: _parserOptions.ServiceConfigurer);
private ArgumentSetDefinition GetArgumentSetFor(Type type) => AttributeBasedArgumentDefinitionFactory.CreateArgumentSet(type);
private ArgumentDefinition GetArgumentDefinition(ArgumentSetAttribute attrib = null) { var argSet = AttributeBasedArgumentDefinitionFactory.CreateArgumentSet(typeof(TestArguments), attrib); return(argSet.AllArguments.Single()); }