2

我有一种情况field1,我需要三个强制参数(field2和可以输入)。field3command namecreatelist

有些命令会有参数,有些则没有。我该如何处理?

我尝试了以下操作,但出现错误:

ArgGroup has no options or positional parameters, and no subgroups
public class CliParserArgs {
    @Option(names = {"--field1"}, required = true)
    private String field1;

    @Option(names = {"--field2"}, required = true)
    String field2;

    @Option(names={"--field3"}, required = true)
    String field3;

    @Option(names = {"-h", "--help"}, usageHelp = true) boolean help;

    class Create {
        private final String val;
        public Create(final String val) {
            this.val = val;
        }
    }

    class ListObjects {
        private final String val;
        public ListObjects(final String val) {
            this.val = val;
        }
    }

    @ArgGroup(heading = "Command", exclusive = true, multiplicity = "1")
    Create create;
    ListObjects listObjects;

    public static void main(String[] args) {
        CliParserArgs cliParserArgs = new CliParserArgs();
        CommandLine cmd = new CommandLine(cliParserArgs);
        CommandLine.ParseResult parseResult = cmd.parseArgs(args);
        System.err.println("parse results: " + parseResult.matchedArgs().toString());

        try {
        if (cmd.isUsageHelpRequested()) {
            cmd.usage(System.out);
        }
        } catch (CommandLine.ParameterException e) {
            System.err.println("error: " + e.getMessage());
            System.err.println(e.getStackTrace());

        }
    }
}
4

1 回答 1

4

听起来你想用subcommands创建一个命令。您可以在 picocli 中通过使用注释标记方法@Command创建单独的命令类并将其注册为父命令的子命令来执行此操作。如果您的子命令有很多选项,您可能希望为它创建一个单独的类。

创建子命令后,您需要调用用户指定的子命令的逻辑。您可以使用该CommandLine.parseArgs方法手动执行此操作,但这需要大量工作。我建议改用该CommandLine.execute方法

execute方法将解析用户输入,处理--help--version请求,处理无效的用户输入,最后(如果用户输入有效)调用用户指定的子命令的业务逻辑。它还将返回一个退出代码。

execute方法要求子命令是带@Command注释的方法或实现或的带@Command注释的类。RunnableCallable

下面是一个基于您的示例代码的示例,作为子命令实现。

@Command(name = "cli", version = "1.0",
    mixinStandardHelpOptions = true,
    subcommands = {Create.class, ListObjects.class})
public class Cli implements Runnable {
    @Option(names = {"--field1"}, required = true)
    private String field1;

    @Option(names = {"--field2"}, required = true)
    String field2;

    @Option(names={"--field3"}, required = true)
    String field3;

    // not needed because we have mixinStandardHelpOptions=true
    //@Option(names = {"-h", "--help"}, usageHelp = true) boolean help;

    public void run() {
        // business logic of the top-level cmd here
        System.out.println("hi, field1="+field1);
    }

    public static void main(String[] args) {
        int exitCode = new CommandLine(new Cli()).execute(args);
        System.exit(exitCode);
    }
}

@Command(name = "create", description = "create ...",
        mixinStandardHelpOptions = true, version = "1.0")
class Create implements Callable<Integer> {
    @Option(names = {"-x", "--times"}, description = "...")
    int x;

    @Override
    public Integer call() {
        // business logic for "create" here...
        return ok ? 0 : 1; // exit code support
    }
}

@Command(name = "list", description = "create ...",
        mixinStandardHelpOptions = true, version = "1.0")
class ListObjects implements Runnable {
    @Option(names = {"-x", "--times"}, description = "...")
    int x;

    @Override
    public void run() {
        // business logic for "list" here...
    }
}
于 2019-11-25T00:17:25.750 回答