Repository navigation
Expand file tree
/
Copy pathgetArgs.java
More file actions
executable file
·103 lines (102 loc) · 2.64 KB
/
Copy pathgetArgs.java
File metadata and controls
executable file
·103 lines (102 loc) · 2.64 KB
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
package inversion;
import java.util.Hashtable;
public class getArgs {
/*
public static void main(String[] args) throws IOException, InterruptedException{
parse(args);
System.out.println(getString("input",false,""));
System.out.println(getInt("int",false,100));
System.out.println(getBoolean("boolean",false,false));
System.out.println(getDouble("double",false,1.0));
}
*/
public static Hashtable<String, String> StringHash= new Hashtable<String, String>();
public String cmd="";
public boolean help=false;
public getArgs(String[] args){
//parse args[i] to args[i+1] end at legnth-1
for (int i=0;i<args.length-1;i++){
if(args[i].length()>2 && args[i].substring(0,2).equals("--")){
StringHash.put(args[i].substring(2), args[i+1]);
}
}
for (int i=0;i<args.length;i++){
if(args[i].equals("--help")){
help=true;
}
}
}
public void addUsage(String add){
cmd=cmd.concat(add);
}
//input key,required parameter or not 1=required ,0=notRequired, default value
public String getString(String key,boolean required,String a){
if (StringHash.containsKey(key)){
return StringHash.get(key);
}else{
if (required){
System.out.println("No Key Name: "+key+"\n"+cmd);
System.exit(1);
return "";
}else{
return a;
}
}
}
public boolean getBoolean(String key,boolean required,boolean a){
if (StringHash.containsKey(key)){
String tmp=StringHash.get(key);
if (tmp.equalsIgnoreCase("true") || tmp.equalsIgnoreCase("t")){
return true;
}else if(tmp.equalsIgnoreCase("false")||tmp.equalsIgnoreCase("f")){
return false;
}else{
System.out.println("Can't recognise boolean type for key: "+key+" . Please try True/T for true, False/F for false\n"+cmd);
System.exit(1);
return true;//useless
}
}else{
if (required){
System.out.println("No Key Name: "+key+"\n"+cmd);
System.exit(1);
return true;
}else{
return a;
}
}
}
public int getInt(String key,boolean required,int a){
if(StringHash.containsKey(key)){
return Integer.parseInt( StringHash.get(key));
}else{
if (required){
System.out.println("No Key Name: "+key+"\n"+cmd);
System.exit(1);
return 1;
}else{
return a;
}
}
}
public double getDouble(String key,boolean required,double a){
if(StringHash.containsKey(key)){
return Double.parseDouble( StringHash.get(key));
}else{
if (required){
System.out.println("No Key Name: "+key+"\n"+cmd);
System.exit(1);
return 1.0;
}else{
return a;
}
}
}
public Boolean getString(String key) {
// TODO Auto-generated method stub
if (StringHash.containsKey(key)){
return true;
}else{
return false;
}
}
}