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

            return(0);
        }, null)
        {
        }
Esempio n. 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);
        }
Esempio n. 6
0
        private CommandConfig FindDefaultCommandConfig(CommandConfig commandConfig, ArrayReader <string> argumentsReader)
        {
            while (HasDefaultCommand(commandConfig))
            {
                commandConfig = GetDefaultCommandConfig(commandConfig);
            }

            InitBaseOptions(ref commandConfig);

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