From ea52a75cfb6454d11b66440bc20d18a686612fab Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Fri, 2 Aug 2019 17:06:56 +0530 Subject: [PATCH 1/3] Updated README.md --- README.md | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/README.md b/README.md index 90a0fdb..47bdedf 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,99 @@ Then add JArgs to your `dependencies` list tag: ``` +Usage Example +------------- + +#### Creating and parsing + +``` +public static void main( String[] args ) { + + // First, create a CmdLineParser + CmdLineParser parser = new CmdLineParser(); + + // Add Options + // Boolean Options like debug, and verbose do not have any associated values + Option debug = parser.addBooleanOption('d', "debug"); // Option with a short name and long name + + Option verbose = parser.addBooleanOption("verbose"); // Option with just a long name + + // The Options below have a value associated with it + Option size = parser.addIntegerOption('s', "size"); + Option fraction = parser.addDoubleOption('f', "fraction"); + + + // To parse the Options + try { + parser.parse(args); + } catch (CmdLineParser.OptionException e) { + System.err.println(e.getMessage()); + System.exit(-1); + } + + // Getting values + Boolean debugValue = parser.getOptionValue(debug); + + // Setting a default value if no input is provided + Integer sizeValue = parser.getOptionValue(size, new Integer(10)); +} +``` + +#### Collection input +Parameters can be specified more than once and can be saved into a collection + +For example, you can pass multiple listItems and save into a single collection + +###### Program Arguments +``` +--verbose --listItem Foo --listItem Bar +``` + +``` +{ + Option listItem = parser.addIntegerOption("listItem"); + Collection fractionValues = parser.getOptionValues(listItem); + . + . +} +``` + + +#### Values starting with '-' +Although Options keys start with '-', it's possible to pass negative numbers, also works for any string which starts with '-' + +For example, you can pass negative numbers and negative fractions + +###### Program Arguments +``` +--verbose -n -100 --double -20.3 +``` + +``` +{ + Option size = parser.addIntegerOption('n', "number"); + Option fraction = parser.addDoubleOption('d', "double"); + + Integer sizeValue = parser.getOptionValue(size); + Double d = parser.getOptionValue(fraction); + . + . +} +``` + +#### Collecting other remaining command line arguments + +``` +--verbose --debug -n 100 -d 8.3 -d 8.4 -- -other -value + +``` + +``` +String[] otherArgs = parser.getRemainingArgs(); +``` + + + Documentation ------------- From c48eaaaf6bbfa751fdb34457790f5eb9ac826ee4 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Fri, 2 Aug 2019 17:13:14 +0530 Subject: [PATCH 2/3] Update README.md --- README.md | 66 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 47bdedf..2d82332 100644 --- a/README.md +++ b/README.md @@ -84,33 +84,33 @@ Usage Example ``` public static void main( String[] args ) { - // First, create a CmdLineParser - CmdLineParser parser = new CmdLineParser(); + // First, create a CmdLineParser + CmdLineParser parser = new CmdLineParser(); - // Add Options - // Boolean Options like debug, and verbose do not have any associated values - Option debug = parser.addBooleanOption('d', "debug"); // Option with a short name and long name + // Add Options + // Boolean Options like debug, and verbose do not have any associated values + Option debug = parser.addBooleanOption('d', "debug"); // Option with a short name and long name - Option verbose = parser.addBooleanOption("verbose"); // Option with just a long name + Option verbose = parser.addBooleanOption("verbose"); // Option with just a long name - // The Options below have a value associated with it - Option size = parser.addIntegerOption('s', "size"); - Option fraction = parser.addDoubleOption('f', "fraction"); + // The Options below have a value associated with it + Option size = parser.addIntegerOption('s', "size"); + Option fraction = parser.addDoubleOption('f', "fraction"); - // To parse the Options - try { - parser.parse(args); - } catch (CmdLineParser.OptionException e) { - System.err.println(e.getMessage()); - System.exit(-1); - } - - // Getting values - Boolean debugValue = parser.getOptionValue(debug); - - // Setting a default value if no input is provided - Integer sizeValue = parser.getOptionValue(size, new Integer(10)); + // To parse the Options + try { + parser.parse(args); + } catch (CmdLineParser.OptionException e) { + System.err.println(e.getMessage()); + System.exit(-1); + } + + // Getting values + Boolean debugValue = parser.getOptionValue(debug); + + // Setting a default value if no input is provided + Integer sizeValue = parser.getOptionValue(size, new Integer(10)); } ``` @@ -126,10 +126,10 @@ For example, you can pass multiple listItems and save into a single collection ``` { - Option listItem = parser.addIntegerOption("listItem"); - Collection fractionValues = parser.getOptionValues(listItem); - . - . + Option listItem = parser.addIntegerOption("listItem"); + Collection fractionValues = parser.getOptionValues(listItem); + . + . } ``` @@ -146,13 +146,13 @@ For example, you can pass negative numbers and negative fractions ``` { - Option size = parser.addIntegerOption('n', "number"); - Option fraction = parser.addDoubleOption('d', "double"); - - Integer sizeValue = parser.getOptionValue(size); - Double d = parser.getOptionValue(fraction); - . - . + Option size = parser.addIntegerOption('n', "number"); + Option fraction = parser.addDoubleOption('d', "double"); + + Integer sizeValue = parser.getOptionValue(size); + Double d = parser.getOptionValue(fraction); + . + . } ``` From 27d6db9644b2c67984904809c006fdc9ac12f3d8 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Fri, 2 Aug 2019 17:18:01 +0530 Subject: [PATCH 3/3] Updated README.md --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2d82332..fd40577 100644 --- a/README.md +++ b/README.md @@ -128,8 +128,7 @@ For example, you can pass multiple listItems and save into a single collection { Option listItem = parser.addIntegerOption("listItem"); Collection fractionValues = parser.getOptionValues(listItem); - . - . + ... } ``` @@ -151,20 +150,24 @@ For example, you can pass negative numbers and negative fractions Integer sizeValue = parser.getOptionValue(size); Double d = parser.getOptionValue(fraction); - . - . + ... } ``` #### Collecting other remaining command line arguments + +##### Remaining command line arguments can be captured by separating with '--' ``` --verbose --debug -n 100 -d 8.3 -d 8.4 -- -other -value ``` ``` -String[] otherArgs = parser.getRemainingArgs(); +{ + String[] otherArgs = parser.getRemainingArgs(); + ... +} ```