コード例 #1
0
        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);
        }
コード例 #2
0
 private static CommandConfig GetSubCommandConfig(CommandConfig commandConfig, string commandName)
 {
     return(commandConfig
            .Commands
            .FirstOrDefault(command => command.Name == commandName)
            ?.GetCommandConfig(commandConfig));
 }
コード例 #3
0
        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
                }
                    );
            }
        }
コード例 #4
0
        public PrintVersionCommandExecutionContext(CliConfig cliConfig, CommandConfig commandConfig)
            : base((commandArgs, output) =>
        {
            output.WriteLine(cliConfig.Name + " Version: " + Assembly.GetEntryAssembly()?.GetName().Version);

            return(0);
        }, null)
        {
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        private CommandConfig FindDefaultCommandConfig(CommandConfig commandConfig, ArrayReader <string> argumentsReader)
        {
            while (HasDefaultCommand(commandConfig))
            {
                commandConfig = GetDefaultCommandConfig(commandConfig);
            }

            InitBaseOptions(ref commandConfig);

            return(commandConfig);
        }
コード例 #7
0
 internal CommandConfig GetCommandConfig(CommandConfig parentConfig = null)
 {
     return(new CommandConfig(
                Name,
                _printHelpOnExecute,
                _parents,
                _commands,
                _defaultCommand,
                GetCommandOptions(parentConfig),
                _parameters,
                _paramSeries,
                _execute
                ));
 }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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);
        }
コード例 #12
0
 private static bool HasDefaultCommand(CommandConfig commandConfig)
 {
     return(commandConfig.DefaultCommand != null);
 }
コード例 #13
0
 private static CommandConfig GetDefaultCommandConfig(CommandConfig commandConfig)
 {
     return(commandConfig.DefaultCommand?.GetCommandConfig(commandConfig));
 }
コード例 #14
0
 private static bool ArgIsCommand(CommandConfig commandConfig, string arg)
 {
     return(commandConfig.Commands.Any(command => command.Name == arg));
 }
コード例 #15
0
 private PrintVersionCommandExecutionContext PreparePrintVersionCommandExecutionContext(CommandConfig commandConfig)
 {
     return(new PrintVersionCommandExecutionContext(_cliConfig, commandConfig));
 }
コード例 #16
0
 private PrintHelpCommandExecutionContext PrepareHelpCommandExecutionContext(CommandConfig commandConfig, string additionalPrefixMessage)
 {
     return(new PrintHelpCommandExecutionContext(additionalPrefixMessage, _cliConfig, commandConfig));
 }