GitHub sees 1,247 forks for Picocli alone. Yet here we are, Part 2 of CLIAR, a bare-bones Java class daring to parse command-line args without a single dependency.
Look, I’ve covered Java tooling since the JVM was a pup. And every few years, some dev decides to roll their own CLI parser. Why? Because libs like JCommander or Args4j feel like overkill — or worse, someone else’s opinion on conventions.
CLIAR’s creator kicks off with a factory: public static Cliar from(String[] args). Simple. Dead simple. No annotations, no magic. Just a loop chewing through args.
First up, short options. -a, -b c. That loop checks startsWith("-") && !startsWith("--"), grabs the char. Easy. But verbose — myApp -a -b -c? Nobody types that.
So, bundle ‘em: -abc. One tweak: loop over substring(1).toCharArray(). Boom. Now it’s Unix-y, like ls -la.
“An option character should reflect its meaning, like -a for all or -v for verbose. But what if you have options that start with the same letter, like cut and copy?”
Smart callout there. Enter long options: --verbose, --all. Check -- first, substring(2). Order matters — miss it, and longs never trigger. Subtle bug waiting to bite noobs.
Here’s the thing. Positional args next. No prefix? It’s required input, order-based. Like cp file1 file2. TODO in code, but obvious.
Flags are binary — present or nah. Values? --color=red. Stick = to glue ‘em. No space, no ambiguity. red won’t masquerade as positional.
Short options? No values. Rule: longs only. Keeps it predictable. No -a value drama in bundles.
Why Build CLIAR When Picocli Exists?
Cynical me asks: who’s bankrolling this? Nobody. It’s a solo dev’s itch-scratcher, GitHub snapshots included. (Link in Part 1, naturally.) But dig deeper — Java’s CLI scene exploded post-Spring Boot, yet JDK lacks built-in parsing. Oracle? Too busy with GraalVM hype.
Historical parallel: Unix getopt(1) in 1980. C folks hacked it for decades before POSIX blessed optarg. Java? 1995 release, still no stdlib parser. CLIAR echoes that scrappy spirit — zero deps, drop-in class.
But edge cases creep in. arg.startsWith("--") && (2 < arg.length())? Covers --no-op, skips --. Good. Yet -?*/, --n00p, -3 parse fine. Sensible? Nope. Restrict to letters later, promises the post.
Single loop handles all. Flags, longs, values, positionals. Smells like god method. Next part refactors — delegate to parsers. Wise.
Single sentence: Scaling this solo loop? Recipe for regret.
And values — --color=(&3? Parses, validates later. Flexible, risky. Tools like curl --data @file need stdin (-), end-of-options (--). Ignored for now. MVP mindset.
Does CLIAR Beat Java’s CLI Libraries?
Bold prediction: CLIAR won’t dethrone Picocli’s 10k+ stars. Why? Devs crave annotations — @Option(names = "--color") String color;. CLIAR? Manual map population. Educational gold, production? Meh.
Picocli bundles shorts (-abc), values (-a val), even -- terminator. CLIAR v0.2? Flags only, longs with =vals. No --color red (space-separated). Strict by design — or laziness?
Critique the spin: “This article was written with the help of an LLM”. Honest. But tech content? All my calls. Props. Unlike VC-funded “AI-native” vaporware.
Real-world test. Imagine git --color=always status. CLIAR handles. docker run -it --rm alpine. -it bundles, no vals — fine. But docker build -t image .? -t=image short-no-val rule blocks. Fail.
That’s the rub. Simplicity sacrifices POSIX-ish flexibility. Short opts with vals? Common in tar -xf file.tar. CLIAR says no. Devs hack around or grab Picocli.
Wander a bit: Remember Apache Commons CLI? Died slow. JOpt Simple? Abandoned. Java CLI parsers rot — too niche, bikesheddy. CLIAR’s purity? Refreshing. Forces you understand args, not blackbox.
Dense para time. Start with loop: long-first check prevents short bleed; = split via indexOf, substrings clean; bundle chars assume no vals (enforced); positionals catchall. But accumulate: validation, help (-h?), versioning (-V). Loop bloats. Refactor teases delegation — shortParser(), longParser(eqIndex), positionalCollector(). Builder pattern? Nah, factory pure.
Punchy: Love the GitHub snapshots. Transparent evolution.
Skepticism peaks: Who makes money? Nobody. Pure open-source joy. In Kubernetes era — YAML everywhere — CLI purity feels quaint. Yet servers spin on java -jar app.jar --port=8080 data/dir. Solid basics endure.
Unique insight: CLIAR mirrors React’s rise — ditch jQuery bloat for vanilla JS hooks. Java CLIs could use a ‘useCLI’ primitive. JDK 25? Please.
What Edge Cases Did CLIAR Miss?
-- end-marker: cmd -- opt1 -- --pos. Post-double-dash? All positional. Ignored.
Stdin -: cat - file. Special positional. Later.
Quotes? Escapes? ls -- "file name". Positional splits on space — String[] args already tokenized. Shell handles.
Numbers? --port=8080 val as String, parseInt caller-side. Good.
Booleans? Flags imply true. --no-color? Negative longs later?
Six-sentence deep dive. Creator eyes letters-only keys — anti --n00p. Vals flexible. No regex yet — substring city. Single loop? Cyclomatic complexity climbs. Part 3 delegates: extractShorts(), parseLongWithVal(), collectPositionals(). Test-driven? GitHub lacks JUnit. Add ‘em.
Medium: Conventions matter. GNU standards: shorts bundle, longs spaced or =, positionals ordered.
Fragment: CLIAR — 80% there.
Real Talk: CLIAR for Your Next Tool?
Drop-in? Yes. Cliar cli = Cliar.from(args); boolean verbose = cli.has('v'); String color = cli.get("color"); Infer from code.
Prod? Pair with validation lib. Micrometer? Nah.
I’ve seen CLI hell in Jenkins plugins, Maven mojos. CLIAR sidesteps annotation ceremony. For scripts, toys — perfect.
Prediction: Forks incoming for Spring Boot starters. Or stalls at 10 stars.
Wrapping messy: Love the rolling blog. Breaks ground-up, honest.
**
🧬 Related Insights
- Read more: Claude’s New MCP Server Knows Your Usage Habits Better Than You Do
- Read more: AI Marketing: How Real Estate Agents Are Scoring 47% More Qualified Leads
Frequently Asked Questions**
What is CLIAR Java parser?
CLIAR’s a minimal, dependency-free Java class for parsing CLI args — flags, bundled shorts, long opts with values, positionals.
How does CLIAR handle short option bundles?
Treats -abc as three flags: -a, -b, -c. No values on shorts, keeps it dead simple.
Is CLIAR production-ready for Java CLI tools?
Great for learning or tiny tools. For complex apps, Picocli edges it with annotations and full POSIX support.