-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- 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
src/test/java/picocli/Issue1125_1538_OptionNameOrSubcommandAsOptionValue.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; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
import java.util.concurrent.Callable; | ||
|
||
import org.junit.Test; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.IParameterPreprocessor; | ||
import picocli.CommandLine.Model.ArgSpec; | ||
import picocli.CommandLine.Model.CommandSpec; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.ParameterException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class Issue1125_1538_OptionNameOrSubcommandAsOptionValue { | ||
@Command(name = "mycommand") | ||
static class MyCommand implements Callable<Integer> { | ||
|
||
@Option(names = "-output", preprocessor = MyPreprocessor.class) | ||
File output = new File("."); | ||
|
||
static class MyPreprocessor implements IParameterPreprocessor { | ||
public boolean preprocess(Stack<String> args, CommandSpec commandSpec, ArgSpec argSpec, Map<String, Object> info) { | ||
if (args.isEmpty()) { | ||
throw new ParameterException(commandSpec.commandLine(), "Error: option '-output' requires a parameter"); | ||
} | ||
String arg = args.pop(); | ||
//System.err.printf("Setting %s to '%s'%n", argSpec, arg); | ||
argSpec.setValue(new File(arg)); | ||
return true; | ||
} | ||
} | ||
|
||
public Integer call() { | ||
return 11; | ||
} | ||
|
||
@Command(name = "mySubcommand") | ||
public int mySubcommand() { | ||
return 13; | ||
} | ||
} | ||
|
||
@Test | ||
public void testSubcommandAsOptionName() { | ||
MyCommand obj = new MyCommand(); | ||
CommandLine cmdLine = new CommandLine(obj); | ||
int exitCode = cmdLine.execute("-output", "abc", "mySubcommand"); | ||
assertEquals(13, exitCode); | ||
assertEquals("abc", obj.output.getName()); | ||
|
||
exitCode = cmdLine.execute("-output=mySubcommand", "mySubcommand"); | ||
assertEquals(13, exitCode); | ||
assertEquals("mySubcommand", obj.output.getName()); | ||
|
||
exitCode = cmdLine.execute("-output", "mySubcommand", "mySubcommand"); | ||
assertEquals(13, exitCode); | ||
assertEquals("mySubcommand", obj.output.getName()); | ||
} | ||
} |