Disabling the feature altogether
You can disable the expansion of the @
parameters via:
jc.setExpandAmpersat(false)
(as noted in this answer)
- OR ,
jc.setExpandAtSign(false)
(in some versions)
There is also a corresponding option on the builder
Using indirection with a file
If you absolutely rely on @
in your commandline parsing, and you expect that your users need to pass filenames or other parameters that start with @
, you could ask them to use indirection using extra files.
E.g. to pass filename @foo
to --file
, write @foo
on a single line of a text file, and pass that file via --file @textfile
. The expansion is not recursive.
This doesn't work in all situations. But you could convince yourself this is acceptable.
Escaping arguments starting with @
It should be noted that JCommander only expands @
if they start the argument. Knowing this, and assuming you already have some control over argument parsing in your program, you could leave the JCommander default behavior alone, but ask users to explicitly disable it per-argument, using an escape character prefix .
e.g. You could establish a convention where if a user wishes to pass a filename "@foo" (or say, a twitter handle) on the command line, they can prefix it with a backslash or a space: --file " @foo"
or --file "\\@foo"
. This will prevent the automatic expansion by JCommander, but then your code needs to detect and strip that extra character before further argument processing.