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

Main parameter conversion has priority over arity validation #546

Open
jontmy opened this issue Nov 2, 2022 · 1 comment
Open

Main parameter conversion has priority over arity validation #546

jontmy opened this issue Nov 2, 2022 · 1 comment
Labels
bug Bug

Comments

@jontmy
Copy link

jontmy commented Nov 2, 2022

Suppose that I have this converter which takes a String and attempts conversion into an Index - which is basically a wrapper around an int:

public class IndexConverter implements IStringConverter<Index> {
    @Override
    public Index convert(String value) {
        if (!StringUtil.isNonZeroUnsignedInteger(value)) {
            throw new ParameterException("The index must be a non-zero unsigned integer.");
        }
        return Index.fromOneBased(Integer.parseInt(value));
    }
}

However, if I define a parameter in my command class like so, and I attempt to let JCommander parse the command <command word> 123 abc, now I get the ParameterException thrown because the "abc" is not an integer - but should JCommander instead realize that the arity of 2 is incorrect before attempting conversion?:

@Parameters(commandDescription = "...")
public class ExampleCommand extends Command {

    @Parameter(description = "...", required = true, converter = IndexConverter.class, arity = 1)
    private Index targetIndex;
    ...
}

My rationale for raising this issue is that the exception message is of the wrong specificity - the exception from the failed conversion has higher priority than the exception from the arity.

This is behavior is undesirable for my use case as it's more important to reject the incorrect arity than to inform the user of a failed conversion.

Could there be a way to configure JCommander to validate arities before conversions?

@mkarg
Copy link
Collaborator

mkarg commented Apr 28, 2024

@jontmy Did you find such configuration option in the source code? If not, do you agree to provide a PR for this bug?

@mkarg mkarg added the bug Bug label Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug
Projects
None yet
Development

No branches or pull requests

2 participants