string CommandParameterToString(CommandParameter parameter) { Func <CommandParameter, object> func; if (!commandParameterStrings.TryGetValue(parameter.GetType(), out func)) { func = (p) => p.Token; } return(func(parameter).ToString()); }
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); }