~skiqqy/JOpts

Opt arg parsing for java
147a82c6 — Stephen Cochrane 1 year, 5 months ago
Added script to help with authoring releases
c06a9713 — Stephen Cochrane 1 year, 8 months ago
Small tweak to usage output
d0308db7 — Stephen Cochrane 1 year, 8 months ago
Updated pom and readme

clone

read-only
https://git.sr.ht/~skiqqy/JOpts
read/write
git@git.sr.ht:~skiqqy/JOpts

You can also use your local clone with git send-email.

#jopts

Simple opt args for java

#Installing

Too make use of this lib, follow the instructions found here.

Alternatively, you can download the latest jar directly.

#Usage

JOpts constructor takes 2 arguments, an opt string, and the arguments passed to the program (in that order)

The opt string has a specic format:

  • A semi colon seperated list of comma seperated values.
  • The comma seperated values all map to the same opt arg
  • The comma seperated values all begin with an implicit -
    • This means that the opt string 'v' maps to '-v', and '-v' maps to '--v' and so on

An example progra is shown below, notice how the opt string is

v, -value MAPPING TO -v, --value on the command line

h, -help MAPPING TO -h, --help on the command line

import xyz.skiqqy.jopts.JOpts;

public class Test {
	public static void main(String... argv) {
		JOpts jo = new JOpts("v,-value:;h,-help", argv); // Construct + parse

		String value = jo.getElse("-value", "Default Value");
		boolean help = jo.is("h");

		System.out.print(help + ", ");
		System.out.println(value);

		// Command line: $ JOpts --help # prints: true, Default Value
		// Command line: $ JOpts -h # prints: true, Default Value

		// Command line: $ JOpts -v test # prints: false, test 
		// Command line: $ JOpts --value test # prints: false, test 

		// Command line: $ JOpts --value # Throws an exception

		// Command line: $ JOpts --value test -h # prints: true, test
		// Command line: $ JOpts -v test -h # prints: true, test
		// Command line: $ JOpts -h -v test # prints: true, test
	}
}

#Notes

License: