/src/lib/cli/command_line_argument_factory.e

http://github.com/tybor/Liberty · Specman e · 179 lines · 132 code · 19 blank · 28 comment · 8 complexity · 98abda22f7c69492582c6b2044032515 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class COMMAND_LINE_ARGUMENT_FACTORY
  5. --
  6. -- The command-line arguments factory.
  7. --
  8. feature {ANY} -- Options
  9. option_string (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[FIXED_STRING]
  10. require
  11. short /= Void implies short_pattern.match(short.out)
  12. long /= Void implies long_pattern.match(long.out)
  13. short /= Void or else long /= Void
  14. name /= Void
  15. do
  16. create {CLARG_STRING} Result.optional(short, long, name, usage)
  17. end
  18. option_strings (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[TRAVERSABLE[FIXED_STRING]]
  19. require
  20. short /= Void implies short_pattern.match(short.out)
  21. long /= Void implies long_pattern.match(long.out)
  22. short /= Void or else long /= Void
  23. name /= Void
  24. do
  25. create {CLARG_STRINGS} Result.optional(short, long, name, usage)
  26. end
  27. option_file (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[REGULAR_FILE]
  28. require
  29. short /= Void implies short_pattern.match(short.out)
  30. long /= Void implies long_pattern.match(long.out)
  31. short /= Void or else long /= Void
  32. name /= Void
  33. do
  34. create {CLARG_FILE} Result.optional(short, long, name, usage)
  35. end
  36. option_directory (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[DIRECTORY]
  37. require
  38. short /= Void implies short_pattern.match(short.out)
  39. long /= Void implies long_pattern.match(long.out)
  40. short /= Void or else long /= Void
  41. name /= Void
  42. do
  43. create {CLARG_DIRECTORY} Result.optional(short, long, name, usage)
  44. end
  45. option_integer (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[INTEGER]
  46. require
  47. short /= Void implies short_pattern.match(short.out)
  48. long /= Void implies long_pattern.match(long.out)
  49. short /= Void or else long /= Void
  50. name /= Void
  51. do
  52. create {CLARG_INTEGER} Result.optional(short, long, name, usage)
  53. end
  54. option_integers (short, long, name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[TRAVERSABLE[INTEGER]]
  55. require
  56. short /= Void implies short_pattern.match(short.out)
  57. long /= Void implies long_pattern.match(long.out)
  58. short /= Void or else long /= Void
  59. name /= Void
  60. do
  61. create {CLARG_INTEGERS} Result.optional(short, long, name, usage)
  62. end
  63. option_boolean (short, long, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[BOOLEAN]
  64. require
  65. short /= Void implies short_pattern.match(short.out)
  66. long /= Void implies long_pattern.match(long.out)
  67. short /= Void or else long /= Void
  68. do
  69. create {CLARG_BOOLEAN} Result.make(short, long, usage)
  70. end
  71. option_counter (short, long, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[INTEGER]
  72. require
  73. short /= Void implies short_pattern.match(short.out)
  74. long /= Void implies long_pattern.match(long.out)
  75. short /= Void or else long /= Void
  76. do
  77. create {CLARG_COUNTER} Result.make(short, long, usage)
  78. end
  79. feature {ANY} -- Positional
  80. positional_string (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[FIXED_STRING]
  81. require
  82. name /= Void
  83. do
  84. create {CLARG_STRING} Result.positional(name, usage)
  85. end
  86. positional_strings (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[TRAVERSABLE[FIXED_STRING]]
  87. require
  88. name /= Void
  89. do
  90. create {CLARG_STRINGS} Result.positional(name, usage)
  91. end
  92. positional_file (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[REGULAR_FILE]
  93. require
  94. name /= Void
  95. do
  96. create {CLARG_FILE} Result.positional(name, usage)
  97. end
  98. positional_directory (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[DIRECTORY]
  99. require
  100. name /= Void
  101. do
  102. create {CLARG_DIRECTORY} Result.positional(name, usage)
  103. end
  104. positional_integer (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[INTEGER]
  105. require
  106. name /= Void
  107. do
  108. create {CLARG_INTEGER} Result.positional(name, usage)
  109. end
  110. positional_integers (name, usage: ABSTRACT_STRING): COMMAND_LINE_TYPED_ARGUMENT[TRAVERSABLE[INTEGER]]
  111. require
  112. name /= Void
  113. do
  114. create {CLARG_INTEGERS} Result.positional(name, usage)
  115. end
  116. feature {ANY}
  117. no_parameters: COMMAND_LINE_ARGUMENT
  118. -- useful to allow an empty command line.
  119. once
  120. create {CLARG_NOP} Result.make
  121. end
  122. remaining_parameters: COMMAND_LINE_TYPED_ARGUMENT[TRAVERSABLE[FIXED_STRING]]
  123. -- allows parameters to be set after a special "--" option
  124. once
  125. create {CLARG_REMAINING} Result.make
  126. end
  127. feature {ANY} -- Option names validity
  128. short_pattern: REGULAR_EXPRESSION
  129. local
  130. re: REGULAR_EXPRESSION_BUILDER
  131. once
  132. Result := re.convert_posix_pattern("^[A-Za-z0-9]$")
  133. end
  134. long_pattern: REGULAR_EXPRESSION
  135. local
  136. re: REGULAR_EXPRESSION_BUILDER
  137. once
  138. Result := re.convert_posix_pattern("^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$")
  139. end
  140. end -- class COMMAND_LINE_ARGUMENT_FACTORY
  141. --
  142. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  143. --
  144. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  145. -- of this software and associated documentation files (the "Software"), to deal
  146. -- in the Software without restriction, including without limitation the rights
  147. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  148. -- copies of the Software, and to permit persons to whom the Software is
  149. -- furnished to do so, subject to the following conditions:
  150. --
  151. -- The above copyright notice and this permission notice shall be included in
  152. -- all copies or substantial portions of the Software.
  153. --
  154. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  155. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  156. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  157. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  158. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  159. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  160. -- THE SOFTWARE.