Esempio n. 1
0
        ParameterInfo ICommandExecutorImpl.SetValueParameter(string key, CommandAttribute command, SortedList <int, object> parameters, ParameterInfo lastParameter)
        {
            if (_impl.TryAddToLastParameter(lastParameter, key, parameters))
            {
                return(lastParameter);
            }

            var parameter = command.Values.Select(x => x.Parameter).FirstOrDefault(x => !parameters.ContainsKey(x.Position));

            if (parameter == null)
            {
                throw new BadCommandException(command, $"Too many values");
            }

            var value = _valueConverter.GetValue(key, parameter.ParameterType);

            parameters.Add(parameter.Position, value);

            return(parameter);
        }
Esempio n. 2
0
        async Task ICommandExecutorImpl.ExecuteCommandAsync(CommandAttribute command, IEnumerator <CommandPart> commandParts, GlobalOptionsWrapper globalOptions)
        {
            var parameters = new SortedList <int, object>();

            ParameterInfo lastParameter = null;

            while (commandParts.MoveNext())
            {
                var key = _parser.GetString(commandParts.Current.Key);

                if (commandParts.Current.IsArgument && _commandHelper.TryShowHelpOrVersion(commandParts.Current, command, key, _options, globalOptions))
                {
                    return;
                }

                if (commandParts.Current.IsArgument)
                {
                    if (_impl.TrySetGlobalOption(key, commandParts.Current, globalOptions))
                    {
                        continue;
                    }

                    lastParameter = _impl.SetOptionParameter(key, commandParts.Current, command, parameters);
                }
                else
                {
                    lastParameter = _impl.SetValueParameter(key, command, parameters, lastParameter);
                }
            }

            _impl.AddDefaultValues(command, parameters);

            if (globalOptions.GlobalOptions != null && command.GlobalOptionsParameter != null)
            {
                parameters[command.GlobalOptionsParameter.Position] = globalOptions.GlobalOptions;
            }

            await _impl.ExecuteActualCommandAsync(command, parameters);

            return;
        }
Esempio n. 3
0
        void ICommandHelper.WriteCommandHelp(CommandAttribute command, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions)
        {
            Console.Write($"Usage:  {command.GetName()}");

            if (options.ValuesFirst)
            {
                foreach (var value in command.Values)
                {
                    Console.Write($" {{{value.Parameter.Name}}}");
                }
            }

            if (command.Options.Count > 0)
            {
                Console.Write(" [Options]");
            }

            if (!options.ValuesFirst)
            {
                foreach (var value in command.Values)
                {
                    Console.Write($" {{{value.Parameter.Name}}}");
                }
            }

            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine(command.HelpText);

            if (command.Values.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("Values:");

                foreach (var value in command.Values)
                {
                    Console.WriteLine($"    {value.GetName(),-20}{value.HelpText}");
                }
            }

            if (command.Options.Count == 0)
            {
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Options:");

            var written = new HashSet <OptionAttribute>();

            foreach (var option in command.Options)
            {
                if (written.Contains(option.Value))
                {
                    continue;
                }

                Console.Write("    ");

                string name = "   ";

                if (option.Value.ShortForm != '\0')
                {
                    name = " -" + option.Value.ShortForm;
                }

                if (option.Value.LongForm != null)
                {
                    name += " --" + option.Value.LongForm + (option.Value.Parameter.ParameterType == typeof(bool) ? "" : "=<value>");
                }

                var helpText = option.Value.HelpText;

                if (option.Value.Parameter.ParameterType.IsArray)
                {
                    helpText = "(Array) " + helpText;
                }

                Console.WriteLine(name.PadRight(30, ' ') + helpText);

                written.Add(option.Value);
            }

            Console.WriteLine();
        }
Esempio n. 4
0
 public bool TryGetCommandAttribute(MethodInfo methodInfo, out CommandAttribute commandAttribute)
 {
     commandAttribute = methodInfo.GetCustomAttribute <CommandAttribute>();
     return(commandAttribute != null);
 }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 public BadCommandException(CommandAttribute command, string badCommand)
     : base($"Malformed or missing command {badCommand}")
 {
     Command    = command;
     BadCommand = badCommand;
 }