Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Command.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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. package uk.org.ownage.dmdirc.commandparser;
  23. /**
  24. * Represents a generic command
  25. * @author chris
  26. */
  27. public abstract class Command {
  28. /**
  29. * The name of this command (i.e., the string used by the user to execute it)
  30. */
  31. protected String name;
  32. /**
  33. * The arity of this command
  34. */
  35. protected int arity = 0;
  36. /**
  37. * Whether this command is polyadic or not
  38. */
  39. protected boolean polyadic;
  40. /**
  41. * Whether this command should be shown in help output
  42. */
  43. protected boolean show = true;
  44. /**
  45. * A textual description of this command's arguments
  46. */
  47. protected String arguments = "<unknown>";
  48. /**
  49. * A description of this command
  50. */
  51. protected String description = "unknown";
  52. /**
  53. * Creates a new instance of Command
  54. */
  55. public Command() {
  56. }
  57. /**
  58. * Returns the signature of this command. For polyadic commands, the signature
  59. * is simply the name. For other commands, the signature is a concatenation of
  60. * the name, a literal "/", and the arity.
  61. * @return The signature of this command
  62. */
  63. public String getSignature() {
  64. if (polyadic) {
  65. return name;
  66. } else {
  67. return name+"/"+arity;
  68. }
  69. }
  70. /**
  71. * Returns whether or not this command should be shown in help messages
  72. * @return True iff the command should be shown, false otherwise
  73. */
  74. public boolean showInHelp() {
  75. return show;
  76. }
  77. /**
  78. * Returns a string representing the help message for this command
  79. * @return the help message for this command
  80. */
  81. public String getHelp() {
  82. return name+" "+arguments+" - "+description;
  83. }
  84. /**
  85. * Implodes the given list of arguments
  86. * @param args The arguments to implode
  87. * @return A string containing each argument seperated by a space
  88. */
  89. protected String implodeArgs(String... args) {
  90. String res = "";
  91. for (int i = 0; i < args.length; i++) {
  92. if (res.equals("")) {
  93. res = args[i];
  94. } else {
  95. res = res.concat(" "+args[i]);
  96. }
  97. }
  98. return res;
  99. }
  100. /**
  101. * Returns this command's name
  102. * @return The name of this command
  103. */
  104. public String getName() {
  105. return name;
  106. }
  107. }