There are some executable tools written in Java that require callers to pass in a large number of parameters. At first, I used the most traditional way of directly passing an array of parameters, so the following is a spectacular code:
public static void Main (string[] args) {
String AAA = Args[0];
String bbb = args[1];
String CCC = args[2];
String ddd = args[3];
String eee = args[4];
String FFF = args[5];
String GGG = args[6];
String HHH = args[7];
Do something with these arguments
}
Called by:
Java mytool Hello world1 world2 world3 world4 world5 world6 world7
After the customer colleague took to use, felt very inconvenient, complained to me. It turns out that when he was writing a script call, he had to look at my Java source code repeatedly to see what each parameter was. After writing, the next time you need to change, but also forget to find out. and the parameter order can not be wrong, the number can not be less, in short, is very troublesome.
I also realized that this problem really exists, and asked the client colleague there is no good way, he gave me such an example:
public static void Main (string[] args) {
String AAA = System.getproperty ("AAA");
String bbb = System.getproperty ("BBB");
String CCC = System.getproperty ("CCC");
String DDD = System.getproperty ("ddd");
String eee = system.getproperty ("eee");
String FFF = system.getproperty ("fff");
String GGG = System.getproperty ("GGG");
String HHH = System.getproperty ("HHH");
Do something with these arguments
}
Called by:
Java-daaa=hello-dbbb=world1-dccc=world2-dddd=world3-deee=world4-dfff=world5-dggg=world6-dhhh=world7 MyTool
What the client's colleague means is that this practice "perfectly" solves the few inconveniences he mentions, such as:
-DThe following parameter names can be used to remind the meaning of the parameter
- You can write the order.
- If the parameter is optional, you can delete it directly.
Although my first feeling is "unexpectedly dare to use this way to pass parameters!" "But there was nothing to oppose in the moment, because he could do it in that way, and I couldn't do it that way."
Think for a moment, these two ways are quite complementary, I have you no, I have none of you have. In comparison, I think the latter problem is more, because it pollutes the system property environment and makes it easier to produce unforeseen problems, but it is also a thought-worthy solution.
Then I thought of a scheme that tried to combine the advantages of both:
public static void Main (string[] args) {
Map map = new HashMap ();
for (int i=0; i
Map.put (Args[i], args[i+1]);
}
String AAA = Map.get ("-aaa");
String bbb = Map.get ("-bbb");
String CCC = Map.get ("-CCC");
String DDD = Map.get ("-ddd");
String eee = map.get ("-eee");
String FFF = map.get ("-fff");
String GGG = Map.get ("-GGG");
String HHH = Map.get ("-hhh");
Do something with these arguments
}
Called by:
Java mytool-aaa hello-bbb world1-ccc world2-ddd world3-eee world4-fff world5-ggg world6-hhh world7
It seems clear some, though a little longer. Now, it can also:
- Parameter list is used as hint parameter
- You can write the order.
- If there is an optional parameter, delete the corresponding item directly
And does not pollute the system property environment, compared -D to the previous scenario, a little better.
Is that the only way to do it? Looking at the three scenarios above, there are actually some problems that need to be written in more code:
- Print usage
- Check the parameters, if there is a missing or unreasonable, to prompt the error
These missing jobs are actually quite cumbersome, making our code more confusing and indispensable.
Is there a way of elegance? The answer is yes, we can use Java's annotation to describe our parameters, use this information to validate parameters, value and print the use of help.
There are many such libraries on the market, such as args4j, Jcommands and so on, I finally chose the args4j.
First we need to write a class that uses annotation to describe what parameters our program requires:
public class Args {
@Option (Required=true, name= "-aaa", usage= "AAA is something")
Private String AAA;
@Option (name= "-bbb", usage= "BBB is something")
Private String BBB;
@Option (name= "-CCC", usage= "CCC is something")
Private String CCC;
@Option (name= "-ddd", usage= "ddd is something")
Private String DDD;
@Option (name= "-eee", usage= "Eee is something")
Private String eee;
@Option (name= "-fff", usage= "FFF is something")
Private String FFF;
@Option (name= "-GGG", usage= "GGG is something")
Private String GGG;
Getters for them
}
This is a clear way to know the function, description, etc. of each parameter.
Then take the value:
public static void Main (string[] args) {
Args Myargs = new Args ();
Cmdlineparser parser = new Cmdlineparser (Myargs);
Parser.parseargument (Myargs);
}
At this point, the fields myArgs in the instance are automatically assigned the corresponding values, which are assigned by the Cmdlineparser through reflection.
Want to print using hints?
Parser.printusage (System.out);
It will be based on Args the definition, automatically organized into a better readable format, the output of the use of hints, very intimate.
Called by:
Java mytool-aaa hello-bbb world1-ccc world2-ddd world3-eee world4-fff world5-ggg world6-hhh world7
Although it seems that the invocation is not the same as before, but the connotation is richer, and we have to do is to define a description class, and call 32 lines of code just.
This is the most elegant solution I am currently thinking of.
A large number of parameters are passed to main () Java