parseNamed<T> method

Future<T?> parseNamed<T>(
  1. String name
)

Attempts to parse the value of the option name to a value of type T.

If T is not a supertype of the type of the parameter name in the command callback, an exception is thrown.

If the user has not provided a value for the option name, null is returned.

If no parameter with the name name exists in the command callback, an exception is thrown.

You might also be interested in:

  • parse, for parsing the current option.

Implementation

Future<T?> parseNamed<T>(String name) async {
  ParameterData<dynamic> parameterData = _functionData.parametersData.singleWhere(
    (element) => element.name == name,
    orElse: () => throw CommandsException(
      'No option with name "$name" found in command ${command.fullName}',
    ),
  );

  if (!RuntimeType<T>().isSupertypeOf(parameterData.type)) {
    throw CommandsException('Type $T is not a supertype of ${parameterData.type}');
  }

  String? value = arguments[parameterData.name];

  if (value == null) {
    return null;
  }

  return converters
      .parse(
        commands,
        this,
        StringView(value, isRestBlock: true),
        parameterData.type,
      )
      .then((value) => value as T);
}