-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.java
51 lines (47 loc) · 1.64 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package examples.dustin.commandline.argparse4j;
import static java.lang.System.out;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
/**
* Demonstrate command-line processing in Java with argparse4j.
*/
public class Main
{
/**
* Main function demonstrating command-line processing via argparse4j.
*
* @param arguments Command-line arguments.
*/
public static void main(final String[] arguments)
{
final ArgumentParser argumentParser =
ArgumentParsers.newArgumentParser("Main", true);
argumentParser.addArgument("-f", "--file")
.dest("file")
.required(true)
.help("Path and name of file");
argumentParser.addArgument("-v", "--verbose")
.dest("verbose")
.type(Boolean.class)
.nargs("?")
.setConst(true)
.help("Enable verbose output.");
try
{
final Namespace response = argumentParser.parseArgs(arguments);
final String filePathAndName = response.getString("file");
final Boolean verbositySet = response.getBoolean("verbose");
out.println(
"Path/name of file is '" + filePathAndName
+ "' and verbosity is "
+ (Boolean.TRUE.equals(verbositySet) ? "SET" : "NOT SET")
+ ".");
}
catch (ArgumentParserException parserEx)
{
argumentParser.handleError(parserEx);
}
}
}