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.89k stars 420 forks source link

Interactive shell with hierarchy navigation #520

Closed DmitryAndreyeff closed 5 years ago

DmitryAndreyeff commented 5 years ago

Hi! I'm looking for a way to build interactive shell for managing the configuration of a large system. The configuration has hierarchical structure, and I would like to provide user experience very similar to file system navigation where most used commands will be ls and cd. I would like to have autocompletion based on current "directory" content, and shell prompt to change dynamically to reflect current path in the hierarchy. Is this achievable with picocli? Thanks!

remkop commented 5 years ago

I believe so, yes.

Please take a look at the picocli-shell-jline2 module: it provides a component to integrate picocli commands with the jline interactive shell. By default this component generates completion candidates based on the options and positional parameters for the command. It passes these candidates to a JLine API, and JLine will display these candidates when the end user hits <TAB> in the shell.

Each option or positional parameter of a picocli @Command can have custom completion candidates: all you need to do is specify a class that implements Iterable<String>. The simplest thing to do is to subclass ArrayList with some fixed values, but a more sophisticated completion candidates provider could return all files in the current directory, etc.

The rest is providing your business logic in the picocli commands, and perhaps some polishing to make the JLine interface look and feel the way you want.

DmitryAndreyeff commented 5 years ago

Thank you for the quick response! I will look into it.

remkop commented 5 years ago

@DmitryAndreyeff Were you able to make progress? Do you have follow-up questions, or can this ticket be closed?

DmitryAndreyeff commented 5 years ago

Hi, I don't have questions for now. Thank you for assistance!

bobtiernay-okta commented 5 years ago

@remkop Having a lot of fun with this module 💯 !

One question though, why did you go with jline2 vs jline3? Was it a licensing concern?

remkop commented 5 years ago

JLine2 requires only Java5, like picocli. Also, I had the Completer code ready from my contributions to the Micronaut CLI.

JLine3 requires Java 8. Planning to add a JLine 3 module later.

If you feel like contributing a JLine 3-based Completer, we can add another module for JLine 3. Pull requests welcome!