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

Introduce new typed ArgResults flag(String), option(String), and multiOption(String) methods #248

Merged
merged 10 commits into from
Mar 15, 2024

Conversation

devoncarew
Copy link
Member

@devoncarew devoncarew commented Jun 1, 2023

  • introduce new typed ArgResults flag(String), option(String), and multiOption(String) methods

This API lets people pull flags, options, and multi-options out of results objects without having to cast the values (e.g., results['param1'] as bool, results['param2'] as String?, ...). This is similar to the approach discussed in #95 (comment).


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

lib/command_runner.dart Outdated Show resolved Hide resolved
lib/command_runner.dart Outdated Show resolved Hide resolved
@devoncarew devoncarew marked this pull request as ready for review June 6, 2023 21:05
@natebosch
Copy link
Member

cc @jakemac53 @munificent

lib/src/arg_results.dart Outdated Show resolved Hide resolved
@@ -57,7 +57,7 @@ class ArgResults {
: rest = UnmodifiableListView(rest),
arguments = UnmodifiableListView(arguments);

/// Returns the parsed ore default command-line option named [name].
/// Returns the parsed or default command-line option named [name].
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate this? Or at least mention the new alternatives here in the dartdoc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mark it deprecated (or at least not yet), but I like the idea of having the doc comment here point to the new better methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was a little hesitant about deprecating it given that essentially every user of this package will get deprecation messages.

Updating the docs here make sense.

Copy link
Member Author

@devoncarew devoncarew Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  /// > [!Note]
  /// > Callers should prefer using the more strongly typed methods - [flag] for
  /// > flags, [option] for options, and [multiOption] for multi-options.

Copy link
Member

@munificent munificent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@@ -57,7 +57,7 @@ class ArgResults {
: rest = UnmodifiableListView(rest),
arguments = UnmodifiableListView(arguments);

/// Returns the parsed ore default command-line option named [name].
/// Returns the parsed or default command-line option named [name].
///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mark it deprecated (or at least not yet), but I like the idea of having the doc comment here point to the new better methods.

test/parse_test.dart Outdated Show resolved Hide resolved
test/parse_test.dart Outdated Show resolved Hide resolved
@devoncarew devoncarew merged commit 788d935 into master Mar 15, 2024
6 checks passed
@devoncarew devoncarew deleted the typed_results branch March 15, 2024 16:20
@devoncarew
Copy link
Member Author

Hmm, this PR has introduced a bit of asymmetry in the API. With the new bool flag(String) ArgResults method, you can only retrieve a non-null bool from the parsed results.

However, with the existing ArgParser.addFlag() method, you can set a null default value for the flag (bool? defaultsTo = false):

https://github.com/dart-lang/args/blob/master/lib/src/arg_parser.dart#L129C3-L136C42

So an ArgParser can be created that will always throw when you try and retrieve the flag value from the ArgResults.

I could see a few resolutions here:

  1. update the ArgParser.addFlag method to not allow a null value for defaultsTo; rev the major version of package:args to capture the API change.
  2. ignore this issue for now; for places where people do want to pull out a null value, let them rely on the existing argResults[flagName] mechanism. Create an issue to remove the nullability with the next major package publish.

I'd lean towards option 2 above.

@natebosch
Copy link
Member

I suppose folks who are broken by the change can move to checking wasParsed to handle whatever behavior they intended from the null default? Option 2 SGTM.

@devoncarew
Copy link
Member Author

Yeah, I ran across this when updating the dart cli to use these new methods. dart compile wasm uses a strange tri-state for a --minify flag - null, true, or false. I updated it to use wasParsed (args.wasParsed('minify') ? null : args.flag('minify')), but really don't think the pattern's that great - supporting a null default value.

@munificent
Copy link
Member

You could add something like bool? ArgResults.optionalFlag() to check the tri-state value of a nullable flag.

@natebosch
Copy link
Member

You could add something like bool? ArgResults.optionalFlag() to check the tri-state value of a nullable flag.

I think I'm more inclined not add this in favor of keeping the undesirable pattern localized to where it is used. If code is doing something usual like a tri-state flag, it's OK for the code to also look unusual and need an extra wasParsed check.

If some project has strong opinions about the API they can hide that pattern behind an optionalFlag extension method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants