addFlag method

void addFlag (String name, { String abbr, String help, bool defaultsTo: false bool negatable: true void callback(bool value), bool hide: false })

Defines a boolean flag.

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 flag. For example, abbr: "a" will allow the user to pass -a to enable the flag.

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

The defaultsTo argument indicates the value this flag will have if the user doesn't explicitly pass it in.

The negatable argument indicates whether this flag's value can be set to false. For example, if name is flag, the user can pass --no-flag to set its value to false.

The callback argument is invoked with the flag's value when the flag 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.

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

Throws an ArgumentError if:

  • There is already an option named name.
  • There is already an option using abbreviation abbr.

Implementation

void addFlag(String name,
    {String abbr,
    String help,
    bool defaultsTo = false,
    bool negatable = true,
    void callback(bool value),
    bool hide = false}) {
  _addOption(
      name,
      abbr,
      help,
      null,
      null,
      null,
      defaultsTo,
      callback == null ? null : (value) => callback(value as bool),
      OptionType.flag,
      negatable: negatable,
      hide: hide);
}