private CommandArgs ReadCommandArguments(CommandConfig commandConfig, ArrayReader <string> argumentsReader) { var options = commandConfig.Options; var optionValues = new Dictionary <string, ArrayAggregator>(); var parameterSeries = commandConfig.ParameterSeries; var parameters = new ArrayReader <Parameter>(commandConfig.Parameters.ToArray()); var commandArgs = InitDefaultCommandArgs(commandConfig); while (argumentsReader.HasData()) { var arg = argumentsReader.Read(); if (!ReadOpt(options, commandArgs, optionValues, argumentsReader, arg) && !ReadParam(commandArgs, parameters, arg) && !ReadParamSeries(parameterSeries, commandArgs, arg)) { throw new TooManyParametersException(arg); } } foreach (var(optionName, optionValue) in optionValues) { commandArgs.AddOptionValueProvider( optionName, new ConstValueProvider( optionValue.GetArray( options.Single(o => o.Name == optionName).ValueType) ) ); } return(commandArgs); }
private static CommandConfig GetSubCommandConfig(CommandConfig commandConfig, string commandName) { return(commandConfig .Commands .FirstOrDefault(command => command.Name == commandName) ?.GetCommandConfig(commandConfig)); }
private void InitBaseOptions(ref CommandConfig commandConfig) { if (commandConfig.Options.All(a => a.Name != _cliConfig.HelpConfig.Flag)) { commandConfig.Options.Add( new Flag { Name = _cliConfig.HelpConfig.Flag, Abr = _cliConfig.HelpConfig.Abr, Description = _cliConfig.HelpConfig.Description, DefaultValue = false } ); } if (_cliConfig.VersionConfig.Enabled && commandConfig.Options.All(a => a.Name != _cliConfig.VersionConfig.Flag)) { commandConfig.Options.Add( new Flag { Name = _cliConfig.VersionConfig.Flag, Abr = _cliConfig.VersionConfig.Abr, Description = _cliConfig.VersionConfig.Description, DefaultValue = false } ); } }
public PrintVersionCommandExecutionContext(CliConfig cliConfig, CommandConfig commandConfig) : base((commandArgs, output) => { output.WriteLine(cliConfig.Name + " Version: " + Assembly.GetEntryAssembly()?.GetName().Version); return(0); }, null) { }
private List <PrefixedDefaultValueArgument> GetCommandOptions(CommandConfig parentConfig) { var commandOptions = new List <PrefixedDefaultValueArgument>(); commandOptions.AddRange(_options); if (parentConfig != null) { commandOptions.AddRange(parentConfig.Options.Where(f => f.IsPersistent)); } return(commandOptions); }
private CommandConfig FindDefaultCommandConfig(CommandConfig commandConfig, ArrayReader <string> argumentsReader) { while (HasDefaultCommand(commandConfig)) { commandConfig = GetDefaultCommandConfig(commandConfig); } InitBaseOptions(ref commandConfig); return(commandConfig); }
internal CommandConfig GetCommandConfig(CommandConfig parentConfig = null) { return(new CommandConfig( Name, _printHelpOnExecute, _parents, _commands, _defaultCommand, GetCommandOptions(parentConfig), _parameters, _paramSeries, _execute )); }
private CommandConfig ParseCommands(CommandConfig commandConfig, ArrayReader <string> argumentsReader) { while (argumentsReader.HasData() && ArgIsCommand(commandConfig, argumentsReader.Current())) { if (argumentsReader.HasData() && ArgIsCommand(commandConfig, argumentsReader.Current())) { commandConfig = GetSubCommandConfig(commandConfig, argumentsReader.Read()); } } InitBaseOptions(ref commandConfig); return(commandConfig); }
public CommandExecutionContextProvider( CliConfig cliConfig, CommandConfig rootCommandConfig, string[] args) { _cliConfig = cliConfig; _rootCommandConfig = rootCommandConfig; _args = args; _valueConverter = new ValueConverter(cliConfig.ArgumentConverters); _argumentValueReader = new ArgumentValueReader(_valueConverter); _configReader = new ConfigReader(_argumentValueReader, cliConfig.Config, cliConfig.GenericConfig); }
private CommandExecutionContext GetCommandExecutionContext(CommandConfig config, CommandConfig defaultConfig, CommandArgs commandArgs) { if (_cliConfig.VersionConfig.Enabled && commandArgs.GetFlag(_cliConfig.VersionConfig.Flag)) { return(PreparePrintVersionCommandExecutionContext(config)); } if (commandArgs.GetFlag(_cliConfig.HelpConfig.Flag)) { return(PrepareHelpCommandExecutionContext(config)); } if (defaultConfig.PrintHelpOnExecute) { return(PrepareHelpCommandExecutionContext(defaultConfig)); } return(new CommandExecutionContext(defaultConfig.Execute, commandArgs)); }
private CommandArgs InitDefaultCommandArgs(CommandConfig commandConfig) { var commandArgs = new CommandArgs(); foreach (var option in commandConfig.Options) { foreach (var valueProvider in GetDefaultValueProvidersInPrecedence(option)) { commandArgs.AddOptionValueProvider(option.Name, valueProvider); } } foreach (var parameter in commandConfig.Parameters) { foreach (var valueProvider in GetDefaultValueProvidersInPrecedence(parameter)) { commandArgs.AddParameterValueProvider(parameter.Name, valueProvider); } } return(commandArgs); }
private static bool HasDefaultCommand(CommandConfig commandConfig) { return(commandConfig.DefaultCommand != null); }
private static CommandConfig GetDefaultCommandConfig(CommandConfig commandConfig) { return(commandConfig.DefaultCommand?.GetCommandConfig(commandConfig)); }
private static bool ArgIsCommand(CommandConfig commandConfig, string arg) { return(commandConfig.Commands.Any(command => command.Name == arg)); }
private PrintVersionCommandExecutionContext PreparePrintVersionCommandExecutionContext(CommandConfig commandConfig) { return(new PrintVersionCommandExecutionContext(_cliConfig, commandConfig)); }
private PrintHelpCommandExecutionContext PrepareHelpCommandExecutionContext(CommandConfig commandConfig, string additionalPrefixMessage) { return(new PrintHelpCommandExecutionContext(additionalPrefixMessage, _cliConfig, commandConfig)); }