public CommandTree(CommandTree parent, CommandInfo command) { Parent = parent; Command = command; Mapped = new List <MappedCommandParameter>(); Unmapped = new List <CommandParameter>(); }
public static CommandTree GetRootCommand(this CommandTree node) { while (node.Parent != null) { node = node.Parent; } return(node); }
public static CommandOption FindOption(this CommandTree tree, string name, bool longOption) { return(tree.Command.Parameters .OfType <CommandOption>() .FirstOrDefault(o => longOption ? o.LongNames.Contains(name, StringComparer.Ordinal) : o.ShortNames.Contains(name, StringComparer.Ordinal))); }
public static CommandTree GetLeafCommand(this CommandTree node) { while (node.Next != null) { node = node.Next; } return(node); }
private CommandTree ParseCommandParameters( CommandTreeParserContext context, CommandInfo command, CommandTree parent, CommandTreeTokenStream stream) { context.ResetArgumentPosition(); var node = new CommandTree(parent, command); while (stream.Peek() != null) { var token = stream.Peek(); switch (token.TokenKind) { case CommandTreeToken.Kind.LongOption: // Long option ParseOption(context, stream, token, node, true); break; case CommandTreeToken.Kind.ShortOption: // Short option ParseOption(context, stream, token, node, false); break; case CommandTreeToken.Kind.String: // Command ParseString(context, stream, node); break; case CommandTreeToken.Kind.Remaining: // Remaining stream.Consume(CommandTreeToken.Kind.Remaining); context.State = State.Remaining; break; default: throw new InvalidOperationException($"Encountered unknown token ({token.TokenKind})."); } } // Add unmapped parameters. foreach (var parameter in node.Command.Parameters) { if (node.Mapped.All(m => m.Parameter != parameter)) { node.Unmapped.Add(parameter); } } return(node); }
private void ParseString( CommandTreeParserContext context, CommandTreeTokenStream stream, CommandTree node) { if (context.State == State.Remaining) { stream.Consume(CommandTreeToken.Kind.String); return; } var token = stream.Expect(CommandTreeToken.Kind.String); // Command? var command = node.Command.FindCommand(token.Value); if (command != null) { if (context.State == State.Normal) { node.Next = ParseCommand(context, node.Command, node, stream); } return; } // Current command has no arguments? if (!node.HasArguments()) { throw ParseException.UnknownCommand(_configuration, node, context.Arguments, token); } // Argument? var parameter = node.FindArgument(context.CurrentArgumentPosition); if (parameter == null) { // No parameters left. Any commands after this? if (node.Command.Children.Count > 0 || node.Command.IsDefaultCommand) { throw ParseException.UnknownCommand(_configuration, node, context.Arguments, token); } throw ParseException.CouldNotMatchArgument(context.Arguments, token); } // Yes, this was an argument. var value = stream.Consume(CommandTreeToken.Kind.String).Value; node.Mapped.Add(new MappedCommandParameter(parameter, value)); context.IncreaseArgumentPosition(); }
private CommandTree ParseCommand( CommandTreeParserContext context, ICommandContainer current, CommandTree parent, CommandTreeTokenStream stream) { // Find the command. var commandToken = stream.Consume(CommandTreeToken.Kind.String); var command = current.FindCommand(commandToken.Value); if (command == null) { throw ParseException.UnknownCommand(_configuration, parent, context.Arguments, commandToken); } return(ParseCommandParameters(context, command, parent, stream)); }
private void ParseOption( CommandTreeParserContext context, CommandTreeTokenStream stream, CommandTreeToken token, CommandTree node, bool isLongOption) { // Consume the option token. stream.Consume(isLongOption ? CommandTreeToken.Kind.LongOption : CommandTreeToken.Kind.ShortOption); if (context.State == State.Normal) { // Find the option. var option = node.FindOption(token.Value, isLongOption); if (option != null) { node.Mapped.Add(new MappedCommandParameter( option, ParseOptionValue(context, stream, token, node, option))); return; } // Help? if (_help?.IsMatch(token.Value) == true) { node.ShowHelp = true; return; } } if (context.State == State.Remaining) { ParseOptionValue(context, stream, token, node, null); return; } if (context.ParsingMode == ParsingMode.Strict) { throw ParseException.UnknownOption(context.Arguments, token); } else { ParseOptionValue(context, stream, token, node, null); } }
public static bool IsOptionMappedWithParent(this CommandTree tree, string name, bool longOption) { var node = tree.Parent; while (node != null) { var option = node.Command?.Parameters.OfType <CommandOption>() .FirstOrDefault(o => longOption ? o.LongNames.Contains(name, StringComparer.Ordinal) : o.ShortNames.Contains(name, StringComparer.Ordinal)); if (option != null) { return(node.Mapped.Any(p => p.Item1 == option)); } node = node.Parent; } return(false); }
public CommandTreeParserResult(CommandTree tree, IRemainingArguments remaining) { Tree = tree; Remaining = remaining; }
private static string ParseOptionValue( CommandTreeParserContext context, CommandTreeTokenStream stream, CommandTreeToken token, CommandTree current, CommandParameter parameter) { var value = (string)null; // Parse the value of the token (if any). var valueToken = stream.Peek(); if (valueToken?.TokenKind == CommandTreeToken.Kind.String) { var parseValue = true; if (token.TokenKind == CommandTreeToken.Kind.ShortOption && token.IsGrouped) { parseValue = false; } if (context.State == State.Normal && parseValue) { // Is this a command? if (current.Command.FindCommand(valueToken.Value) == null) { if (parameter != null) { if (parameter.ParameterKind == ParameterKind.Single) { value = stream.Consume(CommandTreeToken.Kind.String).Value; } else if (parameter.ParameterKind == ParameterKind.Flag) { // Flags cannot be assigned a value. throw ParseException.CannotAssignValueToFlag(context.Arguments, token); } } else { // Unknown parameter value. value = stream.Consume(CommandTreeToken.Kind.String).Value; // In relaxed parsing mode? if (context.ParsingMode == ParsingMode.Relaxed) { context.AddRemainingArgument(token.Value, value); } } } } else { context.AddRemainingArgument(token.Value, parseValue ? valueToken.Value : null); } } else { if (context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed) { context.AddRemainingArgument(token.Value, null); } } // No value? if (context.State == State.Normal) { if (value == null && parameter != null) { if (parameter.ParameterKind == ParameterKind.Flag) { value = "true"; } else { switch (parameter) { case CommandOption option: throw ParseException.OptionHasNoValue(context.Arguments, token, option); default: // This should not happen at all. If it does, it's because we've added a new // option type which isn't a CommandOption for some reason. throw new InvalidOperationException($"Found invalid parameter type '{parameter.GetType().FullName}'."); } } } } return(value); }
public static CommandArgument FindArgument(this CommandTree tree, int position) { return(tree.Command.Parameters .OfType <CommandArgument>() .FirstOrDefault(c => c.Position == position)); }
public static bool HasArguments(this CommandTree tree) { return(tree.Command.Parameters.OfType <CommandArgument>().Any()); }