expandDynamicArg expands nonDynamicArgs

Open Beginner friendly
#439 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
java
Domain
cli

Research direction

Start in src/main/java/com/beust/jcommander/JCommander.java around the expandDynamicArgs logic at the linked line. Reproduce the case with -abc as a parameter and -ab as a dynamic parameter, then inspect how the assignment string is handled. Done means -abc is not incorrectly split into -ab and c, while valid dynamic arguments still expand correctly.

Written by the indexing model from the issue text.

Description

bug

If you have a parameter called -abc and a dynamic parameter -ab.
-abc will be expanded to "-ab" "c"

And behold you'll get an exception since "c" is not a valid argument for dynamic parameter.

Checking if arg should be expanded/split
https://github.com/cbeust/jcommander/blob/9f251415677106b683e4bbaf82123f0e5930cdce/src/main/java/com/beust/jcommander/JCommander.java#L472
"-abc".startsWith("-ab") && !"-abc".equals("-ab")

adding a validation that the assigment string exist in the arg should solve the problem.

if (arg.startsWith(name) && !arg.equals(name) && arg.contains(pd.getParameter().getAssignment()) {

But should probably be extracted to a method either in JCommander or a public boolean handlesArg(arg) in WrappedParameter which could handle the name validation and remove some of the nesting in expandDynamicArgs.

Dominant language
Java
Stars
2k
Forks
342
Avg merge
4h 10m
Merged PRs (30d)
4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cbeust/jcommander

All issues in cbeust/jcommander

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.