-
Notifications
You must be signed in to change notification settings - Fork 188
/
SampleMain.java
105 lines (80 loc) · 3.41 KB
/
SampleMain.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import static org.kohsuke.args4j.ExampleMode.ALL;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Sample program that shows how you can use args4j.
*
* @author
* Kohsuke Kawaguchi ([email protected])
*/
public class SampleMain {
@Option(name="-r",usage="recursively run something")
private boolean recursive;
@Option(name="-o",usage="output to this file",metaVar="OUTPUT")
private File out = new File(".");
@Option(name="-str") // no usage
private String str = "(default value)";
@Option(name="-hidden-str2",hidden=true,usage="hidden option")
private String hiddenStr2 = "(default value)";
@Option(name="-n",usage="repeat <n> times\nusage can have new lines in it and also it can be verrrrrrrrrrrrrrrrrry long")
private int num = -1;
// using 'handler=...' allows you to specify a custom OptionHandler
// implementation class. This allows you to bind a standard Java type
// with a non-standard option syntax
@Option(name="-custom",handler=BooleanOptionHandler.class,usage="boolean value for checking the custom handler")
private boolean data;
// receives other command line parameters than options
@Argument
private List<String> arguments = new ArrayList<String>();
public static void main(String[] args) throws IOException {
new SampleMain().doMain(args);
}
public void doMain(String[] args) throws IOException {
CmdLineParser parser = new CmdLineParser(this);
// if you have a wider console, you could increase the value;
// here 80 is also the default
parser.setUsageWidth(80);
try {
// parse the arguments.
parser.parseArgument(args);
// you can parse additional arguments if you want.
// parser.parseArgument("more","args");
// after parsing arguments, you should check
// if enough arguments are given.
if( arguments.isEmpty() )
throw new CmdLineException(parser,"No argument is given");
} catch( CmdLineException e ) {
// if there's a problem in the command line,
// you'll get this exception. this will report
// an error message.
System.err.println(e.getMessage());
System.err.println("java SampleMain [options...] arguments...");
// print the list of available options
parser.printUsage(System.err);
System.err.println();
// print option sample. This is useful some time
System.err.println(" Example: java SampleMain"+parser.printExample(ALL));
return;
}
// this will redirect the output to the specified output
System.out.println(out);
if( recursive )
System.out.println("-r flag is set");
if( data )
System.out.println("-custom flag is set");
System.out.println("-str was "+str);
if( num>=0 )
System.out.println("-n was "+num);
// access non-option arguments
System.out.println("other arguments are:");
for( String s : arguments )
System.out.println(s);
}
}