ParameterInfo ICommandExecutorImpl.SetOptionParameter(string key, CommandPart commandPart, CommandAttribute command, SortedList <int, object> parameters) { if (!command.Options.TryGetValue(key, out var option)) { throw new BadCommandException(command, $"Unknown option `{key}`"); } var valueString = _parser.GetString(commandPart.Value); var value = _valueConverter.GetValue(valueString, option.Parameter.ParameterType); if (parameters.TryGetValue(option.Parameter.Position, out var existingValue)) { if (!option.Parameter.ParameterType.IsArray) { throw new BadCommandException(command, $"Option `{key}` specified too many times"); } parameters[option.Parameter.Position] = ((Array)existingValue).ExtendAndAddArray((Array)value); } else { parameters.Add(option.Parameter.Position, value); } return(option.Parameter); }
bool ICommandParserImplementation.TryGetCommandPart(out CommandPart commandPart) { commandPart = default; _impl.ConsumeWhitespace(); if (_impl.Peek() == '\0') { return(false); } if (_impl.ConsumeIf('-')) { if (_impl.ConsumeIf('-')) { commandPart = _impl.GetLongFormArgument(); } else { commandPart = _impl.GetShortFormArgument(); } } else { commandPart = _impl.GetValue(); } return(true); }
bool ICommandExecutorImpl.TrySetGlobalOption(string key, CommandPart commandPart, GlobalOptionsWrapper globalOptions) { if (globalOptions?.Options == null || !globalOptions.Options.TryGetValue(key, out var optionAtt)) { return(false); } var value = _parser.GetString(commandPart.Value); var globalvalue = _valueConverter.GetValue(value, optionAtt.Property.PropertyType); optionAtt.Property.SetValue(globalOptions.GlobalOptions, globalvalue); return(true); }
bool ICommandHelper.TryShowHelpOrVersion(CommandPart commandPart, VerbAttribute verb, string key, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions) { if ((commandPart.IsShortForm && key[0] == options.VersionShortForm) || (!commandPart.IsShortForm && key.Equals(options.VersionLongForm, StringComparison.OrdinalIgnoreCase))) { _commandHelper.WriteVersion(options); return(true); } if ((commandPart.IsShortForm && key[0] == options.HelpShortForm) || (!commandPart.IsShortForm && key.Equals(options.HelpLongForm, StringComparison.OrdinalIgnoreCase))) { _commandHelper.WriteVerbHelp(verb, options, globalOptions); return(true); } return(false); }