【问题标题】:Enabling or Disabling Options during Runtime在运行时启用或禁用选项
【发布时间】:2019-08-13 16:07:21
【问题描述】:

我正在尝试使用 Picocli 制作交互式 cli,并希望在完成 a(n) 操作/要求后显示一些选项。有没有办法在不使用 CommandSpec 的情况下做到这一点?

之前显示的选项

@Option(names = {"-c","--chooseDevice"}, description = {"Choose Devices"})
    private boolean chooseDevice;

--
some code that will initialize a device
--

之后显示的选项

@Options(names = {,"-d", "--deviceCommand", description = "some device command")
    private boolean deviceCommand;

输出应该是

//before choosing device
-c  --chooseDevice "Choose Devices"

//after choosing device
-c  --chooseDevice   "Choose Devices"
-d  --deviceCommand  "some device command"

【问题讨论】:

    标签: java command-line-interface picocli


    【解决方案1】:

    可以在运行时更改选项的hidden 属性,但它确实需要使用编程API(如CommandSpec 类)。

    Picocli 4.0 添加了从CommandSpec 中删除选项的功能,因此您可以将其替换为具有不同hidden 属性值的选项副本。

    类似这样的:

    CommandLine cmd = new CommandLine(new MyApp());
    
    // replace the old "--device" option with a different one that is not hidden
    CommandSpec spec = cmd.getCommandSpec();
    OptionSpec old = spec.findOption("--device");
    OptionSpec newDeviceOption = OptionSpec.builder(old).hidden(false).build();
    spec.remove(old);
    spec.add(newDeviceOption);
    
    cmd.execute(args);
    

    请参阅GitHub issue #736 了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      相关资源
      最近更新 更多