Skip to content
This repository was archived by the owner on May 16, 2022. It is now read-only.

Commit 954ce80

Browse files
authored
Update README.md
Change table of contents reference
1 parent c923aa7 commit 954ce80

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ I have needed an argument parser for some java projects. There are some librarie
1111
- [Get Argument Values ](#get_argument_values )
1212
- [Put All Together](#put_all_together)
1313
- [Further Examples](#further_examples)
14-
## Scope Of Functions
1514
<a name="scope_of_functions"></a>
15+
## Scope Of Functions
1616
- Define arguments needed for your console applications
1717
- Integer Arguments
1818
- Double Arguments
@@ -21,17 +21,17 @@ I have needed an argument parser for some java projects. There are some librarie
2121
- Define whether an argument is required
2222
- Define a default value
2323
- Define different rules for this argument
24-
## How To Use It
2524
<a name="how_to_use_it"></a>
25+
## How To Use It
2626
It's that simple: Import the JAR file (`from 'release/ArgParser_X.X'`) or the packages and classes into your project. Now you can create an `ArgumentParser` object and define your arguments. Finally, you call the parse method and pass the arguments and get the result. A more detailed description of how to use ArgParser can be found in the following sections.
27-
### Create The Main Object
2827
<a name="create_the_main_object"></a>
28+
### Create The Main Object
2929
Before the arguments are defined, a `ArgumentParser` object is required. Here `bool1` defines whether the program should be terminated if the user uses a help argument. `bool2` defines whether an exception should be thrown if a required argument is not given. `bool3` defines whether an exception should be thrown if the user enters an undefined argument.
3030
```
3131
ArgumentParser argumentParser = new ArgumentParser(bool1, bool2, bool3);
3232
```
33-
### Definition Simple Arguments
3433
<a name="definition_simple_arguments"></a>
34+
### Definition Simple Arguments
3535
The arguments are then defined. In the following example a `StringArgumentDefinition` is created. It could also be an `IntegerArgumentDefinition`, `DoubleArgumentDefinition` or `FlagArgumentDefinition`. The parameters are the same for all four. The respective argument is assigned one or more prefixes in the first parameter, e.g. `new String[]{"-s", "-src", "-source"}`. In `argumentDescription` a string can be set which describes the argument e.g. `"The image source."`. `isRequired` specifies whether this argument must be set by the user or is optional. In `defaultValue` is deposited which value is valid for this argument by default.
3636
```
3737
StringArgumentDefinition sad = new StringArgumentDefinition(prefixes, argumentDescription, isRequired, defaultValue)
@@ -40,8 +40,8 @@ Then the argument definition can be added to the argument parser.
4040
```
4141
argumentParser.add(sad);
4242
```
43-
### Definition Arguments With Rules
4443
<a name="definition_arguments_with_rules"></a>
44+
### Definition Arguments With Rules
4545
Let's assume that we want to add rules to the previously created argument definition. In this example we want an argument for a PNG image. Then we could change the instantiation as follows. As an additional parameter we set an array with the type `StringArgumentRule`. The first added rule checks if the image has the extension `.png | .PNG`. The second rule checks if the file exists and is really a file.
4646
```
4747
StringArgumentDefinition sad = new StringArgumentDefinition(
@@ -56,8 +56,8 @@ StringArgumentDefinition sad = new StringArgumentDefinition(
5656
);
5757
```
5858
For the `IntegerArgumentDefinition` use `IntegerArgumentRule` and for `DoubleArgumentDefinition` `DoubleArgumentRule`. There are no rules for `FlagArgumentDefinition` because it is a bit assignment (`true, false`) without variance.
59-
### Add Autogenerated Help Argument
6059
<a name="add_autogenerated_help_argument"></a>
60+
### Add Autogenerated Help Argument
6161
Now an automatically generated help argument can be created. As with the previous argument definitions, a prefix is defined for this. In addition, a title is defined for the output. The following example shows an instantiation of this help argument.
6262
```
6363
argumentParser.addHelpArgument(new String[] {"-h", "-help"}, "This arguments are available:");
@@ -67,20 +67,20 @@ The output would look like this once the user enters `-h` or `-help`:
6767
This arguments are available:
6868
{-s,-src,-source} Required: yes Image source
6969
```
70-
### Parse The Arguments
7170
<a name="parse_the_arguments"></a>
71+
### Parse The Arguments
7272
Now the arguments from the main `(String[] args)` can be passed to the parser.
7373
```
7474
argumentParser.parse(args);
7575
```
76-
### Get Argument Values
7776
<a name="get_argument_values "></a>
77+
### Get Argument Values
7878
To get the value after parsing, simply read the value from the respective definition object as follows.
7979
```
8080
System.out.println(iad.getValue());
8181
```
82-
### Put All Together
8382
<a name="put_all_together"></a>
83+
### Put All Together
8484
Now everything together and with an error handling.
8585
```
8686
public class Main {
@@ -113,8 +113,8 @@ public class Main {
113113
}
114114
}
115115
```
116-
## Further Examples
117116
<a name="further_examples"></a>
117+
## Further Examples
118118
In the following example, a temperature is written to the database. As argument we want the username, the password and the table name of the database. Additionally we need the IP and a port. We also offer the user the option to disable encrypted communication. Last but not least we want a valid temperature. We assume that we have tempreatures in the range of -40 to 120 degrees.
119119
```
120120
public class Main2 {
@@ -155,7 +155,7 @@ public class Main2 {
155155
156156
System.out.println("Connect to " + mysqlIp.getValue() + ":" + mysqlPort.getValue() + (encryptedCommunication.getValue()?" with a secure connection":" with an unsafe connection"));
157157
System.out.println("Use credentials Username:[" + mysqlUser.getValue() + "] Password:[" + mysqlPassword.getValue() + "]");
158-
System.out.println("Insert [" + temperature.getValue() + "]°C into table [" + mysqlTable.getValue() + "]");
158+
System.out.println("Insert [" + temperature.getValue() + "]°C into table [" + mysqlTable.getValue() + "]");
159159
160160
} catch (NonUniquePrefixException e) { // If that happens, you have a bug in the code
161161
e.printStackTrace();
@@ -181,7 +181,7 @@ If the arguments are given `-u root -p Tutorial19 -ip 127.0.0.1 -c -temp 22.3`.
181181
```
182182
Connect to 127.0.0.1:3306 with an unsafe connection
183183
Use credentials Username:[root] Password:[Tutorial19]
184-
Insert [22.3]°C into table [temperature_log]
184+
Insert [22.3]°C into table [temperature_log]
185185
```
186186
If an unallowed port is specified `-u root -p Tutorial19 -ip 127.0.0.1 -port 42 -c -temp 22.3`, then the output is:
187187
```
@@ -191,4 +191,4 @@ If the temperature argument is not given, the output is:
191191
```
192192
Missing argument:
193193
{-temp,-temperature} Temperature to log
194-
```
194+
```

0 commit comments

Comments
 (0)