forked from b3601993/CodeTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGnuGetOpt.java
121 lines (106 loc) · 4.45 KB
/
GnuGetOpt.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package bitpal;
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
import java.math.BigInteger;
/**
* 使用gnu.getopt 处理命令行参数,
*
* <dependency>
* <groupId>gnu.getopt</groupId>
* <artifactId>java-getopt</artifactId>
* <version>1.0.13</version>
* </dependency>
*
* Created by zhangjikai on 16-3-14.
*/
public class GnuGetOpt {
public static void printHelp() {
StringBuilder builder = new StringBuilder();
builder.append("Options:\n\n");
builder.append(String.format("%-30s", "-M,--match <arg>:"));
builder.append("Specify the match score which should be positive or zero. Default is 2.\n\n");
builder.append(String.format("%-30s", "-I,--mismatch <arg>: "));
builder.append("Specify the mismatch score which should be negative. Default is -3.\n\n");
builder.append(String.format("%-30s", "-G,--gap <arg>: "));
builder.append("Specify the gap score which should be negative. Default is -5.\n\n");
builder.append(String.format("%-30s", "-d,--directory <arg>: "));
builder.append("Specify the directory where to place generated source files.\n\n");
builder.append(String.format("%-30s", "-h,--help: "));
builder.append("Display help Information.\n\n");
builder.append(String.format("%-30s", "-n,--name <arg>: "));
builder.append("Set the name of generated source file.\n\n");
builder.append(String.format("%-30s", "-t,--type <arg>: "));
builder.append("Set the type of generated files. Valid values are: cpu, mic, avx2. Default is cpu.\n\n");
System.out.println(builder.toString());
}
public static void handleArgs(String[] args) {
LongOpt longOpts[] = new LongOpt[7];
longOpts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
longOpts[1] = new LongOpt("directory", LongOpt.REQUIRED_ARGUMENT, null, 'd');
longOpts[2] = new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null, 'n');
longOpts[3] = new LongOpt("match", LongOpt.REQUIRED_ARGUMENT, null, 'M');
longOpts[4] = new LongOpt("mismatch", LongOpt.REQUIRED_ARGUMENT, null, 'I');
longOpts[5] = new LongOpt("gap", LongOpt.REQUIRED_ARGUMENT, null, 'G');
longOpts[6] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null, 't');
int c;
int num;
String arg;
Getopt opt = new Getopt("bitpal", args, "M:I:G:d:hn:t", longOpts);
while ((c = opt.getopt()) != -1) {
switch (c) {
case 'M':
arg = opt.getOptarg();
try {
num = Integer.parseInt(arg);
} catch (Exception e) {
System.err.println("-M: You should input an integer.");
System.exit(-1);
}
break;
case 'I':
arg = opt.getOptarg();
try {
num = Integer.parseInt(arg);
} catch (Exception e) {
System.err.println("-I: You should input an integer.");
System.exit(-1);
}
break;
case 'G':
arg = opt.getOptarg();
try {
num = Integer.parseInt(arg);
} catch (Exception e) {
System.err.println("-G: You should input an integer.");
System.exit(-1);
}
break;
case 'd':
arg = opt.getOptarg();
break;
case 'h':
printHelp();
break;
case 'n':
arg = opt.getOptarg();
break;
case 't':
arg = opt.getOptarg();
if (!"cpumicavx2".contains(arg)) {
System.err.println("-t: Valid values are {cpu, mic, avx2}");
System.exit(-1);
}
break;
case '?':
System.err.println("The option '" + (char) opt.getOptopt() + "' is not valid");
System.exit(-1);
break;
default:
break;
}
}
}
public static void main(String[] args) {
handleArgs(args);
}
}