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(); if (token == null) { // Should not happen, but the compiler isn't // smart enough to realize this... throw new RuntimeException("Could not get the next token."); } 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); }
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?GetRootCommand(this CommandTree node) { while (node.Parent != null) { node = node.Parent; } return(node); }
public static CommandTree GetLeafCommand(this CommandTree node) { while (node.Next != null) { node = node.Next; } return(node); }
public static void Bind(CommandTree tree, ref CommandSettings settings, ITypeResolver resolver) { ValidateRequiredParameters(tree); TypeConverter GetConverter(CommandParameter parameter) { if (parameter.Converter == null) { if (parameter.ParameterType.IsArray) { // Return a converter for each array item (not the whole array) return(TypeDescriptor.GetConverter(parameter.ParameterType.GetElementType())); } return(TypeDescriptor.GetConverter(parameter.ParameterType)); } var type = Type.GetType(parameter.Converter.ConverterTypeName); return(resolver.Resolve(type) as TypeConverter); } while (tree != null) { // Process mapped parameters. foreach (var mapped in tree.Mapped) { var converter = GetConverter(mapped.Parameter); mapped.Parameter.Assign(settings, converter.ConvertFromInvariantString(mapped.Value)); ValidateParameter(mapped.Parameter, settings); } // Process unmapped parameters. foreach (var parameter in tree.Unmapped) { // Is this an option with a default value? if (parameter is CommandOption option && option.DefaultValue != null) { parameter.Assign(settings, option.DefaultValue.Value); ValidateParameter(parameter, settings); } } tree = tree.Next; } // Validate the settings. var validationResult = settings.Validate(); if (!validationResult.Successful) { throw RuntimeException.ValidationFailed(validationResult); } }
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, CaseSensitivity); 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); } }
private static void ValidateRequiredParameters(CommandTree tree) { var node = tree.GetRootCommand(); while (node != null) { foreach (var parameter in node.Unmapped) { if (parameter.Required) { switch (parameter) { case CommandArgument argument: throw RuntimeException.MissingRequiredArgument(node, argument); } } } node = node.Next; } }
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.Parameter == option)); } node = node.Parent; } return(false); }
private static Task <int> Execute( CommandTree leaf, CommandTree tree, CommandContext context, ITypeResolver resolver, IConfiguration configuration) { // Bind the command tree against the settings. var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver); configuration.Settings.Interceptor?.Intercept(context, settings); // Create and validate the command. var command = leaf.CreateCommand(resolver); var validationResult = command.Validate(context, settings); if (!validationResult.Successful) { throw RuntimeException.ValidationFailed(validationResult); } // Execute the command. return(command.Execute(context, settings)); }
private string?ParseOptionValue( CommandTreeParserContext context, CommandTreeTokenStream stream, CommandTreeToken token, CommandTree current, CommandParameter?parameter) { var value = default(string); // 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, CaseSensitivity) == null) { if (parameter != null) { if (parameter.ParameterKind == ParameterKind.Flag) { if (!Constants.AcceptedBooleanValues.Contains(valueToken.Value, StringComparer.OrdinalIgnoreCase)) { // Flags cannot be assigned a value. throw ParseException.CannotAssignValueToFlag(context.Arguments, token); } } value = stream.Consume(CommandTreeToken.Kind.String)?.Value; } 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 { if (parameter is CommandOption option) { if (parameter.IsFlagValue()) { return(null); } throw ParseException.OptionHasNoValue(context.Arguments, token, option); } else { // 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); }
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, CaseSensitivity); 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. if (parameter.ParameterKind == ParameterKind.Vector) { // Vector var current = stream.Current; while (current?.TokenKind == CommandTreeToken.Kind.String) { var value = stream.Consume(CommandTreeToken.Kind.String)?.Value; node.Mapped.Add(new MappedCommandParameter(parameter, value)); current = stream.Current; } } else { // Scalar var value = stream.Consume(CommandTreeToken.Kind.String)?.Value; node.Mapped.Add(new MappedCommandParameter(parameter, value)); context.IncreaseArgumentPosition(); } }
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()); }