Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CLIParser.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2008 Shane Mc Cormack
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * SVN: $Id$
  23. */
  24. package com.dmdirc.installer.cliparser;
  25. import java.util.ArrayList;
  26. import java.util.Hashtable;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * Command Line argument parser.
  31. */
  32. public class CLIParser {
  33. /** Singleton instance of CLIParser. */
  34. private static CLIParser me;
  35. /** Singleton instance of CLIParser. */
  36. CLIParam helpParam = null;
  37. /**
  38. * Known arguments.
  39. * This hashtable stores the arguments with their flags as the key.
  40. */
  41. private final Map<String, CLIParam> params = new Hashtable<String, CLIParam>();
  42. /**
  43. * Known arguments.
  44. * This ArrayList stores every param type. (used for help)
  45. */
  46. private final List<CLIParam> paramList = new ArrayList<CLIParam>();
  47. /**
  48. * Redundant Strings.
  49. * This ArrayList stores redundant strings found whilst parsing the params.
  50. */
  51. private final List<String> redundant = new ArrayList<String>();
  52. /**
  53. * Get a reference to the CLIParser.
  54. */
  55. public static synchronized CLIParser getCLIParser() {
  56. if (me == null) { me = new CLIParser(); }
  57. return me;
  58. }
  59. /** Private constructor for CLIParser to prevent non-singleton instance. */
  60. private CLIParser() { }
  61. /** Clear known params from the hashtable. */
  62. public void clear() {
  63. params.clear();
  64. paramList.clear();
  65. redundant.clear();
  66. }
  67. /**
  68. * Add a CLIParam to the cliparser.
  69. *
  70. * @param param CLIParam sub-class to use as a parameter.
  71. * @return true if added, false if already exists.
  72. */
  73. public boolean add(final CLIParam param) {
  74. boolean validChar = (param.getChr() == 0 || !params.containsKey(""+param.getChr()));
  75. boolean validString = (param.getString().isEmpty() || !params.containsKey("-"+param.getString()));
  76. if (validChar && validString) {
  77. if (param.getChr() != 0) {
  78. params.put(""+param.getChr(), param);
  79. }
  80. if (!param.getString().isEmpty()) {
  81. params.put("-"+param.getString(), param);
  82. }
  83. paramList.add(param);
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. /**
  90. * Get the number of times a param was given.
  91. * In the case of params with both a char and string value, this number is
  92. * the total for both.
  93. *
  94. * @param flag Flag to get count for
  95. * @return number, or -1 if the param is invalud
  96. */
  97. public int getParamNumber(final String flag) {
  98. if (params.containsKey(flag)) {
  99. return params.get(flag).getNumber();
  100. } else {
  101. return -1;
  102. }
  103. }
  104. /**
  105. * Get a CLIParam object for a given flag.
  106. *
  107. * @param flag Flag to get param for
  108. * @return CLIParam object, or null if there is none.
  109. */
  110. public CLIParam getParam(final String flag) {
  111. if (params.containsKey(flag)) {
  112. return params.get(flag);
  113. } else {
  114. return null;
  115. }
  116. }
  117. /**
  118. * Get the list of params.
  119. *
  120. * @return list of params.
  121. */
  122. public List<CLIParam> getParamList() {
  123. return paramList;
  124. }
  125. /**
  126. * Get the list of redundant strings.
  127. *
  128. * @return list of redundant strings.
  129. */
  130. public List<String> getRedundant() {
  131. final List<String> result = new ArrayList<String>();
  132. for (String item : redundant) {
  133. result.add(item);
  134. }
  135. return result;
  136. }
  137. /**
  138. * Set the "help" command.
  139. *
  140. * @param param Param to look for in wantsHelp.
  141. */
  142. public void setHelp(final CLIParam param) {
  143. helpParam = param;
  144. }
  145. /**
  146. * Check if the help parameter has been passed to the CLI.
  147. */
  148. public boolean wantsHelp(final String[] args) {
  149. if (helpParam == null) { return false; }
  150. for (String arg : args) {
  151. if (arg.length() > 1 && arg.charAt(0) == '-') {
  152. final String name = arg.substring(1);
  153. if (name.equals("-")) {
  154. return false;
  155. } else {
  156. final CLIParam param = getParam(name);
  157. if (param == helpParam) {
  158. return true;
  159. }
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Show the help
  167. */
  168. public void showHelp(final String title, final String usage) {
  169. System.out.println(title);
  170. System.out.println("------------------");
  171. System.out.println(usage);
  172. System.out.println(" ");
  173. for (CLIParam param : this.getParamList()) {
  174. if (param.getChr() != 0) {
  175. System.out.print("-"+param.getChr()+" ");
  176. } else {
  177. System.out.print(" ");
  178. }
  179. if (!param.getString().isEmpty()) {
  180. System.out.print("--"+param.getString()+" ");
  181. } else {
  182. System.out.print("\t\t");
  183. }
  184. System.out.println("\t"+param.getDescription());
  185. }
  186. }
  187. /**
  188. * Given a string array of arguments, parse as CLI Params.
  189. *
  190. * @param args Arguments to pass
  191. * @param strict if True, will terminate if a given param is invalid.
  192. */
  193. public void parseArgs(final String[] args, final boolean strict) {
  194. CLIParam lastParam = null;
  195. boolean allRedundant = false;
  196. for (String arg : args) {
  197. if (arg.length() > 1 && arg.charAt(0) == '-' && !allRedundant) {
  198. if (lastParam != null) { lastParam.setValue(""); }
  199. final String name = arg.substring(1);
  200. if (name.equals("-")) {
  201. allRedundant = true;
  202. } else {
  203. lastParam = getParam(name);
  204. if (lastParam == null) {
  205. System.out.println("Unknown Param: -"+name);
  206. if (helpParam != null) {
  207. String command = "";
  208. if (!helpParam.getString().isEmpty()) {
  209. command = helpParam.getString();
  210. } else if (helpParam.getChr() != 0) {
  211. command = ""+helpParam.getChr();
  212. }
  213. if (!command.isEmpty()) {
  214. System.out.println("Use "+command+" to get help.");
  215. }
  216. }
  217. if (strict) {
  218. System.exit(1);
  219. }
  220. } else {
  221. lastParam.incNumber();
  222. }
  223. }
  224. } else {
  225. if (arg.charAt(0) == '\\' && arg.length() > 1) { arg = arg.substring(1); }
  226. if (lastParam == null || allRedundant || !lastParam.setValue(arg)) {
  227. redundant.add(arg);
  228. }
  229. }
  230. }
  231. }
  232. }