Building CLIAR — A simple drop-in Java class for parsing command-line arguments (Part 2)
Welcome to the second part of my rolling blog, where I start building CLIAR from the ground up-starting with the simplest possible parser—and see where it breaks. You can find the first part here a...

Source: DEV Community
Welcome to the second part of my rolling blog, where I start building CLIAR from the ground up-starting with the simplest possible parser—and see where it breaks. You can find the first part here and current snapshots of the code on my GitHub page. Getting started with Short Options To keep the interface simple, we start with a factory method: public static Cliar from(String[] args) { ... } With that in place, we can begin parsing the simplest possible arguments: single-character options. for (String arg : args) { if (arg.startsWith("-") && !arg.startsWith("--")) { char option = arg.charAt(1); // ... } } This allows basic usage like: myApp -a myApp -a -b -c However, passing each option separately is verbose. A common convention is to group them: myApp -abc This requires only a small change: for (String arg : args) { if (arg.startsWith("-") && !arg.startsWith("--")) { for (char chr : arg.substring(1).toCharArray()) { // ... } } } Long Options for Clarity An option charac