cbeust/jcommander — Issue #99: Commands starting with two hyphens not possible
Author: lefou
Created: 2012-01-12 17:14:40+00:00
State: open

With JCommander verison 1.20 it is not possible to have commands that starts with two hyphens.

Example Scala-Code:

object MyApp {

class BaseArgs {
@Parameter(names = Array("-h", "--help"), description = "Show this help screen")
var help: Boolean = _
@Parameter(names = Array("--version", "-version"), description = "Show the program version")
var version: Boolean = _
}

@Parameters(commandNames = Array("--configure"))
class ConfigureArgs {
@Parameter(names = Array("--maven-bin"))
var mavenBinary: String = _
}

def main(args: Array[String]) {
val baseArgs = new BaseArgs()
val confConfig = new ConfigureArgs()
val jc = new JCommander()
jc.addObject(baseArgs)
jc.addCommand(confConfig))

jc.parse(args: _*)

if(baseArgs.help) {
jc.usage
System.exit(0)
}
}

Output of `java -cp test.jar --help`:

Usage:

[options] [command] [command options]
Options:
-h, --help Show this help screen
Default: false
--version, -version Show the program version
Default: false
Commands:
--configure
Usage: --configure [options]
Options:
--maven-bin

Whereas given the command `java -cp test.jar --configure` gives an exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Caused by: com.beust.jcommander.ParameterException: Unknown option: --configure
at com.beust.jcommander.JCommander.parseValues(JCommander.java:573)
at com.beust.jcommander.JCommander.parse(JCommander.java:257)
at com.beust.jcommander.JCommander.parse(JCommander.java:240)

When naming the command "configure" instead of "--configure" everything works as expected.