parse<T> function

Future<T> parse<T>(
  1. CommandsPlugin commands,
  2. ContextData context,
  3. StringView toParse,
  4. RuntimeType<T> expectedType,
  5. {Converter<T>? converterOverride}
)

Apply a converter to an input and return the result.

  • commands is the instance of CommandsPlugin used to retrieve the appropriate converter;
  • context is the context to parse arguments in;
  • toParse is the input to the converter;
  • expectedType is the type that should be returned from this function;
  • converterOverride can be specified to use that converter instead of querying commands for the converter to use.

You might also be interested in:

  • Command.invoke, which parses multiple arguments and executes a command.

Implementation

Future<T> parse<T>(
  CommandsPlugin commands,
  ContextData context,
  StringView toParse,
  RuntimeType<T> expectedType, {
  Converter<T>? converterOverride,
}) async {
  Converter<T>? converter = converterOverride ?? commands.getConverter(expectedType);
  if (converter == null) {
    throw NoConverterException(expectedType);
  }

  StringView originalInput = toParse.copy();

  try {
    T? parsed = await converter.convert(toParse, context);

    if (parsed == null) {
      throw ConverterFailedException(converter, originalInput, context);
    }

    return parsed;
  } on ParsingException catch (e) {
    throw BadInputException('Bad input $context: ${e.message}', context);
  }
}