0

I'm trying to make an interactive cli using Picocli, and want to have some options appear after a(n) action/requirement has been completed. Is there a way I can do this without using the CommandSpec?

Option to be shown before

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

--
some code that will initialize a device
--

Option to be show after

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

Output should be

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

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

Edmond Chu
  • 11
  • 1

1 Answers1

0

It is possible to change the hidden attribute of an option at runtime, but it does require using the programmatic API (like the CommandSpec class).

Picocli 4.0 added the ability to remove options from a CommandSpec, so you can replace it with a copy of the option that has a different value for its hidden attribute.

Something like this:

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);

Please see GitHub issue #736 for more details.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114