addCliOpt method

bool addCliOpt(
  1. List<CliOptDef> optDefs,
  2. String? name, {
  3. bool isPositive = true,
  4. String? value,
  5. int argNo = -1,
})

Add new option and/or value to the list of parsed options

Implementation

bool addCliOpt(List<CliOptDef> optDefs, String? name,
    {bool isPositive = true, String? value, int argNo = -1}) {
  var opt = findCliOptByName(name);
  CliOptDef? optDef;

  if (opt == null) {
    optDef = optDefs.findCliOptDef(name);

    if (optDef == null) {
      return false;
    }

    opt = CliOpt(optDef,
        argNo: argNo,
        fullName: optDef.name.addPrefix(isPositive: isPositive));
    add(opt);
  }

  final newValueCount = opt.addValue(value);
  optDef = opt.optDef;

  // If the option allows any number of values, then can continue adding values
  //
  if (optDef.hasManyValues) {
    return (optDef.name.isEmpty || (newValueCount <= 1));
  }

  // If the option is flag, then no more value can be added
  //
  if (optDef.isFlag) {
    return false;
  }

  // In case of an option with single value, can add value only if no value has been added yet
  //
  return opt.values.isEmpty;
}