Java command line “shell” with auto-complete

两盒软妹~` 提交于 2021-01-22 07:00:28

问题


I need to create a Java command line to that will be invoked remotely on a server. It will need to:

  • Read "lines" of text from the user.
  • Recognise if the user presses the "tab" key to facilitate auto-complete.
  • Recognise if the user presses the "up/down" keys for history.

Before I go off and roll my own, Is anyone aware of a Java library that might facilitate all or part of this?

i.e. From the command line in ssh it might look like this:

bob> java -jar MyTool.jar

MyTool Started.
Please enter command:

> server1 startup
server starting...
server started

> server2 load accounts
Done

> server3 shutdown
Complete

>quit


回答1:


Check out JReadline and jline2.




回答2:


Update: picocli-shell-jline2 and picocli-shell-jline3 combine the strength of JLine 2 and 3 with picocli.

Picocli allows you to write commands (and subcommands) with options and positional parameters using very little code, and the library will generate JLine 2 and 3 command completers for all commands.

It uses ANSI colors and styles in the usage help message, and has many other unique features like negatable options, repeatable nested argument groups, variable interpolation and more.

Disclaimer: I am the author and therefore biased.




回答3:


it seems like you're trying to ask 3 different questions at once and don't know what you really want an answer to.

  • accepting user input and providing auto-complete is trivial and i highly doubt you will find a generalized library for such a task

  • parsing complex bash-like statements sounds like something cool to have and a library may exist to do that, but i don't think it would give you much headroom to create your own set of bash-like instructions. (especially considering you say it needs to be more sophisticated than anything you could do as a bash script - which is a tall order)

  • parsing simple user input as if it was a command-line input or command is also rather trivial, and if this is what you are looking for, you should look at this possible duplicate: How to parse command line arguments in Java?

i recommend restructuring your question to be more specific in exactly what you are looking for and to avoid putting emphasis on the trivial task of "auto-complete" and simply accepting the users input in a text box.




回答4:


Have you taken a look at the BeanShell? It doesn't act like a shell proper (like bash or csh) but it'll let you type java commands like an interpreter and you can use tab to autocomplete.

I've only used the 1.X versions of bean shell but they always open a window for you so it's not something you can run inside an existing shell.




回答5:


I assume you mean something similar to the python interpreter. The reason there is not equivalent in Java is because Java needs to be compiled to bytecode before it can be executed.

If you are looking for something with good auto-complete capabilities. I would recommend eclipse or netbeans. They also compile your application automatically, allowing you to quickly run your code once you are done writing it.

Hope that helps.




回答6:


Bit late to the party on this one, but I'd add Crash and Cliche to the mix.




回答7:


Apache Karaf has a command shell which can be used as a library to build a command shell for other applications. You create classes to represent commands, and use annotations to specify the options to the commands. The Karaf library provides tab completion, history and line editing, and the ability to run interactively or read in files or command line arguments to execute as a script.

I found out about it here and have used it in my own project; it works quite well. I can't compare it to any of the others as I haven't used them.



来源:https://stackoverflow.com/questions/12595270/java-command-line-shell-with-auto-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!