Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

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
Contributor

@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 dart-lang/core#39.


  • 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
@devoncarew devoncarew requested a review from natebosch March 12, 2024 16:45
@natebosch
Copy link
Contributor

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
Contributor

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
Contributor 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
Contributor 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
Contributor

@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
Contributor

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
Contributor 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
Contributor

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
Contributor 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
Contributor

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

@natebosch
Copy link
Contributor

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.

mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 11, 2024
…d `multiOption(String)` methods (dart-archive/args#248)

* introduce types methods to ArgResults

* rename methods

* updates

* review feedback

* reduce PR diffs

* reduce PR diffs

* review feedback

* Update arg_results.dart
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants