options without - prepended hyphen

73 views
Skip to first unread message

theonlygusti

unread,
Jan 22, 2017, 1:59:17 PM1/22/17
to jcommander
How can I create options that don't require a prepended hyphen?

private class CommandTemplate {
  @Parameter
  private List<String> parameters = new ArrayList<>();

  @Parameter(names = "help", help = true)
  private boolean help;
}

Doesn't seem to work, 

Cédric Beust ♔

unread,
Jan 22, 2017, 2:15:14 PM1/22/17
to jcommander
Options need to start with `-` right now (otherwise JCommander can't easily distinguish between an option and a main parameter).


-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gbakewell7

unread,
Jan 22, 2017, 2:38:56 PM1/22/17
to jcommander
But surely it can just take option/value pairings like: mainCommand
<option> <value> <option arity 2> <value> <value> <option> etc..

George Bakewell

unread,
Jan 22, 2017, 2:39:53 PM1/22/17
to jcommander
But surely it can just take option/value pairings like: mainCommand <option> <value> <option arity 2> <value> <value> <option> etc..

George Bakewell

unread,
Jan 22, 2017, 2:53:49 PM1/22/17
to jcommander
Then what happens if a value starts with `-`? Surely this must be a problem in the current implementation.

On 22 January 2017 at 19:14, Cédric Beust ♔ <ced...@beust.com> wrote:

Cédric Beust ♔

unread,
Jan 22, 2017, 3:04:28 PM1/22/17
to jcommander
It's certainly possible to improve the option detection. Right now, all it does is check if the word parsed starts with a `-`. Instead, it could instead compare it with all the option names declared in your annotations and if all of this fails, then the parameter is a main parameter.


-- 
Cédric

gbakewell7

unread,
Jan 22, 2017, 4:41:27 PM1/22/17
to jcommander
Any chance of this being implemented?

Cédric Beust ♔

unread,
Jan 22, 2017, 4:44:52 PM1/22/17
to jcommander
Not in the near future by me, but you are welcome to send a pull request if you are interested in implementing this yourself.


-- 
Cédric


On Sun, Jan 22, 2017 at 1:41 PM, gbakewell7 <gbake...@gmail.com> wrote:
Any chance of this being implemented?

theonlygusti

unread,
Jan 23, 2017, 3:44:26 AM1/23/17
to jcommander, ced...@beust.com
Could you point me at the relevant code?


On Sunday, 22 January 2017 21:44:52 UTC, Cédric Beust ♔ wrote:
Not in the near future by me, but you are welcome to send a pull request if you are interested in implementing this yourself.


-- 
Cédric


On Sun, Jan 22, 2017 at 1:41 PM, gbakewell7 <gbake...@gmail.com> wrote:
Any chance of this being implemented?

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

Cédric Beust ♔

unread,
Jan 24, 2017, 1:06:58 PM1/24/17
to jcommander
Actually I'm looking into this.


-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.

Cédric Beust ♔

unread,
Jan 24, 2017, 1:07:53 PM1/24/17
to jcommander

-- 
Cédric

Cédric Beust ♔

unread,
Jan 24, 2017, 3:52:34 PM1/24/17
to jcommander

I just deployed a new JCommander to JCenter which no longer requires options to have a prefix. As a consequence, the following test passes:

  public void noDash() {
    class Arguments {
      private int bar;

      @Parameter(names = { "bar", "foo" })
      private void setBar(int value) {
        bar = value;
      }

      @Parameter(names = "otherName")
      private String otherName;
    }

    Arguments a = new Arguments();
    new JCommander(a, "bar", "1");
    Assert.assertEquals(a.bar, 1);
  }

There might be a few edge cases here and there but all the tests are passing so I’m pretty sure there are no regressions.

File issues if you encounter any difficulties.

Thanks!


-- 
Cédric

gbakewell7

unread,
Jan 24, 2017, 3:55:26 PM1/24/17
to jcommander
Thank you so much Cédric for being so helpful! It's awesome that
you've managed to update it in such a short time, I haven't actually
tested it yet but I'll be sure to file an issue if I encounter any!
Thanks :)

Cédric Beust ♔

