Skip to content

Add ArgResult.flagCount.#938

Open
lrhn wants to merge 1 commit intomainfrom
args-flag-count
Open

Add ArgResult.flagCount.#938
lrhn wants to merge 1 commit intomainfrom
args-flag-count

Conversation

@lrhn
Copy link
Member

@lrhn lrhn commented Feb 9, 2026

Counts the occurrences of a flag, and makes it accessible in the ArgResult.

Only changes parser and result class, the Option class still treats flags as bool options, with a value that is true only when the count is greater than zero.

Fixes #937.

(The -vv is something I often use in my own scripts, and one reason for never using the args package.)

Counts the occurrences of a flag, and makes it accessible in the
`ArgResult`.

Only changes parser and result class, the `Option` class still
treats flags as `bool` options, with a value that is `true`
only when the count is greater than zero.

Fixes #937.
@lrhn lrhn requested a review from a team as a code owner February 9, 2026 11:30
@github-actions
Copy link

github-actions bot commented Feb 9, 2026

PR Health

License Headers ✔️
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/collection/benchmark/legacy_quicksort.dart

This check can be disabled by tagging the PR with skip-license-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
args Breaking 2.7.0 2.9.0-wip 2.9.0-wip ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

Unused Dependencies ✔️
Package Status
args ✔️ All dependencies utilized correctly.

For details on how to fix these, see dependency_validator.

This check can be disabled by tagging the PR with skip-unused-dependencies-check.

Coverage ⚠️
File Coverage
pkgs/args/lib/src/arg_results.dart 💔 81 % ⬇️ 7 %
pkgs/args/lib/src/parser.dart 💚 99 % ⬆️ 0 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

Comment on lines +104 to +105
/// If a flag occurred more than once in the arguments,
/// this is the total number of counts.
Copy link
Member

Choose a reason for hiding this comment

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

[nit]

Suggested change
/// If a flag occurred more than once in the arguments,
/// this is the total number of counts.
/// If a flag occurred more than once in the arguments,
/// this is the total number of occurrences.

Comment on lines +111 to +112
/// If the default is to be enabled, the default count is `1` if there were
/// no occurrences of the flag.
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably best to do so. Just to be safe.

throwsIllegalArg(() => results.multiOption('a'));
});

test('flagCount', () {
Copy link
Member

Choose a reason for hiding this comment

The 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.

parser.addFlag('negatable-flag', abbr: 'n', defaultsTo: false);

// Flags not occurring for testing default value.
parser.addFlag('default-true', abbr: 'd', defaultsTo: true);
Copy link
Member

Choose a reason for hiding this comment

The 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 --no flag.

@Levi-Lesches
Copy link

Thanks, I was just looking for this when writing a CLI tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow flags to occur multiple times, and reveal how many.

3 participants