Skip to content

Update flutter_command.dart getter methods for command line options #101595

@Jasguerrero

Description

@Jasguerrero

Use case

Currently this 4 methods (https://github.com/flutter/flutter/blob/flutter-2.13-candidate.0/packages/flutter_tools/lib/src/runner/flutter_command.dart#L1505) checks if argResults is null and it defaults to dummy values if so. They make an assumption that the argument passed to them is valid therefore if the key is not found this methods raise an ArgumentError

  /// Gets the parsed command-line option named [name] as a bool.
  bool boolArg(String name) => argResults?[name] as bool? ?? false;

  /// Gets the parsed command-line option named [name] as a String.
  String? stringArg(String name) => argResults?[name] as String?;

  /// Gets the parsed command-line option named [name] as an int.
  int? intArg(String name) => argResults?[name] as int?;

  /// Gets the parsed command-line option named [name] as List<String>.
  List<String> stringsArg(String name) => argResults?[name] as List<String>? ?? <String>[];

Proposal

Make new boolArg and stringArg functions to return bool? and List? respectively and for stringArg and intArg to check if the given argument is valid to avoid the ArgumentError; then start incrementally using the new functions in all the places needed; the caller should handle what default value to use if a null is returned.

bool? boolArgSafe(String name) {
     if (argResult == null) {
          return null
     } else {
          bool? result;
          if (argParser.options.containsKey(name)) {
               result = argResults[name] as bool? ?? false
          }
          return result;
     }
}
// eg: argResults = {"key1": bool}
void myCaller(){
    bool? key1 = boolArg("key1")
    bool? key2 = boolArg("key2")
    if (key1 == null || key2== null){
         print("Missing params")
    }
    ...
}
  • rename current functions fnName to fnNameDeprecated
  • make new function handling nulls bool? boolArg
  • make new function handling nulls String? stringArg
  • make new function handling nulls int? intArg
  • migrate boolArgDeprecated to boolArg
  • migrate stringArgDeprecated to stringArg
  • migrate intArgDeprecated to intArg
  • rename stringsArgDeprecated to stringsArg

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions