Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declared order of options incorrect when mixing methods and fields #508

Closed
zegelin opened this issue Oct 8, 2018 · 3 comments
Closed

Declared order of options incorrect when mixing methods and fields #508

zegelin opened this issue Oct 8, 2018 · 3 comments

Comments

@zegelin
Copy link

zegelin commented Oct 8, 2018

With @Command(sortOptions = false) the following option definitions don't display in their declaration order:

    @CommandLine.Option(names = "--foo")
    public void something(final boolean value) {}

    @CommandLine.Option(names = "--bar")
    public String bar;

    @CommandLine.Option(names = "--baz")
    public void somethingElse(final boolean value) {}

The help output:

      --bar=<bar>
      --foo
      --baz

Additionally, the following declaration seems to have unpredictable order -- it varies run to run.

    static class FirstMixin {
        @CommandLine.Option(names = {"-e", "--exclude"}, paramLabel = "EXCLUSION", arity = "1..*")
        void setExclusions(final Set<String> values) {}

        @CommandLine.Option(names = {"-g", "--global-labels"}, paramLabel = "LABEL", split = ",")
        public Set<String> globalLabels;

        @CommandLine.Option(names = {"--no-global-labels"})
        public void setNoGlobalLabels(final boolean noGlobalLabels) {}

        @CommandLine.Option(names = {"--no-fast-float"})
        public void setNoFastFloat(final boolean noFastFloat) {}

        @CommandLine.Option(names = {"--enable-per-thread-cpu-times"})
        public boolean perThreadTimingEnabled;
    }

    @CommandLine.Mixin
    FirstMixin firstMixin;

First run help:

-g, --global-labels=LABEL[,LABEL...]

      --enable-per-thread-cpu-times

      --no-global-labels
      --no-fast-float
  -e, --exclude=EXCLUSION...

Second run:

  -g, --global-labels=LABEL[,LABEL...]

      --enable-per-thread-cpu-times

  -e, --exclude=EXCLUSION...

      --no-global-labels
      --no-fast-float

This is with version: 3.6.1

@remkop remkop added this to the 3.7 milestone Oct 8, 2018
@remkop
Copy link
Owner

remkop commented Oct 8, 2018

Good find, thanks for reporting it!
Looks like declaration order only works when all options are annotated fields, but the order gets mixed up when annotated methods come into play.

The best way to solve this is to let application authors explicitly specify the order. Perhaps with an index attribute, like this:

@Option(names = “-x”, index = 2) int x;
@Option(names = “-y”, index = 0) int y;
@Option(names = “-z”, index = 1) int z;

I’m open to other suggestions, including a better name than index

@remkop
Copy link
Owner

remkop commented Oct 8, 2018

Perhaps order is a better name.

@remkop
Copy link
Owner

remkop commented Dec 21, 2018

Added @Option(order = <int>) attribute to allow explicit control of option ordering in the usage help message.

Pushed to master. Please verify.
This will be in the upcoming 3.9 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants