public ParserContext(ParserConfiguration configuration, string text) { Configuration = configuration; Result = new ParseResult(); Lexer = new Lexer(text); Mapper = new CommandMapper(Configuration.Commands); }
public ParseResult Parse(string input) { var result = new ParseResult(); var lexer = new Lexer(input); var state = new ParserState(CommandRoot, lexer, _helpFlags, _helpVerb); while (state.Advance()) { } result.FinalNode = state.CurrentNode; result.NamedParameters = state.NamedParameters; result.PositionalParameters = state.PositionalParameters; if (state.HelpWanted) { result.Kind = ParseResultKind.Help; } else if (state.CurrentNode is CommandRoot) { if (state.PendingRequired.Any()) { var missing = string.Join(", ", state.PendingRequired); throw new CommandLineParserException(string.Concat("Missing required parameter(s): ", missing)); } result.Kind = ParseResultKind.DefaultCommand; } else if (state.CurrentNode is Command) { if (state.PendingRequired.Any()) { var missing = string.Join(", ", state.PendingRequired); throw new CommandLineParserException(string.Concat("Missing required parameter(s): ", missing)); } if (state.CurrentNode is ExternalCommand) { result.Kind = ParseResultKind.ExternalCommand; } else if (state.CurrentNode is InternalCommand) { result.Kind = ParseResultKind.InternalCommand; } } else { // TODO - scan forward to see if this is unique // TODO - build up a pretty message throw new CommandLineParserException("Ambiguous/incomplete command!"); } return result; }
public void CanPushString() { ParserParameter param = new ParserNamedParameter("Name", false); ParseResult result = new ParseResult(); result.AddCommandValue(param, "value"); var pusher = new CommandPusher(result); var command = new StringCommand(); pusher.Push(command); command.Name.ShouldBe("value"); }
public void CanPushBool() { ParserParameter param = new ParserNamedParameter("Add", true); ParseResult result = new ParseResult(); result.AddCommandValue(param, "true"); var pusher = new CommandPusher(result); var command = new BoolCommand(); pusher.Push(command); command.Add.ShouldBe(true); }
public void CanPushStringList() { ParserParameter param = new ParserPositionalParameter("Names", true); ParseResult result = new ParseResult(); result.AddCommandListValue(param, "Fred"); result.AddCommandListValue(param, "Barney"); var pusher = new CommandPusher(result); var command = new ListCommand(); pusher.Push(command); command.Names.ShouldContain(x => x == "Fred"); command.Names.ShouldContain(x => x == "Barney"); }
public CommandPusher(ParseResult result) { _result = result; }
public PropertyInjector(ParseResult result) { _result = result; }