Coder Social home page Coder Social logo

Comments (2)

nodece avatar nodece commented on September 22, 2024

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.

nodece avatar nodece commented on September 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.