Git Product home page Git Product logo

freeargparser-java's Introduction

FreeArgParser

FreeArgParser library provides API for parsing command line arguments passed to programs written in Java.
Basically Commons CLI but with array support, cleaner syntax and with GNU GPLv3 license.

Example help message on parsing error:

 Short Name  Long Name       Argument Type  Is Required  Description
  -i          --int           int            +            your_description1
  -d          --double        double         +            your_description2
  -s          --string        String                      your_description3
  -b          --boolean       boolean        +            your_description4
  -na         --noarg         no argument                 your_description5
  -intA       --intArray      int[]          +            your_description6
  -strA       --stringArray   String[]       +            your_description7
  -douA       --doubleArray   double[]                    your_description8
  -booA       --booleanArray  boolean[]                   your_description9

Implementation

After importing the .jar, classes used can be found in me.krypek.freeargparser
Here's a simple two argument program:
ParsedData data = new ParserBuilder()
   .add("i", 	"int",	  true,	false, 	ArgType.Int,	"integer")
   .add("d", 	"double", true,	false,  ArgType.Double, "double")
   .parse(args);
		
int i = data.getInt("i");
double d = data.getDouble("double");

Explaining

Firstable, you have to create new ParserBuilder instance.
Then, you add all arguments you want, by calling the add function.
Example:
.add("a",        "argument", true,        false,       ArgType.Int,   "description")
//   short name, long name,  is_required, = style, argument type, description


You can access the argument either by:

  • Short name, e.g. -a
  • Long name, e.g. --argument


When is_required is set to true, the argument has to be provided.
If it's not, the program will display the help message with the error and terminate.

If = style is set to true, value will have to be connected with argument name using = char, not space char.
Example:

--testArg=2

insted of

--testArg 2


Description is what will be displayed in help message about the argument.

Argument types:

ArgType Usage Comments
None no arguments
Int 123
Double 3.14159
Boolean true, false, 1, 0 1=true, 0=false
String pancake, "pan cake" "pancake" == pancake
IntArray [1, 2, 3, 4]
DoubleArray [3.141, 0.25, 0.5]
BooleanArray [false, 0, true, 1, 0] 1=true, 0=false
StringArray [hi, "hell o", spa ce] "hell o" == hell o

If you want to put " (quote char) in your string, type \ before it.

After adding all of your wanted arguments, parse them.
Parse method accepts both String and String[].

.parse(args);

Alternativly, you can build it and then parse it, both methods do the same.

.build().parse(args);

Accessing data

After you have sucessfully parsed your arguments into ParsedData class, you can access the data using it's short name. Example for getting int:
int i = parseddata.getInt("i");

And same for other data types.
If the argument isn't required and wasn't provided by the user, FreeArgParser will throw an exception.
To avoid that, use

int i = parseddata.getIntOrDef("i", 1);

If an argument has ArgType set to None, you need to
boolean doesExist = parseddata.has("na");

in order to check if it was provided.


Here's an example program utilizing all argument types:

final String input = """
	-i 1\
	-d=2.3 \
	-s hgfgf \
	-b=true \
	-na \
	--intArray=[1, 2, 3, 4, 5] \
	-strA [hello guys, "hi bro", yooooo] \
	-douA=[2.1, 1.3, 3.7, 7] \
	-booA [0, 0, 0, 1, false, true] """;

ParsedData data = new ParserBuilder()
	.add("i", 	"int", 		true,	false, 	ArgType.Int, 		"description1")
	.add("d", 	"double", 	true,	true,  	ArgType.Double,		"description2")
	.add("s", 	"string", 	true,	false,  ArgType.String,		"description3")
	.add("b", 	"boolean", 	true,	true,  	ArgType.Boolean,	"description4")
	.add("na", 	"noarg",	false,	false, 	ArgType.None, 		"description5")
	.add("intA",	"intArray", 	false,	true,  	ArgType.IntArray, 	"description6")
	.add("strA",	"stringArray", 	true,	false,  ArgType.StringArray, 	"description7")
	.add("douA",	"doubleArray", 	true,	true,  	ArgType.DoubleArray, 	"description8")
	.add("booA",	"booleanArray",	true,	false,  ArgType.BooleanArray,	"description9")
	.parse(input);


System.out.println("\nint:\t\t"+data.getIntOrDef("i", 1)+
	"\ndouble:\t\t"+data.getDouble("d")+
	"\nstring:\t\t"+data.getString("s")+
	"\nboolean:\t"+data.getBoolean("b")+
	"\nnoarg:\t\t"+data.has("na")+
	"\nintA:\t\t"+Arrays.toString(data.getIntArray("intA"))+
	"\nstrA:\t\t"+Arrays.toString(data.getStringArray("strA"))+
	"\ndouA:\t\t"+Arrays.toString(data.getDoubleArray("douA"))+
	"\nbooA:\t\t"+Arrays.toString(data.getBooleanArray("booA"))  );

Output:

int:		1
double:		2.3
string:		hgfgf
boolean:	true
noarg:		true
intA:		[1, 2, 3, 4, 5]
strA:		[hello guys, hi bro, yooooo]
douA:		[2.1, 1.3, 3.7, 7.0]
booA:		[false, false, false, true, false, true]




License

Licensed under GNU GPLv3 or later

freeargparser-java's People

Contributors

krypciak avatar krypciak-zz avatar

Watchers

 avatar

Forkers

fusa4218

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.