You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 16, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,8 @@ I have needed an argument parser for some java projects. There are some librarie
11
11
-[Get Argument Values ](#get_argument_values)
12
12
-[Put All Together](#put_all_together)
13
13
-[Further Examples](#further_examples)
14
-
## Scope Of Functions
15
14
<aname="scope_of_functions"></a>
15
+
## Scope Of Functions
16
16
- Define arguments needed for your console applications
17
17
- Integer Arguments
18
18
- Double Arguments
@@ -21,17 +21,17 @@ I have needed an argument parser for some java projects. There are some librarie
21
21
- Define whether an argument is required
22
22
- Define a default value
23
23
- Define different rules for this argument
24
-
## How To Use It
25
24
<aname="how_to_use_it"></a>
25
+
## How To Use It
26
26
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
28
27
<aname="create_the_main_object"></a>
28
+
### Create The Main Object
29
29
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.
30
30
```
31
31
ArgumentParser argumentParser = new ArgumentParser(bool1, bool2, bool3);
32
32
```
33
-
### Definition Simple Arguments
34
33
<aname="definition_simple_arguments"></a>
34
+
### Definition Simple Arguments
35
35
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.
36
36
```
37
37
StringArgumentDefinition sad = new StringArgumentDefinition(prefixes, argumentDescription, isRequired, defaultValue)
@@ -40,8 +40,8 @@ Then the argument definition can be added to the argument parser.
40
40
```
41
41
argumentParser.add(sad);
42
42
```
43
-
### Definition Arguments With Rules
44
43
<aname="definition_arguments_with_rules"></a>
44
+
### Definition Arguments With Rules
45
45
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.
46
46
```
47
47
StringArgumentDefinition sad = new StringArgumentDefinition(
@@ -56,8 +56,8 @@ StringArgumentDefinition sad = new StringArgumentDefinition(
56
56
);
57
57
```
58
58
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
60
59
<aname="add_autogenerated_help_argument"></a>
60
+
### Add Autogenerated Help Argument
61
61
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.
62
62
```
63
63
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`:
67
67
This arguments are available:
68
68
{-s,-src,-source} Required: yes Image source
69
69
```
70
-
### Parse The Arguments
71
70
<aname="parse_the_arguments"></a>
71
+
### Parse The Arguments
72
72
Now the arguments from the main `(String[] args)` can be passed to the parser.
73
73
```
74
74
argumentParser.parse(args);
75
75
```
76
-
### Get Argument Values
77
76
<aname="get_argument_values "></a>
77
+
### Get Argument Values
78
78
To get the value after parsing, simply read the value from the respective definition object as follows.
79
79
```
80
80
System.out.println(iad.getValue());
81
81
```
82
-
### Put All Together
83
82
<aname="put_all_together"></a>
83
+
### Put All Together
84
84
Now everything together and with an error handling.
85
85
```
86
86
public class Main {
@@ -113,8 +113,8 @@ public class Main {
113
113
}
114
114
}
115
115
```
116
-
## Further Examples
117
116
<aname="further_examples"></a>
117
+
## Further Examples
118
118
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.
119
119
```
120
120
public class Main2 {
@@ -155,7 +155,7 @@ public class Main2 {
155
155
156
156
System.out.println("Connect to " + mysqlIp.getValue() + ":" + mysqlPort.getValue() + (encryptedCommunication.getValue()?" with a secure connection":" with an unsafe connection"));
0 commit comments