-
Notifications
You must be signed in to change notification settings - Fork 426
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
Using TreeMap instead of LinkedHashMap, to sort subcommands alphabeti… #805
Conversation
…cally in usage message
Hi @frontfact, thank you for the PR! To be honest, I’m not sure that this is a good change: Your proposal would limit applications to only alphabetical order, which doesn’t seem to be a good thing. Or am I missing something? |
Hi
Unfortunately, this is not possible in my case: I have 2 different applications that share common commands. I cannot list commands in the alphabetical order.
I did not see that, can you tell me how to do that ? Or point me to the documentation section explaining how to achieve alphabetical ordering of subcommands. Thank you very much |
I thought subcommands are listed in the order that they are declared. So you can control the ordering, no? |
Yes, I see. Thank you for your time, have a nice day! |
Ok, I see now. |
I believe that we can meet your requirements with a custom This ticket has an (fairly complex) example. A custom |
@frontfact, I found an easier solution: you can subclass Here is a full example: import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help;
import picocli.CommandLine.HelpCommand;
import picocli.CommandLine.IHelpFactory;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Spec;
import java.util.Map;
import java.util.TreeMap;
/**
* This class has subcommands that are not declared alphabetically.
* We want the help for this class to show the subcommands alphabetically.
*/
@Command(name = "alphabetic",
description = "a command that shows subcommands sorted alphabetically",
subcommands = {Charlie.class, Bravo.class, Alpha.class, HelpCommand.class})
public class AlphabeticSubcommands implements Runnable {
@Spec CommandSpec spec;
@Override
public void run() {
spec.commandLine().usage(System.out);
}
public static void main(String[] args) {
CommandLine commandLine = new CommandLine(new AlphabeticSubcommands());
commandLine.setHelpFactory(new IHelpFactory() {
@Override
public Help create(CommandSpec commandSpec, Help.ColorScheme colorScheme) {
return new Help(commandSpec, colorScheme) {
/**
* Returns a sorted map of the subcommands.
*/
@Override
protected Map<String, Help> subcommands() {
return new TreeMap<>(super.subcommands());
}
};
}
});
commandLine.execute(args);
}
}
@Command(name = "charlie", description = "my name starts with C")
class Charlie implements Runnable {
public void run() {}
}
@Command(name = "bravo", description = "my name starts with B")
class Bravo implements Runnable {
public void run() {}
}
@Command(name = "alpha", description = "my name starts with A")
class Alpha implements Runnable {
public void run() {}
} Output:
Does this meet your requirements? |
That works perfectly, thank you very much ! |
Great, glad to hear that! |
Using TreeMap instead of LinkedHashMap, to sort subcommands alphabetically in usage message.
Tell me if that breaks something.
Below is a snippet to toy with the expected behaviour (with/without ordering).
`
public class TestApplication {
}
`