简体   繁体   中英

Java command line “shell” with auto-complete

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?

ie 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

查看JReadlinejline2

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.

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.

The picocli Java command line parsing library recently added autocomplete functionality. Its prime use case is creating command line tools, but I've seen it used embedded in a shell-like application. What matters is not how one reads from System.in, but parsing the subcommands, options and parameters from the user input.

You may also like how it enables you to use ANSI colors and styles in the usage help message. You may be able to leverage this in your shell application.

在此输入图像描述

Disclaimer: I am the author and therefore biased.

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.

在这个方面迟到了派对,但我会加入CrashCliche

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM