remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
https://picocli.info
Apache License 2.0
4.92k stars 425 forks source link

Support repeatable subcommands going up the hierarchy (was: I have a question regarding the subcommands) #1149

Open JavaDev99 opened 4 years ago

JavaDev99 commented 4 years ago

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

r2mzes commented 4 years ago

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

remkop commented 4 years ago

@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.

sbernard31 commented 1 year ago

I have this need too : https://github.com/remkop/picocli/issues/2133

sbernard31 commented 1 year ago

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 :

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"
remkop commented 1 year ago

@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…

sbernard31 commented 1 year ago

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 :

Maybe you will succeed to have something OK from users point of view but now for sure your code is hard to maintain.

sbernard31 commented 8 months ago

Hi, @remkop, any news about that ?

remkop commented 8 months ago

Hi @sbernard31, sorry but I cannot see myself working on this in the foreseeable future.

sbernard31 commented 8 months ago

@remkop, ok I understand.

Eventually, if could try to work on it, if I do :