-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#805] add example for alphabetically sorting subcommands by subclass…
…ing `Help`
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
picocli-examples/src/main/java/picocli/examples/customhelp/AlphabeticSubcommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package picocli.examples.customhelp; | ||
|
||
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() {} | ||
} |