-
Notifications
You must be signed in to change notification settings - Fork 425
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
Synopsis should respect order
if specified (was: SortOption=false
does not work when mixing "attributes" and "setter" Options)
#1408
Comments
SortOption=false
does not work when mixing "attributes" and "setter" Options
Hi @sbernard31 you are correct, and this behaviour is documented in the user manual (14.5. Reordering Options):
Glad you already found the solution: you can sort options using (When you do that, I expect that the synopsis ordering will line up with the option list ordering.) |
Is there any action you want me to take related to this ticket? |
I totally missed it ? 🤦♂️
Nope it looks like this :
So finally maybe there is bug with the synopsis order ? I share the code if you want to try to reproduce on your own : @Command(name = "myCommand",
mixinStandardHelpOptions = true,
description = "A command with not well ordered option.",
sortOptions = false)
public class Example {
@Option(names = { "-d", "--option-d" }, defaultValue = "Default D", description = "Should be 1st", order = 1)
private void setD(String value) {
this.d = value;
}
private String d;
@Option(names = { "-c", "--option-c" }, defaultValue = "Default C", description = "Should be 2nd",order = 2)
private void setC(String value) {
this.c = value;
}
private String c;
@Option(names = { "-b", "--option-b" }, defaultValue = "Default B", description = "Should be 3rd", order = 3)
private String b;
@Option(names = { "-a", "--option-a" }, defaultValue = "Default A", description = "Should be 4th", order = 4)
private String a;
/* @Option(names = { "-d", "--option-d" }, defaultValue = "Default D", description = "Should be 1st")
private void setD(String value) {
this.d = value;
}
private String d;
@Option(names = { "-c", "--option-c" }, defaultValue = "Default C", description = "Should be 2nd")
private void setC(String value) {
this.c = value;
}
private String c;
@Option(names = { "-b", "--option-b" }, defaultValue = "Default B", description = "Should be 3rd")
private String b;
@Option(names = { "-a", "--option-a" }, defaultValue = "Default A", description = "Should be 4th")
private String a;*/
public static void main(String... args) {
Example example = new Example();
int exitCode = new CommandLine(example).execute(args);
System.exit(exitCode);
}
} |
Looks like for the synopsis, picocli by default sorts options using the Comparator returned by You are correct, the current implementation always sorts the synopsis options alphabetically and does not take sortOptions=false into account. You can change this via the Help API: similar to this example, you can have a custom IHelpFactory like this: static class MyHelpFactory implements IHelpFactory {
@Override
public Help create(CommandSpec commandSpec, ColorScheme colorScheme) {
return new Help(commandSpec, colorScheme) {
@Override
public String synopsis(int synopsisHeadingLength) {
// the simplest thing is to just do this:
return detailedSynopsis(synopsisHeadingLength,
createOrderComparatorIfNecessary(commandSpec.options()), true);
// but if you want to keep it more generic you can do this:
/*
if (!empty(commandSpec.usageMessage().customSynopsis())) { return customSynopsis(); }
Comparator<OptionSpec> optionSort =
return commandSpec.usageMessage().sortOptions()
? createShortOptionArityAndNameComparator()
: createOrderComparatorIfNecessary(commandSpec.options());
return commandSpec.usageMessage().abbreviateSynopsis() ? abbreviatedSynopsis()
: detailedSynopsis(synopsisHeadingLength, optionSort, true);
*/
}
};
}
} And you use it like this: public static void main(String[] args) {
CommandLine cmd = new CommandLine(new App());
cmd.setHelpFactory(new MyHelpFactory());
cmd.execute(args);
} |
Note to self: maybe related to #964. |
SortOption=false
does not work when mixing "attributes" and "setter" Optionsorder
if specified (was: SortOption=false
does not work when mixing "attributes" and "setter" Options)
A fix for the synopsis order has been implemented. This requires the |
The documentation§14.4. Unsorted Option List says :
I want to sort my options in the order they are declared and so use
sortOptions = false
.I faced a situation where the described behavior is not respected.
I firstly thought that I maybe find a bug, after lot of time trying to understand, I isolated the issue.
It seems it does not really works if you mixed "methods" and "attribitutes"
Knowing that I'm not so sure if this could really fixed so if this is really a bug.
Here is the code to reproduce :
And the output you get :
You can see that :
The workaround is using
@Option(order = <int>)
(see 14.5. Reordering Options)The text was updated successfully, but these errors were encountered: