1/ Tips and tricks for writing java cli apps in 2020, a thread...
2/ There are a lot of little small things to deal with when writing cli apps that looks and behave nicely - the following are some of the things I've used and/or found out while writing jbang...
3/ Parsing command line arguments is on its face a simple task but if you want to do it well you do benefit greatly from a library to do it rather than manually munging the String[] you get handed.

There are a bunch of them but my clear winner is @picocli. It just does it all...
4/ By all I mean that it is annotation based making it declarative and kinda handholds you into a command based class/method structure that is very close to what I would most likely do anyway. It is well documented supports native compilation and has @QuarkusIO extension...
5/ When doing command line apps, especially scripty/automation things you need to run processes and albeit Java has a decent API in ProcessBuilder https://github.com/zeroturnaround/zt-exec simplifies the interaction greatly...
6/ Example of zt-exec's simplicity is you in almost a oneline can invoke a process and iterate over its output in a lambda.

Doing that reliably with ProcessBuilder requires way more scaffolding. Thus zt-exec gets a +1 from here!
7/ Modifying, reading and writing files Java 8+ Files.* and Path apis now do fairly well. move, copy, symbolic linking, reading and writing files is in Java 8 and especially Java 11 trivial one liners.
8/ JSON processing - is an area I'm really not yet happy about; I tend to end up using fastjackson. That said I still feel like I haven't found the upfront ease of use and simplicity json/api's deserves in Java...like where is the restassured for non-testing ?...
9/ Testing - lots of ways to test your code; traditional junit tests goes a long way but I've found that doing some black box testing interacting with the cli as a user/script would do finds a ton of issues. Here @KarateDSL has a great approach and what I would recommend...
10/ @KarateDSL for now just provide the basics to execute command lines; but with a bit of setup code https://github.com/jbangdev/jbang/blob/master/itests/karate-config.js you can write compact "cucumber" style tests checking for exit codes, std out and err output and more.
11/ Packaging of your apps - a simple wrapper script + a jar is quite sufficient today. @jbangdev has a working windows cmd and linux/osx shell if you want to be inspired - but alternatively you can use something like @QuarkusIO to get a natively built command line binary...
12/ ...or if simply just writing a small script to do some automation using the wast java ecosystem of libraries then @jbangdev at http://jbang.dev  would be perfect. With jbang installed the scripts run directly and it will install the right version of java for the user.
13/ Building - its 2020; any app/library should have CI/CD setup and for cross-platform building and testing out of a github repository GitHub actions wins hands down. Its ease of use and ability to test and run on Linux, OSX and Windows makes it a winner for cli's.
14/ Packaging and Distributions is probably worth a whole thread in itself, but just know that it is easier than you think. For @jbangdev it is fully automated to publish after a release the binary as standalone, windows scoop and chocolatey, osx brew and various linux formats...
15/ ...all done via github actions - explore the https://github.com/jbangdev/jbang/tree/master/.github/workflows on how it is done...
16/ I probably forgot a bunch of things but there it it is - tips and tricks for writing java based command line apps in 2020.
You can follow @maxandersen.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: