Skip to content
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

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

Open
JavaDev99 opened this issue Aug 19, 2020 · 9 comments
Labels
theme: parser An issue or change related to the parser type: API 🔌 type: enhancement ✨

Comments

@JavaDev99
Copy link

JavaDev99 commented Aug 19, 2020

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
Copy link

r2mzes commented Aug 19, 2020

As far as I understand correctly it is a matter of not supporting "subcommands going up the hierarchy" that was asked about here: #454 (comment)

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
Copy link
Owner

remkop commented Aug 20, 2020

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

@remkop remkop changed the title I have a question regarding the subcommands Support repeatable subcommands going up the hierarchy (was: I have a question regarding the subcommands) Aug 20, 2020
@remkop remkop added theme: parser An issue or change related to the parser type: API 🔌 labels Feb 21, 2022
@sbernard31
Copy link
Contributor

I have this need too : #2133

@sbernard31
Copy link
Contributor

sbernard31 commented Oct 18, 2023

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

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"

@remkop
Copy link
Owner

remkop commented Oct 19, 2023

@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
Copy link
Contributor

sorry but I’m too swamped to work on picocli at the moment.

Of course as a user I am 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 command subcommandsRepeatable=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.

@sbernard31
Copy link
Contributor

Hi, @remkop, any news about that ?

@remkop
Copy link
Owner

remkop commented Mar 5, 2024

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

@sbernard31
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: parser An issue or change related to the parser type: API 🔌 type: enhancement ✨
Projects
None yet
Development

No branches or pull requests

4 participants