简体   繁体   中英

PicoCLI : How to use @ArgGroup for a CommandLine.Command method

I have two options (-n and -t) under a command where if -n is used, then -t is required, but bothare not required. However, I keep getting an error about I am trying to send the options as a parameter to another method (with business logic) as a parameter.

Valid Usage:

agent.bat install -n -t <blahblah>
agent.bat install -t <blahblah> -n
agent.bat install -t <blah blah>
agent.bat install -t <----This is on interactive so it would ask for a parameter later

Invalid Usage:

agent.bat install -n
agent.bat install -n -t

Current output with valid usage:

agent.bat install -t
Missing required parameter: '<arg0>'
Usage: agent install [-hV] <arg0>
Setup or update the agent service program by install token.
      <arg0>
public class Agent implements Callable<Integer> {
    static class InstallArgs {
        @Option(names = {"-t", "--token"},
                order = 0,
                arity = "0..1",
                interactive = true,
                description = "The agent install token.",
                required = true) String installToken ;
        @Option(names = {"-n", "--noninteractive"},
                order = 1,
                description = "Sets installation to non-interactive",
                required = false) boolean nonInteractive ;
        public String toString() {
            return String.format("%s,%s", installToken, nonInteractive);
        }
    }

    private static String[] programArgs;
    @ArgGroup(exclusive = false, multiplicity = "1")
    @CommandLine.Command(name = AgentCommand.INSTALL_COMMAND, mixinStandardHelpOptions = true,
            description = "Setup or update the agent service program by install token.")
    void install(InstallArgs installArgs) {
        String[] installArgsValues = installArgs.toString().split(",");
        String installToken = installArgsValues[0];
        boolean nonInteractive = Boolean.parseBoolean(installArgsValues[1]);
        IcbProgram.initProgramMode(ProgramMode.INSTALL);
        MainService mainService = MainService.createInstallInstance(configFile, agentUserFile, backupAgentUserFile, installToken, nonInteractive);
    }

    public static void main(String... args) {
        if (ArgumentValidator.validateArgument(args)) {
            programArgs = args;
            int exitCode = new CommandLine(new Agent()).execute(args);
            System.exit(exitCode);
        } else 
            //Exit with usage error
            System.exit(ExitCode.USAGE);        
    }
}

Can you try using arity=1 for installToken ?

    static class InstallArgs {
        @Option(names = {"-t", "--token"},
                order = 0,
                arity = "1",
                interactive = true,
                description = "The agent install token.",
                required = true) String installToken ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM