View Javadoc

1   package net.sf.jour.util;
2   
3   import java.util.List;
4   import java.util.Properties;
5   import java.util.Vector;
6   
7   public abstract class CmdArgs {
8   
9   	/** 
10  	 * This is usage examples:
11  	 * @param args
12  	 */
13  	public static void main(String[] args) {
14  		Properties argsp = CmdArgs.load(args);
15  		if (argsp.getProperty("help") != null) {
16  			//help();
17  			System.exit(0);
18  		}
19  	}
20  
21  	public static int isArgsName(String name) {
22  		if (name.startsWith("--")) {
23  			name = name.substring(2);
24  			return 2;
25  		} else if (name.startsWith("-")) {
26  			name = name.substring(1);
27  			return 1;
28  		}
29  		return 0;
30  	}
31  
32  	public static Properties load(String[] args) {
33  		Properties properties = new Properties();
34  		for (int i = 0; i < args.length; i++) {
35  			String name = args[i];
36  			String value = "true";
37  			int prefix;
38  			if ((prefix = isArgsName(name)) > 0) {
39  				name = name.substring(prefix);
40  				if ((i + 1) < args.length) {
41  					if (isArgsName(args[i + 1]) == 0) {
42  						value = args[i + 1];
43  						i++;
44  					}
45  				}
46  				Object has = properties.get(name);
47  				if (has == null) {
48  					properties.put(name, value);
49  				} else if (has instanceof List) {
50  					((List) has).add(value);
51  				} else {
52  					List valueList = new Vector();
53  					valueList.add((String) has);
54  					valueList.add(value);
55  					properties.put(name, valueList);
56  				}
57  			}
58  		}
59  		return properties;
60  	}
61  
62  }