/src/lib/cli/internal/clarg_boolean.e

http://github.com/tybor/Liberty · Specman e · 188 lines · 144 code · 21 blank · 23 comment · 12 complexity · e5ed64108dc30380ba1aada4752ffb2b MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class CLARG_BOOLEAN
  5. inherit
  6. COMMAND_LINE_TYPED_ARGUMENT[BOOLEAN]
  7. redefine
  8. out_in_tagged_out_memory
  9. end
  10. insert
  11. ARGUMENTS
  12. redefine
  13. out_in_tagged_out_memory
  14. end
  15. CLARG_OPTIONS
  16. redefine
  17. out_in_tagged_out_memory
  18. end
  19. create {COMMAND_LINE_ARGUMENT_FACTORY}
  20. make
  21. feature {ANY}
  22. item: BOOLEAN
  23. usage: FIXED_STRING
  24. is_mandatory: BOOLEAN False
  25. can_be_mandatory: BOOLEAN False
  26. is_optional: BOOLEAN True
  27. can_be_optional: BOOLEAN True
  28. is_positional: BOOLEAN False
  29. is_repeatable: BOOLEAN False
  30. is_set: BOOLEAN
  31. do
  32. Result := item
  33. end
  34. force_index (a_index: INTEGER)
  35. do
  36. check False end
  37. end
  38. out_in_tagged_out_memory
  39. do
  40. if long /= Void then
  41. tagged_out_memory.append(once "--")
  42. tagged_out_memory.append(long)
  43. else
  44. tagged_out_memory.append(once "-")
  45. tagged_out_memory.append(short)
  46. end
  47. end
  48. feature {COMMAND_LINE_ARGUMENTS, COMMAND_LINE_ARGUMENT}
  49. prepare_parse
  50. do
  51. item := False
  52. end
  53. parse_command_line (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT
  54. local
  55. arg: STRING
  56. do
  57. Result := context
  58. if context.is_short then
  59. if is_short(argument(context.index), context.short_option_index) then
  60. item := True
  61. Result.set_index(context.index)
  62. Result.set_short(context.short_index, context.short_option_index + 1)
  63. elseif argument_count >= context.short_index and then is_long(argument(context.short_index)) then
  64. item := True
  65. Result.set_index(context.short_index + 1)
  66. end
  67. elseif argument_count >= context.index then
  68. arg := argument(context.index)
  69. if is_short(arg, 1) then
  70. item := True
  71. Result.set_index(context.index)
  72. Result.set_short(context.index + 1, context.short_option_index + 1)
  73. elseif is_long(arg) then
  74. item := True
  75. Result.set_index(context.index + 1)
  76. end
  77. end
  78. end
  79. is_set_at (context: COMMAND_LINE_CONTEXT): BOOLEAN
  80. do
  81. Result := item
  82. end
  83. undo_parse (context: COMMAND_LINE_CONTEXT)
  84. do
  85. item := False
  86. end
  87. usage_summary (stream: OUTPUT_STREAM)
  88. do
  89. if short /= Void then
  90. if long /= Void then
  91. stream.put_character('(')
  92. put_short(stream)
  93. stream.put_character('|')
  94. put_long(stream)
  95. stream.put_character(')')
  96. else
  97. put_short(stream)
  98. end
  99. elseif long /= Void then
  100. put_long(stream)
  101. end
  102. detailed := False
  103. end
  104. usage_details (stream: OUTPUT_STREAM)
  105. do
  106. if not detailed then
  107. usage_summary(stream)
  108. stream.put_character(':')
  109. stream.put_character(' ')
  110. if usage /= Void then
  111. stream.put_line(usage)
  112. else
  113. stream.put_line(once "Set the flag.")
  114. end
  115. detailed := True
  116. end
  117. end
  118. set_mandatory (parent_option: like Current; enable: BOOLEAN)
  119. do
  120. check False end
  121. end
  122. feature {}
  123. make (a_short, a_long, a_usage: ABSTRACT_STRING)
  124. require
  125. a_short /= Void implies a_short.count = 1
  126. a_short /= Void or else a_long /= Void
  127. do
  128. if a_short /= Void then
  129. short := a_short.intern
  130. end
  131. if a_long /= Void then
  132. long := a_long.intern
  133. end
  134. if a_usage /= Void then
  135. usage := a_usage.intern
  136. end
  137. ensure
  138. a_short /= Void implies short.is_equal(a_short)
  139. a_long /= Void implies long.is_equal(a_long)
  140. a_usage /= Void implies usage.is_equal(a_usage)
  141. end
  142. parent: like Current
  143. detailed: BOOLEAN
  144. invariant
  145. parent = Void
  146. is_optional
  147. end -- class CLARG_BOOLEAN
  148. --
  149. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  150. --
  151. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  152. -- of this software and associated documentation files (the "Software"), to deal
  153. -- in the Software without restriction, including without limitation the rights
  154. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  155. -- copies of the Software, and to permit persons to whom the Software is
  156. -- furnished to do so, subject to the following conditions:
  157. --
  158. -- The above copyright notice and this permission notice shall be included in
  159. -- all copies or substantial portions of the Software.
  160. --
  161. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  162. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  163. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  164. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  165. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  166. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  167. -- THE SOFTWARE.