1 20 21 22 23 package cpmake; 24 25 import java.util.*; 26 27 class CommandLine 28 { 29 HashMap m_optionMap; 30 String [] m_optionList; 31 TreeSet m_boolOptionSet; 32 TreeSet m_valueOptionSet; 33 TreeSet m_invalidOptionSet; 34 Vector m_nonOptions; 35 boolean m_commandLineProcessed; 36 37 private void processCommandLine() 38 { 39 Character option; 40 if (m_commandLineProcessed) 41 return; 42 43 for (int I = 0;I < m_optionList.length;I++) 44 { 45 if (m_optionList[I].charAt(0) == '-') 46 { 47 option = new Character (m_optionList[I].charAt(1)); 48 if (m_boolOptionSet.contains(option)) 49 m_optionMap.put(option, ""); 50 else if (m_valueOptionSet.contains(option)) 51 { 52 m_optionMap.put(option, m_optionList[++I]); 53 } 54 else 55 m_invalidOptionSet.add(option); 56 } 57 else 58 m_nonOptions.add(m_optionList[I]); 59 } 60 61 m_commandLineProcessed = true; 62 } 63 64 public CommandLine(String [] args) 65 { 66 m_optionList = args; 67 m_commandLineProcessed = false; 68 m_optionMap = new HashMap(); 69 m_boolOptionSet = new TreeSet(); 70 m_valueOptionSet = new TreeSet(); 71 m_invalidOptionSet = new TreeSet(); 72 m_nonOptions = new Vector(); 73 } 74 75 public void setBooleanOptions(String options) 76 { 77 char[] optArr = options.toCharArray(); 78 79 for(int I = 0;I < optArr.length;I++) 80 m_boolOptionSet.add(new Character (optArr[I])); 81 82 m_commandLineProcessed = false; 83 } 84 85 public void setValueOptions(String options) 86 { 87 char[] optArr = options.toCharArray(); 88 89 for(int I = 0;I < optArr.length;I++) 90 m_valueOptionSet.add(new Character (optArr[I])); 91 92 m_commandLineProcessed = false; 93 } 94 95 public String [] getNonOptions() 96 { 97 processCommandLine(); 98 return ((String [])m_nonOptions.toArray(new String [0])); 99 } 100 101 public void validateCommandLine() 102 { 104 105 } 106 107 public boolean isSet(char option) 108 { 109 processCommandLine(); 110 return (m_optionMap.containsKey(new Character (option))); 111 } 112 113 public String getOptionValue(char option) 114 { 115 processCommandLine(); 116 return ((String )m_optionMap.get(new Character (option))); 117 } 118 } | Popular Tags |