Comments (2)
I tried to use the SystemRegistry#execute
to execute the line, and it works fine.
But I set up the ExecutionStrategy
in the subcommand(ex: cmd), this strategy wasn't called, why not the picocli doesn't check if the subcommand strategy exists.
from picocli.
This is a workaround by using the preRun
lifecycle:
public class CommanderFactory {
static class CommandExecutionStrategy implements IExecutionStrategy {
private int preRun(ParseResult parseResult) {
Object userObject = parseResult.commandSpec().userObject();
if (userObject instanceof CommandHook) {
int exitCode = ((CommandHook) userObject).preRun();
if (exitCode != 0) {
return exitCode;
}
}
if (parseResult.hasSubcommand()) {
return preRun(parseResult.subcommand());
}
return 0;
}
@Override
public int execute(ParseResult parseResult) throws ExecutionException, ParameterException {
int preRunCode = preRun(parseResult);
if (preRunCode != 0) {
return preRunCode;
}
return new CommandLine.RunLast().execute(parseResult);
}
}
/**
* createRootCommanderWithHook is used for the root command, which supports the hook feature.
* @param object Command class or object.
* @param defaultValueProvider Default value provider of command.
* @return Picocli commander.
*/
public static CommandLine createRootCommanderWithHook(Object object, IDefaultValueProvider defaultValueProvider) {
CommandLine commandLine = new CommandLine(object);
commandLine.setExecutionStrategy(new CommandExecutionStrategy());
if (defaultValueProvider != null) {
commandLine.setDefaultValueProvider(defaultValueProvider);
}
return commandLine;
}
}
public interface CommandHook {
/**
* The preRun hook is used to execute commands before.
*
* @return If the return value is 0, continue to the next stage.
*/
default int preRun() {
return 0;
}
}
@Command(name = "demo", subcommands = {
Demo.Sub1.class
})
class Demo implements CommandHook {
@Override
public int preRun() {
System.out.println("demo preRun");
return 0;
}
@Command(name = "sub1")
static class Sub1 implements CommandHook, Runnable {
@Override
public int preRun() {
System.out.println("sub1 preRun");
return 0;
}
@Override
public void run() {
System.out.println("sub1 run");
}
}
public static void main(String[] args) {
CommandLine commandLine = CommanderFactory.createRootCommanderWithHook(Demo.class,null);
commandLine.execute("sub1");
}
}
Log:
demo preRun
sub1 preRun
sub1 run
Process finished with exit code 0
from picocli.
Related Issues (20)
- DuplicateOptionAnnotationsException on using negatable option in ArgGroup HOT 1
- Recursive subcommand could give better error HOT 1
- Synopsis not alphabetical sort for ArgGroups
- "the class has no constructor" for an inner class HOT 3
- Duplicate help output for ArgGroup from a Mixin HOT 1
- Mixin-with-prefix capability? HOT 4
- Leverage Spring boot DI with Picocli HOT 1
- [Question] Is it possible to get suggestion list dynamically? HOT 5
- [Question] Is it possible to create section headers in help text without using nested fields in the options class? HOT 2
- native-image Runtime Error HOT 3
- Access to ArgSpec and OptionSpec setter/getter fields and methods HOT 4
- [Question] Custom property file with default values HOT 5
- AutoComplete with jline3 is showing hidden commands. HOT 3
- Synopsis in tailtip widget is limited to a certain length
- Strict null handling mode HOT 5
- Module info for picocli`s Spring Boot Starter not included in the released jar HOT 7
- Syntax error in auto-completion script
- Improve navigation for picocli-generated manual pages
- Mode for treating empty string the same as unspecified HOT 5
- Potential issue reading passwords in Windows - this stops CTRL-C from working?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from picocli.