unread,
Jan 24, 2017, 4:24:33 PM1/24/17
to jcommander
This issue was brought up twice in the past few weeks (somebody else mentioned it before you) and it's been nagging me since then because there was no reason why JCommander should have relied on option prefixes in the first place.

And I was glad to see it was as easy to fix as I expected :)


-- 
Cédric


theonlygusti

unread,
Jan 24, 2017, 5:13:42 PM1/24/17
to jcommander, ced...@beust.com
Hey, so I've got a little problem since switching the dependency to 1.7.

I'm trying to create a help option without any prefix:

public class CommandTemplate {
  @Parameter
  private List<String> parameters = new ArrayList<>();
  
  @Parameter(names = "help", help = true)
  private boolean help;
}

public static void main(String[] args) {
  CommandTemplate template = new CommandTemplate();
  JCommander jcommander = new JCommander(template);
  jcommander.setProgramName("prog");
  jcommander.parse(args);
  
  if (template.help) {
    jcommander.usage();
  }
}

It doesn't work,

cannot find symbol
symbol:   method help()

On Tuesday, 24 January 2017 21:24:33 UTC, Cédric Beust ♔ wrote:
This issue was brought up twice in the past few weeks (somebody else mentioned it before you) and it's been nagging me since then because there was no reason why JCommander should have relied on option prefixes in the first place.

And I was glad to see it was as easy to fix as I expected :)


-- 
Cédric


On Tue, Jan 24, 2017 at 12:55 PM, gbakewell7 <gbake...@gmail.com> wrote:
Thank you so much Cédric for being so helpful! It's awesome that
you've managed to update it in such a short time, I haven't actually
tested it yet but I'll be sure to file an issue if I encounter any!
Thanks :)

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

theonlygusti

unread,
Jan 24, 2017, 5:15:13 PM1/24/17
to jcommander, ced...@beust.com
[ERROR] ~/com/prog/Commander.java:[41,40] cannot find symbol
[ERROR] symbol:   method help()
[ERROR] location: @interface com.beust.jcommander.Parameter

On Tuesday, 24 January 2017 21:24:33 UTC, Cédric Beust ♔ wrote:
This issue was brought up twice in the past few weeks (somebody else mentioned it before you) and it's been nagging me since then because there was no reason why JCommander should have relied on option prefixes in the first place.

And I was glad to see it was as easy to fix as I expected :)


-- 
Cédric


On Tue, Jan 24, 2017 at 12:55 PM, gbakewell7 <gbake...@gmail.com> wrote:
Thank you so much Cédric for being so helpful! It's awesome that
you've managed to update it in such a short time, I haven't actually
tested it yet but I'll be sure to file an issue if I encounter any!
Thanks :)

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

Cédric Beust ♔

unread,
Jan 24, 2017, 6:18:46 PM1/24/17
to jcommander

Seems to work for me. I pasted your code verbatim and when I run it:

Usage: prog [options]
  Options:
    help

The compilation error appears to be in your code?

  static class CommandTemplate {
    @Parameter
    private List<String> parameters = new ArrayList<>();

    @Parameter(names = "help", help = true)
    private boolean help;
  }

  @Test(enabled = false)
  public static void main(String[] args) {
    CommandTemplate template = new CommandTemplate();
    JCommander jcommander = new JCommander(template);
    jcommander.setProgramName("prog");
    jcommander.parse("help");

    if (template.help) {
      jcommander.usage();
    }
  }

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.

theonlygusti

unread,
Jan 25, 2017, 6:47:31 AM1/25/17
to jcommander, ced...@beust.com
Well, for some reason it won't work for me and the error being thrown is on this exact line of code:

@Parameter(names = "help", help = true)

I have no idea why it would work for you and not for me, but here's the lines I've added to my pom.xml relevant to this issue:

    <dependency>
      <groupId>com.beust</groupId>
      <artifactId>jcommander</artifactId>
      <version>1.7</version>
    </dependency>

    <repository>
      <id>jcenter</id>
      <url>https://jcenter.bintray.com/</url>
    </repository>

Cédric Beust ♔

unread,
Jan 25, 2017, 9:39:54 AM1/25/17
to jcommander
The latest version is 1.61. 1.7 is a very old version.

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages