【问题标题】:picocli CLI parser: hide the Java variable name while rendering the outputpicocli CLI 解析器:在渲染输出时隐藏 Java 变量名
【发布时间】:2021-03-15 17:32:35
【问题描述】:

我正在使用picocli 来显示我的命令行应用程序的用法。 我无法做到的是隐藏出现在打印输出中的 Java 变量的名称,从我的角度来看,它看起来很丑。

这是我CommandLine.Option的配置:

@CommandLine.Option(
        names = {"-a", "--abc"},
        description = "Please, hide the Java variable name...")
private String xxx;

这就是它的渲染方式:

-a, --abc=<xxx>   Please, hide the Java variable name...

如您所见,Java 变量的名称出现在等号之后:&lt;xxx&gt;

我想隐藏它,像这样:

-a, --abc   Please, hide the Java variable name...

我检查了 API,但看不到任何与此相关的内容。 有什么办法可以关掉吗?

【问题讨论】:

    标签: picocli


    【解决方案1】:

    picocli 注释 API 不提供此功能,但可以通过覆盖帮助 API 并插入您自己的选项渲染器来实现此目的。例如:

    public class HideOptionParams {
    
    @Option(names = {"-u", "--user"}, defaultValue = "${user.name}",
            description = "The connecting user name.")
    private String user;
    
    @Option(names = {"-p", "--password"}, interactive = true,
            description = "Password for the user.")
    private String password;
    
    @Option(names = {"-o", "--port"}, defaultValue = "12345",
            description = "Listening port, default is ${DEFAULT-VALUE}.")
    private int port;
    
    public static void main(String[] args) {
        CommandLine cmd = new CommandLine(new HideOptionParams());
        cmd.setHelpFactory(new IHelpFactory() {
            public Help create(final CommandSpec commandSpec, ColorScheme colorScheme) {
                return new Help(commandSpec, colorScheme) {
                    @Override
                    public IOptionRenderer createDefaultOptionRenderer() {
                        return new IOptionRenderer() {
                            public Text[][] render(OptionSpec option,
                                                   IParamLabelRenderer ignored,
                                                   ColorScheme scheme) {
                                return makeOptionList(option, scheme);
                            }
                        };
                    }
                };
            }
        });
        cmd.usage(System.out);
    }
    
    private static Text[][] makeOptionList(OptionSpec option, ColorScheme scheme) {
        String shortOption = option.shortestName(); // assumes every option has a short option
        String longOption = option.longestName(); // assumes every option has a short and a long option
    
        if (option.negatable()) { // ok to omit if you don't have negatable options
            INegatableOptionTransformer transformer =
                    option.command().negatableOptionTransformer();
            shortOption = transformer.makeSynopsis(shortOption, option.command());
            longOption = transformer.makeSynopsis(longOption, option.command());
        }
    
        // assume one line of description text (may contain embedded %n line separators)
        String[] description = option.description();
        Text[] descriptionFirstLines = scheme.text(description[0]).splitLines();
    
        Text EMPTY = Ansi.OFF.text("");
        List<Text[]> result = new ArrayList<Text[]>();
        result.add(new Text[]{
                scheme.optionText(String.valueOf(
                        option.command().usageMessage().requiredOptionMarker())),
                scheme.optionText(shortOption),
                scheme.text(","), // we assume every option has a short and a long name
                scheme.optionText(longOption), // just the option name without parameter
                descriptionFirstLines[0]});
        for (int i = 1; i < descriptionFirstLines.length; i++) {
            result.add(new Text[]{EMPTY, EMPTY, EMPTY, EMPTY, descriptionFirstLines[i]});
        }
        // if @Command(showDefaultValues = true) was set, append line with default value
        if (option.command().usageMessage().showDefaultValues()) {
            Text defaultValue = scheme.text("  Default: " + option.defaultValueString(true));
            result.add(new Text[]{EMPTY, EMPTY, EMPTY, EMPTY, defaultValue});
        }
        return result.toArray(new Text[result.size()][]);
    }
    }
    

    这会显示以下使用帮助信息:

    Usage: <main class> [-p] [-o=<port>] [-u=<user>]
      -o, --port          Listening port, default is 12345.
      -p, --password      Password for the user.
      -u, --user          The connecting user name.
    

    请注意,选项列表中省略了参数标签,但仍显示在概要中。如果您不想在概要中显示任何参数标签,您可以在 @Command 注释中指定 custom synopsis,请参阅用户手册中的此部分。

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      相关资源
      最近更新 更多