Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgResults: Make easier for Dart2 users #39

Open
5 tasks
matanlurey opened this issue Feb 24, 2018 · 4 comments
Open
5 tasks

ArgResults: Make easier for Dart2 users #39

matanlurey opened this issue Feb 24, 2018 · 4 comments
Labels
package:args type-enhancement A request for a change that isn't a bug

Comments

@matanlurey
Copy link
Contributor

This is a runtime failure in Dart2:

void readListOfNames(ArgResults results) {
  // "List<dynamic> is not a List<String>"
  List<String> names = results['names'];
}

... but patterns like this are really common.

I'd like to see a series of helper methods to make it easier to get common types and collections out of ArgResults, even if all they do behind the scenes is a combination of as and collection casts. Ideas:

Primitives

  • bool readBool(String name)
  • double readDouble(String name)
  • int readInt(String name)
  • String readString(String name)

Collections

  • List<String> readStringList(String name)

Thoughts?

/cc @nex3 @natebosch

@nex3
Copy link
Member

nex3 commented Feb 26, 2018

I think there's a general need for a good API to access heterogeneous untyped information. We have something like this for JSON-RPC 2 parameters, but it would be nice to generalize it enough that it could be used for parsed arguments, JSON/YAML objects, and so on. As the pressures to avoid dynamic values get stronger, this will be more and more relevant.

@srawlins
Copy link
Member

The args package already has a delineation between bool "flags" and other-type "options." What about even just an is or has method for accessing bools?

var argParser = new ArgParser()
    ..addFlag('help');
var argResults = argParser.parse(args);
if (argResults['help']) { ... } // Error: Conditions must have a static type of 'bool'.
if (argResults['help'] == true) { ... } // What I have to do today. :(
if (argResults.has('help')) { ... } // Would be nice.
if (argResults.isNot('help')) { ... } // Lends itself nicely to negatable.

@jonahwilliams
Copy link
Contributor

I'm looking at disabling implicit downcasts in the flutter tool, and a large portion come from ArgResults.[] returning dynamic. I'm working around this with a typed wrapper which exposes getOption, getFlag and getMultiOption in flutter/flutter#31679.

It would be great to have something similar in the args package.

@lrhn
Copy link
Member

lrhn commented Jun 24, 2020

You can do a lot with extensions these days.

extension ArgResultGetters on ArgResults {
   bool? optionalFlag(String name) => this[name] as bool?;
   bool flag(String name, {bool defaultValue: false}) => (this[name] as bool?) ?? defaultValue;
   bool has(String name) => this[name] != null;

   String stringOption(String name, {String defaultValue = ""}) => (this[name] as String?) ?? defaultValue;
   int intOption(String name, {int defaultValue = -1}) {
     var value = this[name];
     if (value is! String) return defaultValue;
     return int.tryParse(value) ?? defaultValue;
   }
   T option<T>(String name) => this[name] as T;

   List<String> multiOption(String name) => this[name] as List<String>;
}

@devoncarew devoncarew added the type-enhancement A request for a change that isn't a bug label Nov 10, 2022
@mosuem mosuem transferred this issue from dart-lang/args Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:args type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants