public CommandTreeParserResult Parse(IEnumerable <string> args) { var context = new CommandTreeParserContext(args, _configuration.ParsingMode); var tokenizerResult = CommandTreeTokenizer.Tokenize(context.Arguments); var tokens = tokenizerResult.Tokens; var rawRemaining = tokenizerResult.Remaining; var result = (CommandTree)null; if (tokens.Count > 0) { // Not a command? var token = tokens.Current; if (token.TokenKind != CommandTreeToken.Kind.String) { // Got a default command? if (_configuration.DefaultCommand != null) { result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens); return(new CommandTreeParserResult( result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining))); } // Show help? if (_help?.IsMatch(token.Value) == true) { return(new CommandTreeParserResult( null, new RemainingArguments(context.GetRemainingArguments(), rawRemaining))); } // Unexpected option. throw ParseException.UnexpectedOption(context.Arguments, token); } // Does the token value match a command? var command = _configuration.FindCommand(token.Value); if (command == null) { if (_configuration.DefaultCommand != null) { result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens); return(new CommandTreeParserResult( result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining))); } } // Parse the command. result = ParseCommand(context, _configuration, null, tokens); } else { // Is there a default command? if (_configuration.DefaultCommand != null) { result = ParseCommandParameters(context, _configuration.DefaultCommand, null, tokens); } } return(new CommandTreeParserResult( result, new RemainingArguments(context.GetRemainingArguments(), rawRemaining))); }