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

Repetition of same Boolean argument with arity 0 negates argument's value #530

Open
twwwt opened this issue Dec 13, 2021 · 2 comments
Open

Comments

@twwwt
Copy link

twwwt commented Dec 13, 2021

Consider the following argument definition:

@Parameter(
	names = { "-y", "--dry-run" },
	description = "Perform a dry-run.",
	arity = 0,
	order = 9
)
private Boolean dryRun = FALSE;

Current Behavior

If the Boolean argument is used repeatedly on the command line (i.e., more than once), its effective Boolean value flips with every repetition:

$ mytool		-- dryRun is false
$ mytool -y		-- dryRun is true
$ mytool -y -y		-- dryRun is false
$ mytool -y -y -y	-- dryRun is true
... and so on

While the first two lines work as expected, starting from the third line (where -y occurs twice), the effective value of dryRun flips with every repetition of -y. This is independent of whether the argument is repeated in direct succession (as is done in the example) or whether other arguments occur in-between repetitions.

Expected Behavior

As an immediate remedy, jCommand should consider arguments to be non-repeatable by default and thus reject repeated use of the same argument by throwing an exception when parsing the command line.

Looking at the problem more generally, it occurs to me that the @Parameter annotation of jCommand needs to be extended with a Boolean parameter, let's call it repeatable, that allows to specify whether an argument can occur repeatedly on the command line. The default of that parameter should be false (i.e. it may not be repeated).

@timbarlotta
Copy link

This is reproducible even if the arity is left at the default of -1. The logic keeps flipping the boolean. Seems like this should either:

  1. error out as the parameter should only be specified once
  2. be set to true no matter how many times the flag is passed

@mkarg
Copy link
Collaborator

mkarg commented Apr 28, 2024

If this bug still exists on JCommander 1.83 please post a unit test here which proofs your claim. Thanks.

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

No branches or pull requests

3 participants