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

Subcommand aliases #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions args4j/src/org/kohsuke/args4j/spi/SubCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
String name();

/**
* Aliases for the command.
*/
String[] aliases() default { };

/**
* The implementation class of this sub command.
*
Expand Down
3 changes: 2 additions & 1 deletion args4j/src/org/kohsuke/args4j/spi/SubCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.kohsuke.args4j.OptionDef;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.ResourceBundle;

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ public int parseArguments(Parameters params) throws CmdLineException {
String subCmd = params.getParameter(0);

for (SubCommand c : commands.value()) {
if (c.name().equals(subCmd)) {
if (c.name().equals(subCmd) || Arrays.asList(c.aliases()).contains(subCmd)) {
setter.addValue(subCommand(c,params));
return params.size(); // consume all the remaining tokens
}
Expand Down
7 changes: 6 additions & 1 deletion args4j/test/org/kohsuke/args4j/SubCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class Foo {
@Argument(handler= SubCommandHandler.class)
@SubCommands({
@SubCommand(name="cmd1",impl=Cmd1.class),
@SubCommand(name="cmd2",impl=Cmd2.class)
@SubCommand(name="cmd2", aliases = {"c2a", "c2b", "c2c"}, impl=Cmd2.class)
})
SubCmd value;

Expand Down Expand Up @@ -46,5 +46,10 @@ public void testCmd2() throws Exception {
parser.parseArgument("cmd2");
assertTrue(testObject.value instanceof Cmd2);
}

public void testCmd2Alias() throws Exception {
parser.parseArgument("c2c");
assertTrue(testObject.value instanceof Cmd2);
}
}