Reputation: 99
I have set sortOptions to false for command but still, I am seeing command's options in sorted order in the Usage section. I want to see them in their declaration order.
Sample code
@Command(name = "application", subcommands = { AddSubCommand.class })
public class Sample implements Runnable {
@Spec CommandSpec spec;
@Override
public void run() {
throw new ParameterException(spec.commandLine(), "Specify a subcommand");
}
public static void main(String... args) {
new CommandLine(new Sample()).execute("add", "-h");
}
}
@Command(name = "add", sortOptions = false)
class AddSubCommand implements Runnable {
@Option(names = { "-h" }, usageHelp = true, hidden = true)
boolean helpFlag;
@Option(names = { "-i" }, required = true, description = "id")
int id;
@Option(names = { "-t" }, required = true, description = "type")
String type;
@Option(names = { "-c" }, required = true, description = "config")
String config;
public void run() {
// logic
}
}
Output
Usage: application add -c=<config> -i=<id> -t=<type>
-i=<id> id
-t=<type> type
-c=<config> configuration
Expectation
Usage: application add -i=<id> -c=<config> -t=<type>
-i=<id> id
-t=<type> type
-c=<config> configuration
Upvotes: 1
Views: 841
Reputation: 448
There now (PicoCLI 4.7.5) seems to be an additional parameter for this: sortSynopsis = false
.
@Command(
name = "add",
sortOptions = false,
sortSynopsis = false // ←
)
// […]
Upvotes: 1
Reputation: 36834
You are correct that options are sorted in the synopsis line, regardless of whether sortOptions = false
is specified. (The sortOptions
annotation attribute only affects the order of the options list section of the usage help message, not the synopsis.)
It is possible to customize the synopsis, but not with the annotations API; you will need to use the programmatic API.
Change the Sample
class main
method to the following:
public static void main(String... args) {
new CommandLine(new Sample())
.setHelpFactory(new UnsortedSynopsisHelpFactory())
.execute("add", "-h");
}
And add the following UnsortedSynopsisHelpFactory
class:
class UnsortedSynopsisHelpFactory implements CommandLine.IHelpFactory {
@Override
public CommandLine.Help create(CommandSpec commandSpec, ColorScheme colorScheme) {
return new CommandLine.Help(commandSpec, colorScheme) {
@Override
protected Ansi.Text createDetailedSynopsisOptionsText(
Collection<ArgSpec> done,
Comparator<OptionSpec> optionSort,
boolean clusterBooleanOptions) {
return super.createDetailedSynopsisOptionsText(
done,
null, // do not sort options in synopsis
clusterBooleanOptions);
}
};
}
}
This will give the following output:
Usage: application add -i=<id> -t=<type> -c=<config>
-i=<id> id
-t=<type> type
-c=<config> config
Thanks for raising this! I added an example Unsorted.java to the picocli-examples
module.
Upvotes: 2