1 19 20 33 package org.htmlparser.util; 34 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 61 62 public class CommandLine 63 { 64 public static boolean VERBOSE = false; 65 66 protected List commands = new ArrayList (); 67 68 protected List flags = new ArrayList (); 69 protected List names = new ArrayList (); 70 protected Map values = new HashMap (); 71 72 public CommandLine(String chars, String [] args) 73 { 74 for (int i = 0; i < chars.length(); i++) 75 { 76 addCommand(chars.charAt(i)); 77 } 78 parse(args); 79 } 80 81 public CommandLine(String [] args) 82 { 83 parse(args); 84 } 85 86 protected void parse(String [] args) 87 { 88 for (int i = 0; i < args.length; i++) 89 { 90 String thisArg = args[i]; 91 String nextArg = null; 92 if (i < args.length - 1) 93 { 94 nextArg = args[i + 1]; 95 } 96 97 if (thisArg.startsWith("-")) 98 { 99 if (thisArg.length() > 2) 100 { 101 Character chr = new Character (thisArg.charAt(1)); 102 if (commands.contains(chr)) 103 { 104 String key = chr.toString(); 105 String val = thisArg.substring(2); 106 if (VERBOSE) 107 { 108 System.out.println("Value " + key + "=" + val); 109 } 110 values.put(key, val); 111 } 112 } 113 if (nextArg != null && !nextArg.startsWith("-")) 114 { 115 String key = thisArg.substring(1); 116 String val = nextArg; 117 if (VERBOSE) 118 { 119 System.out.println("Value " + key + "=" + val); 120 } 121 values.put(key, val); 122 i++; 123 } 124 else 125 { 126 String flag = thisArg.substring(1); 127 flags.add(flag); 128 if (VERBOSE) 129 { 130 System.out.println("Flag " + flag); 131 } 132 } 133 } 134 else 135 { 136 if (VERBOSE) 137 { 138 System.out.println("Name " + thisArg); 139 } 140 names.add(thisArg); 141 } 142 } 143 } 144 145 public void addCommand(char command) 146 { 147 commands.add(new Character (command)); 148 } 149 150 public boolean hasValue(String key) 151 { 152 return values.containsKey(key); 153 } 154 155 public String getValue(String key) 156 { 157 return (String ) values.get(key); 158 } 159 160 public boolean getFlag(String key) 161 { 162 return flags.contains(key); 163 } 164 165 public int getNameCount() 166 { 167 return names.size(); 168 } 169 170 public String getName(int index) 171 { 172 return (String ) names.get(index); 173 } 174 175 public static void main(String [] args) 176 { 177 CommandLine cmd = new CommandLine("f", args); 178 } 179 } 180 | Popular Tags |