cbeust / jcommander

Command line parsing framework for Java
Apache License 2.0
1.96k stars 334 forks source link

Cannot use @-syntax in conjunction with arguments starting with literal @ #501

Closed c-kunz closed 2 months ago

c-kunz commented 4 years ago

Description

One cannot use the @-syntax in conjunction with parameters that may contain a literal @ at the beginning.

Steps to reproduce:

In this repository: https://github.com/c-kunz/jcommander-conflicting-at-syntax

git clone https://github.com/c-kunz/jcommander-conflicting-at-syntax
cd jcommander-conflicting-at-syntax
mvn clean package
java -jar target/demo-jar-with-dependencies.jar -password @X

Expected result

JCommander is able to parse the argument beginning with the literal @ while also being able to parse additional arguments using the @-syntax.

Actual result

JCommander interprets the argument starting with a literal @ as a file containing additional arguments (@-syntax):

Exception in thread "main" com.beust.jcommander.ParameterException: Could not read file X: java.nio.file.NoSuchFileException: X

Workaround

One has to settle for the following workaround, basically disabling the @-syntax altogether:

JCommander cli = ...
cli.setExpandAtSign(false);

Proposal

Since this is an either-or situation, either disabling @-syntax or prohibiting arguments starting with a literal @, it would be nice to be able to for example specify parameters that lookup additional arguments in a file or specify parameters that ignore leading @ signs.

c-kunz commented 4 years ago

Issue https://github.com/cbeust/jcommander/issues/462 might be related:

So... there isn't any externally defined concept of the @file?

mkarg commented 4 years ago

Proposed Solution

JCommander should have a new option which allows using @ as part of parameter values but still recognizes it as file name prefix when used in parameter names. Hence, the old behavior would be the default, but new applications could opt-in to the new behavior.

mkarg commented 4 years ago

See also #330.

HennyNile commented 3 years ago

Hello, I want to try to work on this issue. I plan to modify the judge rule of @-syntax which is inspired by that file name can not start with / in windows and linux.

mkarg commented 2 months ago

@c-kunz Thank you for reporting this bug! It was fixed some minutes ago, and will be contained in either 1.83.1 or 1.84 (whatever comes first).