Java command line "shell" with auto-complete - java

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

Check out JReadline and jline2.

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.

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.

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.

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

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.

Related

How to get git -log of all branches in Java?

I have a task to implement a program in Java (pure Java without 3rd party libraries) that reads a history of any git repository and puts the commits into tree data structure.
Could you give me any hints? How to read git log in Java without 3rd party libraries?
You might want to take a look at Processes and Threads and how to execute a command in the runtime. It does have some details and need fundamental understanding of java.lang.Runtime, java.io and some other relevant topics, so that I'd refrain to write a whole method here and recommend you to search for a good tutorial and also get the first idea from other questions here, like → getting output from executing a command line program

Java: Parse JavaScript & Flag Errors

I've been having terrible luck trying to get this to work, so I'm hopeful someone can help here.
In Java, I need to be able to take an HTML page with JavaScript within it and detect any JavaScript errors without, preferably without executing the JavaScript code.
I found this article:
Javascript parser for Java
And I've attempted to figure out how I'm supposed to use Caja to do this, but I'm having a difficult time finding any documentation with working examples of anything close to what I'm doing.
As a result I took a look at Nashorn also referenced in that article. I found a few examples which show how to execute JavaScript code from Java, but this doesn't process the whole HTML page. Even then, the execution doesn't seem to include the ability to validate common JavaScript functions (e.g. It hadn't heard of "alert").
Can anyone recommend something that might be able to do what I want, and point me in the right direction for their documentation or give me an example?
jshint as a standalone product seems to be a good fit for this:
it can run in java inside rhino (see https://github.com/jshint/jshint/)
a nodejs package exists (see https://www.npmjs.com/package/jshint)
it works with nashorn but it's quite tricky
I will only cover the technical difficulties around 3rd solution as I finally managed to make it work too...
Spoiler alert: "alert()" is not detected yet... Solution nb 2 will help there...
You first need to grab this specific release of jshint: https://github.com/jshint/jshint/releases/tag/2.4.4
Anything later than v2.7.0 will fail for now and I personally gave up patching intensively prototypes and namespaces... Releases from v2.4.4 until v2.6.3 work without modification but are limited in functionalities.
In the release notes, it's specifically written that "support for the Nashorn JavaScript engine" is working on this release. I'm using JDK8 nashorn 1.8.0_45 for this test.
Next step is to extract from this release this single file jshint-2.4.4/dist/jshint-rhino.js
Now you need to run nashorn/jjs in scripting mode and you need to be specific about the single file you wish to verify. In solution 2 (nodejs based) you can do multiple files or a complete hierarchy below a folder...
Create a simple file file.js:
function(){}
Now run the following command (please note the presence of -- ):
jjs -scripting jshint-rhino.js -- file.js
This will give you the following output:
Missing name in function declaration. (file.js:1:9)
> function(){}
So this covers the how to run jshint in a simple manner with nashorn... With the 3rd solution, at least you can find missing semicolons and several typical errors. But it's not a silver bullet and to me it's not a real alternative.
My personal preference would be to stick to solution 2 only. If you've the possibility to install either nodejs or iojs on your dev platform, go and grab https://www.npmjs.com/package/jshint. Not only will you be able to do more than the 3rd solution, you'll also be able to configure a jshintrc file as described at http://jshint.com/docs/

Realize modular architecture / simple plugin system

I wrote a simple program taking some commands from a XML file, executing it, checkings for errors of the commands, reporting back by mail etc. An entry for a command looks e.g. like this:
<command>C:\Program Files\Test\test.exe /xyz</command>
Now I want to introduce some kind of plugins:
<command myPlugin1="abc,1" myPlugin2="def">C:\Program Files\Test\test.exe /xyz</command>
I thought about writing the plugins as a Java class implementing some interface or subclassing some Plugin class, then having a method like
pluginExec(List<String> parameters, String command)
that gets passed the parameters (e.g. abc,1) and the command (C:\Program Files\Test\test.exe /xyz).
Then maybe take all attribute names of the command tag, therefore myPlugin1 and myPlugin2, then search for a class with the same name as the attribute on myapp.plugins namespace and call pluginExec(...).
Any better ideas on this? I guess with my idea I need some kind of reflection, is there a way without it? The application is a private tool, no need to be able to add plugins without recompiling the software. I just don't want to change the code everytime, I just want to plug in my new plugin class.
If your plugin based program you develop will not grow a lot in the next future then your design is fine.
However, if you expect it to grow and you expect to add multiple features to it than I suggest you try Java standards like OSGi.

Manipulating the output of GIT version control system from a Java program

I am invoking the Git executable from my Java program using the process builder class and running various git commands. Java program is being run from the command prompt.
However, I'm not finding a way to extract only a particular part of the output.
I would assume you use a StreamGobbler thread to get the input.
Just put some logic in there that filters the output.
like the others I would recommend the use of a java library, JGit allows many low-level operations if you need something very precise.
But maybe the Git command you are looking for is : "git diff commit_parent commit -U0" giving only the changes and no context.
You can also read the manual: "git help diff", you will find what Git offers.
But once again, I do not think it is a better idea to use a java Git library: what if the Git developers decide to change the output format tomorrow? Will you need to start over your program?

Java library for text/string processing simular to unix/linux utilities

I'm a java programmer. I use bash scripts a lot for text processing.
Utilities like grep,sed,awk,tr,wc,find, along with piping between commands gives such a powerful combination.
However bash programming lacks portability, testability and the more elegant programming constructs that exist in Java. It also makes it harder to integrate into our other Java products.
I was wondering if anyone knows of any Java text processing libraries out there which might offer what I'm looking for.
It would be so cool to be able to write:
Text.createFromFile("blah.txt).grep("-v","ERROR.*").sed("s/ERROR/blah/g").awk("print $1").writeTo("output.txt")
This might be pie-in-in-the-sky stuff. But thought I'd put the question out there anyway.
Unix4j implements some basic unix commands, mainly focussing on text-processing (with support for piping between commands): http://www.unix4j.org
Example (Ben's example, but without awk as this is not currently supported):
Unix4j.fromStrings("1:here is no error", "2:ERRORS everywhere", "3:another ERROR", "4:nothing").toFile("blah.txt");
Unix4j.fromFile("blah.txt").grep(Grep.Options.v, "ERROR.*").sed("s/ERROR/blah/g").toFile("output.txt");
Unix4j.fromFile("output.txt").toStdOut();
>>>
1:here is no error
4:nothing
Note:
the author of the question is involved in the unix4j project
Believe it or not, but I used embedded Ant for many of those tasks.
Update
Ant has Java api's that allow it to be called from Java projects. This is embedded mode. This is a reference to And Api 1.6.1. Distribution should include docs as well.
To use it, you would create new task object, set appropriate parameters and execute it just as you would in build.xml but via Java Api. Than you can run your task.
Something like
ReplaceRegExp regexp = new ReplaceRegExp();
regexp.setMatch("bla");
regexp.setFile(new File("inputFile"));
regexp.execute();
You may need to set up some other stuff as well.
Not sure if it solves your problem, but Ant has a lot of code to do things. Just search through docs.

Categories