BToolOptions.fromArgs constructor

BToolOptions.fromArgs(
  1. Iterable<String> args, {
  2. FileSystem? fs,
})

Implementation

factory BToolOptions.fromArgs(
  Iterable<String> args, {
  FileSystem? fs,
}) {
  final _fs = fs ?? const LocalFileSystem();
  BToolOptionKey _key;
  BToolAction _action;
  Iterable<String>? _args;
  String? _value;

  final res = parser.parse(args);

  // <get/set> <key> [<value?>]
  if (res.rest.length > 1) {
    try {
      _action = BToolAction.values.firstWhere((e) => e.name == res.rest[0]);
    } catch (e) {
      throw BToolException('Unknown action: ${res.rest[0]}');
    }
    try {
      _key = BToolOptionKey.values.firstWhere((e) => e.name == res.rest[1]);
    } catch (e) {
      throw BToolException('Unknown key: ${res.rest[1]}');
    }
    _args = res.rest.sublist(2);
    if (_args.isNotEmpty) {
      _value = _args.elementAt(0);
    }
  } else if (res['version'] != true && res['help'] != true) {
    throw BToolOptionsException('Bad arguments');
  } else {
    return BToolOptions._(
      key: BToolOptionKey.applicationId,
      action: BToolAction.get,
      parseResult: res,
      workingDir: res['working-dir'] != null
          ? _fs.directory(res['working-dir'])
          : null,
    );
  }

  return BToolOptions._(
    key: _key,
    action: _action,
    args: _args,
    value: _value,
    workingDir:
        res['working-dir'] != null ? _fs.directory(res['working-dir']) : null,
    parseResult: res,
  );
}