public object Interpret(string[] args, out string[] errors, bool useDefault, bool isInteractive = false) { var interpreter = GetInterpreter(); var messages = new List<string>(); BaseCommandConfig command = null; string commandName = null; int firstArgumentIndex = 0; if (useDefault || !_config.Commands.Any()) { if (_config.DefaultCommand == null) throw new CommandConfigurationInvalid(); command = _config.DefaultCommand; firstArgumentIndex = 0; } else { if (args == null || args.Length == 0) { errors = null; return null; } commandName = args[0].ToLower(); command = _config.Commands.FirstOrDefault(c => c.Name == commandName); firstArgumentIndex = 1; } if (command == null) { messages.Add("Command not recognised."); errors = messages.ToArray(); return null; } var parserArgs = args.Skip(firstArgumentIndex).ToArray(); var result = new ParserResult(command, commandName); var optionList = GetOptionsAndAliases(command); interpreter.Parse(parserArgs, optionList, command.Positionals, result); result.ParseCompleted(); if (result.Status != ParseStatus.CompletedOk) { errors = new[] {result.Error}; return null; } var validationMessages = new List<string>(); var commandValid = command.Validate(result.ParamObject, validationMessages); messages.AddRange(validationMessages); errors = messages.ToArray(); return commandValid ? result.ParamObject : null; }
public void SetUp() { _config = new CommandLineInterpreterConfiguration(); _command = _config.Parameters(() => new CommandParams()) .Positional<string>("pos1", (c, s) => c.Pos1 = s) .Positional<int>("pos2", (c, i) => c.Pos2 = i) .Option<string, int>("opt1", (c, s, i) => { c.Opt1String = s; c.Opt1Int = i; }) .Alias("1") .Alias("one") .Option("opt2", (c, b) => c.Opt2 = b) .Alias("2") .Alias("two") .Option<string>("opt3", (c, s) => c.Opt3 = s) .Alias("3") .Alias("three"); _parserResult = new ParserResult(_command, "commandName"); }