addOption method

void addOption (String name, { String abbr, String help, String valueHelp, Iterable<String> allowed, Map<String, String> allowedHelp, String defaultsTo, Function callback, @Deprecated("Use addMultiOption() instead.") bool allowMultiple: false, @Deprecated("Use addMultiOption() instead.") bool splitCommas, bool hide: false })

Defines an option that takes a value.

This adds an Option with the given properties to options.

The abbr argument is a single-character string that can be used as a shorthand for this option. For example, abbr: "a" will allow the user to pass -a value or -avalue.

The help argument is used by usage to describe this option.

The valueHelp argument is used by usage as a name for the value this option takes. For example, valueHelp: "FOO" will include --option=<FOO> rather than just --option in the usage string.

The allowed argument is a list of valid values for this option. If it's non-null and the user passes a value that's not included in the list, parse will throw a FormatException. The allowed values will also be included in usage.

The allowedHelp argument is a map from values in allowed to documentation for those values that will be included in usage.

The defaultsTo argument indicates the value this option will have if the user doesn't explicitly pass it in (or null by default).

The callback argument is invoked with the option's value when the option is parsed. Note that this makes argument parsing order-dependent in ways that are often surprising, and its use is discouraged in favor of reading values from the ArgResult.

The allowMultiple and splitCommas options are deprecated; the addMultiOption method should be used instead.

If hide is true, this option won't be included in usage.

Throws an ArgumentError if:

  • There is already an option with name name.
  • There is already an option using abbreviation abbr.
  • splitCommas is passed but allowMultiple is false.

Implementation

void addOption(String name,
    {String abbr,
    String help,
    String valueHelp,
    Iterable<String> allowed,
    Map<String, String> allowedHelp,
    String defaultsTo,
    Function callback,
    @Deprecated("Use addMultiOption() instead.") bool allowMultiple = false,
    @Deprecated("Use addMultiOption() instead.") bool splitCommas,
    bool hide = false}) {
  if (!allowMultiple && splitCommas != null) {
    throw ArgumentError(
        'splitCommas may not be set if allowMultiple is false.');
  }

  _addOption(
      name,
      abbr,
      help,
      valueHelp,
      allowed,
      allowedHelp,
      allowMultiple
          ? (defaultsTo == null ? <String>[] : [defaultsTo])
          : defaultsTo,
      callback,
      allowMultiple ? OptionType.multiple : OptionType.single,
      splitCommas: splitCommas,
      hide: hide);
}