-
Notifications
You must be signed in to change notification settings - Fork 42
Add ArgResult.flagCount.
#938
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,15 +66,18 @@ class ArgResults { | |
| /// > flags, [option] for options, and [multiOption] for multi-options. | ||
| dynamic operator [](String name) { | ||
| if (!_parser.options.containsKey(name)) { | ||
| throw ArgumentError('Could not find an option named "--$name".'); | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find an option named "--$name".'); | ||
| } | ||
|
|
||
| final option = _parser.options[name]!; | ||
| if (option.mandatory && !_parsed.containsKey(name)) { | ||
| throw ArgumentError('Option $name is mandatory.'); | ||
| throw ArgumentError.value(name, 'name', 'Option $name is mandatory.'); | ||
| } | ||
|
|
||
| return option.valueOrDefault(_parsed[name]); | ||
| var parsedValue = _parsed[name]; | ||
| if (option.isFlag && parsedValue is int) parsedValue = parsedValue > 0; | ||
| var result = option.valueOrDefault(parsedValue); | ||
| return result; | ||
| } | ||
|
|
||
| /// Returns the parsed or default command-line flag named [name]. | ||
|
|
@@ -83,12 +86,45 @@ class ArgResults { | |
| bool flag(String name) { | ||
| final option = _parser.options[name]; | ||
| if (option == null) { | ||
| throw ArgumentError('Could not find a flag named "--$name".'); | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find a flag named "--$name".'); | ||
| } | ||
| if (!option.isFlag) { | ||
| throw ArgumentError.value(name, 'name', '"$name" is not a flag.'); | ||
| } | ||
| var parsedValue = _parsed[name]; | ||
| if (parsedValue is int) { | ||
| parsedValue = parsedValue > 0; | ||
| } | ||
| return option.valueOrDefault(parsedValue) as bool; | ||
| } | ||
|
|
||
| /// The number of times the flag named [name] occurred. | ||
| /// | ||
| /// If a flag occurred more than once in the arguments, | ||
| /// this is the total number of counts. | ||
| /// | ||
| /// If the negated flag occurred, it resets the count to zero, | ||
| /// ignoring all prior occurrences. Later occurrences may still | ||
| /// increase the count again. | ||
| /// | ||
| /// If the default is to be enabled, the default count is `1` if there were | ||
| /// no occurrences of the flag. | ||
|
Comment on lines
+111
to
+112
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth describing specifically the behavior of passing the flag once when it is also the default?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably best to do so. Just to be safe. |
||
| /// | ||
| /// The [name] must be a valid flag name in the parser. | ||
| /// | ||
| /// The result is never negative. | ||
| int flagCount(String name) { | ||
| final option = _parser.options[name]; | ||
| if (option == null) { | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find a flag named "--$name".'); | ||
| } | ||
| if (!option.isFlag) { | ||
| throw ArgumentError('"$name" is not a flag.'); | ||
| throw ArgumentError.value(name, 'name', '"$name" is not a flag.'); | ||
| } | ||
| return option.valueOrDefault(_parsed[name]) as bool; | ||
| return (_parsed[name] as int?) ?? | ||
| (option.valueOrDefault(null) as bool ? 1 : 0); | ||
| } | ||
|
|
||
| /// Returns the parsed or default command-line option named [name]. | ||
|
|
@@ -97,13 +133,14 @@ class ArgResults { | |
| String? option(String name) { | ||
| final option = _parser.options[name]; | ||
| if (option == null) { | ||
| throw ArgumentError('Could not find an option named "--$name".'); | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find an option named "--$name".'); | ||
| } | ||
| if (!option.isSingle) { | ||
| throw ArgumentError('"$name" is a multi-option.'); | ||
| throw ArgumentError.value(name, 'name', '"$name" is a multi-option.'); | ||
| } | ||
| if (option.mandatory && !_parsed.containsKey(name)) { | ||
| throw ArgumentError('Option $name is mandatory.'); | ||
| throw ArgumentError.value(name, 'name', 'Option $name is mandatory.'); | ||
| } | ||
| return option.valueOrDefault(_parsed[name]) as String?; | ||
| } | ||
|
|
@@ -114,10 +151,11 @@ class ArgResults { | |
| List<String> multiOption(String name) { | ||
| var option = _parser.options[name]; | ||
| if (option == null) { | ||
| throw ArgumentError('Could not find an option named "--$name".'); | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find an option named "--$name".'); | ||
| } | ||
| if (!option.isMultiple) { | ||
| throw ArgumentError('"$name" is not a multi-option.'); | ||
| throw ArgumentError.value(name, 'name', '"$name" is not a multi-option.'); | ||
| } | ||
| return option.valueOrDefault(_parsed[name]) as List<String>; | ||
| } | ||
|
|
@@ -146,7 +184,8 @@ class ArgResults { | |
| /// [name] must be a valid option name in the parser. | ||
| bool wasParsed(String name) { | ||
| if (!_parser.options.containsKey(name)) { | ||
| throw ArgumentError('Could not find an option named "--$name".'); | ||
| throw ArgumentError.value( | ||
| name, 'name', 'Could not find an option named "--$name".'); | ||
| } | ||
|
|
||
| return _parsed.containsKey(name); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,50 @@ void main() { | |
| var results = parser.parse(['--a']); | ||
| throwsIllegalArg(() => results.multiOption('a')); | ||
| }); | ||
|
|
||
| test('flagCount', () { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] I think it would be better to test these different flag behaviors in different test cases. |
||
| var parser = ArgParser(); | ||
| parser.addFlag('flag', abbr: 'f', defaultsTo: false, negatable: false); | ||
| parser.addFlag('negatable-flag', abbr: 'n', defaultsTo: false); | ||
|
|
||
| // Flags not occurring for testing default value. | ||
| parser.addFlag('default-true', abbr: 'd', defaultsTo: true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Can you add a test case with a flag that defaults to true and is also passed? Also one that defaults to true and is negated with a |
||
| parser.addFlag('default-false', defaultsTo: false); | ||
|
|
||
| var results = parser.parse([ | ||
| '--flag', | ||
| '--negatable-flag', | ||
| '-f', | ||
| '-n', | ||
| '-fn', | ||
| '--no-negatable-flag', // Resets `-n` count | ||
| '-nf', | ||
| '-f', | ||
| '-n', | ||
| '--flag', | ||
| '--negatable-flag', | ||
| ]); | ||
|
|
||
| // Counts all occurrences. | ||
| expect(results.flagCount('flag'), 6); | ||
| expect(results.flag('flag'), true); | ||
|
|
||
| // Reset at `--no-negatable-flag`, only counts those after. | ||
| expect(results.flagCount('negatable-flag'), 3); | ||
| expect(results.flag('negatable-flag'), true); | ||
|
|
||
| expect(results.flagCount('default-true'), 1); | ||
| expect(results.flag('default-true'), true); | ||
|
|
||
| expect(results.flagCount('default-false'), 0); | ||
| expect(results.flag('default-false'), false); | ||
|
|
||
| // Reset works correctly as last occurrence, | ||
| // and doesn't fall back on default value. | ||
| results = parser.parse(['-d', '--no-default-true']); | ||
| expect(results.flagCount('default-true'), 0); | ||
| expect(results.flag('default-true'), false); | ||
| }); | ||
| }); | ||
|
|
||
| group('flag()', () { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit]