picocli
picocli copied to clipboard
Support repeatable subcommands going up the hierarchy (was: I have a question regarding the subcommands)
We are trying to implement this code
public class TestCommands {
public static void main(String[] args) {
args = new String[]{
"-first",
"-second", "-secondFirstSub", "-secondSecondSub",
"-third", "-thirdFirstSub", "-thirdSecondSub", "-thirdThirdSub"
};
CommandLine commandLine = new CommandLine(new MainCommand());
commandLine.parseArgs(args);
}
}
@CommandLine.Command(
subcommandsRepeatable = true,
subcommands = {FirstCommand.class, SecondCommand.class, ThirdCommand.class}
)
class MainCommand {
}
@CommandLine.Command(name = "-first")
class FirstCommand {
}
@CommandLine.Command(name = "-second", subcommandsRepeatable = true, subcommands = {SecondFirstSubCommand.class, SecondSecondSubCommand.class})
class SecondCommand {
}
@CommandLine.Command(name = "-secondFirstSub")
class SecondFirstSubCommand {
}
@CommandLine.Command(name = "-secondSecondSub")
class SecondSecondSubCommand {
}
@CommandLine.Command(name = "-third", subcommandsRepeatable = true, subcommands = {ThirdFirstSubCommand.class, ThirdSecondSubCommand.class, ThirdThirdSubCommand.class})
class ThirdCommand {
}
@CommandLine.Command(name = "-thirdFirstSub")
class ThirdFirstSubCommand {
}
@CommandLine.Command(name = "-thirdSecondSub")
class ThirdSecondSubCommand {
}
@CommandLine.Command(name = "-thirdThirdSub")
class ThirdThirdSubCommand {
}
And we get the following error.
picocli.CommandLine$UnmatchedArgumentException: Unknown options: '-third', '-thirdFirstSub', '-thirdSecondSub', '-thirdThirdSub'
Could you tell us what solution may be for this
As far as I understand correctly it is a matter of not supporting "subcommands going up the hierarchy" that was asked about here: https://github.com/remkop/picocli/issues/454#issuecomment-580972877
If this is the case, would it be possible to enhance the picocli in a way that would allow for scenarios like the one posted above? It would be great to have this feature ;)
@JavaDev99, @r2mzes's comment is correct. Currently picocli's repeatable subcommands do not support this.
I will look into whether it is feasible to support this.
I have this need too : https://github.com/remkop/picocli/issues/2133
Must-Have Feature
I think a bit more about it and it seems to me that having a way to support subcommand going up the hierarchy
is really a must-have feature.
You could answer that this is still possible to flattening the commands hierarchy OR massively use options but this often lead to ugly CLI in complex use cases.
I feel this is like if GUI library doesn't allow you to create submenu (or submenu is allowed only as last element of menu) and so the alternative was "put all action in same menu"
Something smelly with Current Behavior :
I really think that current behavior is counterintuitive :thinking: .
From CLI user perspective (I mean users who use CLI, not developers who use picocli library), it will be really hard to understand that he :
- can do
toplevelcmd subcmd-A subcmd-B subsubB
- but can not do
toplevelcmd subcmd-B subsubB subcmd-A
I feel it would be clearer to allow both OR forbid both.
Imagine you have an image transformation tool.
You can do :
// convert to grayscale then save
java -jar jimage.jar
color -to-grayscale
save "image_before_transformation.png"
// convert to grayscale then save, then rotate then mirror
java -jar jimage.jar
color -to-grayscale
save "image_before_transformation.png"
transform rotate -degree 90
transform mirror -vertical
But you can not do :
// convert to grayscale then save, then rotate then mirror, then save again.
java -jar jimage.jar
color -to-grayscale
save "image_before_transformation.png"
transform rotate -degree 90
transform mirror -vertical
save "image_after_transformation.png"
@sbernard31 sorry but I’m too swamped to work on picocli at the moment.
For that last use case one idea is to add the save
command as a subcommand to transform also…
sorry but I’m too swamped to work on picocli at the moment.
Of course as a user I am disappointed :disappointed:. But I also manage an open source project, so I totally understand that you could haven't time now. Could I ask if that means : "there is no chance, I work on it in next week ? month ? or year ? or more ?"
I'm sorry if I seem too pushy but unfortunately users have only 1 power to impact project they depend on : discuss and provide argument hoping maintainer will agree.
For that last use case one idea is to add the save command as a subcommand to transform also…
Yep, but you need to :
- make
transform
commandsubcommandsRepeatable=true
(maybe you don't want to allow that) - AND each time you will add a new level 1 subcommand which as level 2 subcommand (like transform) you need to add all other level 1 subcommands to level 2 subcommands.... (and here there is only 2 levels)
Maybe you will succeed to have something OK from users point of view but now for sure your code is hard to maintain.
Hi, @remkop, any news about that ?
Hi @sbernard31, sorry but I cannot see myself working on this in the foreseeable future.
@remkop, ok I understand.
Eventually, if could try to work on it, if I do :
- is their any chance that you integrate the modification ? (or you didn't want to integrate that ?)
- could you provide assistance ?
- Should we try to implement this feature ? or should we try to find a way to make it possible to customize the behavior with code ?