-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime control of hidden options #736
Comments
Changing the model is currently not easy. |
I had a look and I need to make some changes to the My goal is to change things so that you can customize like this: import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help;
import picocli.CommandLine.Help.ColorScheme;
import picocli.CommandLine.IHelpFactory;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.Option;
@Command(name = "show-hidden-options")
public class ShowHiddenOptions {
@Option(names = "--visible") String visible;
@Option(names = "--hidden", hidden = true) String hidden;
public static void main(String[] args) {
final boolean IS_MAC_OS = System.getProperty("os.name").toLowerCase().contains("mac");
CommandLine cmd = new CommandLine(new ShowHiddenOptions());
cmd.setHelpFactory(new IHelpFactory() {
@Override
public Help create(CommandSpec commandSpec, ColorScheme colorScheme) {
return new Help(commandSpec, colorScheme) {
@Override
protected boolean isHidden(OptionSpec option) {
return option.hidden() && !IS_MAC_OS;
}
};
}
});
cmd.usage(System.out);
}
} I hope to have something for you later this week. |
Sorry for the delay. Allowing applications to change the model is a more general and more powerful solution. At the moment Something like this: CommandLine cmd = new CommandLine(new MyApp());
// replace the old "--open" option with a different one that is not hidden on Darwin
CommandSpec spec = cmd.getCommandSpec();
OptionSpec old = spec.findOption("--open");
OptionSpec newOpenOption = OptionSpec.builder(old).hidden(!isDarwin).build();
spec.remove(old);
spec.add(newOpenOption);
cmd.execute(args); |
I added a See also this test. |
Is there a way to accomplish the same but with an ArgSpec that is part of an ArgGroup? When removing the ArgSpec from the CommandSpec: public CommandSpec remove(ArgSpec arg) {
if (arg.group() != null) {
throw new UnsupportedOperationException("Cannot remove ArgSpec that is part of an ArgGroup");
}
... As far as I can see the ArgGroup is immutable as well. Is there any workaround? Thank you in advance. |
@JPedroBorges Currently there’s no way to modify argument groups. Can you raise a separate ticket for this? |
Is there a way to control the
hidden
attribute of a command line option at runtime? The use case I'm working with requires that an option should only be available to the user when executing the application on a specific operating system. For example:The above example would be ideal, although the compiler complains that the value for the
hidden
attribute must be a constant expression. I'm guessing it has to do with the way the annotation processor generates the code at compile time.Since the above isn't supported, can I get at the CommandLine.Option object and toggle the hidden attribute at runtime, let's say like in the constructor of my object?
Any feedback is welcome. Thanks in advance!
The text was updated successfully, but these errors were encountered: