/src/lib/cli/internal/clarg_and.e

http://github.com/tybor/Liberty · Specman e · 178 lines · 136 code · 19 blank · 23 comment · 9 complexity · d1027edda1a74238c6ea82bd5a31d5d5 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_AND
  5. inherit
  6. COMMAND_LINE_ARGUMENT
  7. redefine
  8. infix "and", infix "and then"
  9. end
  10. create {COMMAND_LINE_ARGUMENT}
  11. make
  12. feature {ANY}
  13. is_repeatable: BOOLEAN False
  14. infix "and", infix "and then" (other: COMMAND_LINE_ARGUMENT): COMMAND_LINE_ARGUMENT
  15. do
  16. args.add_last(other)
  17. mandatory_count_memory := -1
  18. Result := Current
  19. end
  20. is_set: BOOLEAN
  21. is_mandatory: BOOLEAN
  22. do
  23. Result := args.exists(agent {COMMAND_LINE_ARGUMENT}.is_mandatory)
  24. end
  25. is_set_at (context: COMMAND_LINE_CONTEXT): BOOLEAN
  26. do
  27. Result := args.exists(agent {COMMAND_LINE_ARGUMENT}.is_set_at(context))
  28. end
  29. feature {COMMAND_LINE_ARGUMENTS, COMMAND_LINE_ARGUMENT}
  30. prepare_parse
  31. do
  32. is_set := False
  33. set_count := 0
  34. args.for_each(agent {COMMAND_LINE_ARGUMENT}.prepare_parse)
  35. end
  36. parse_command_line (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT
  37. do
  38. Result := parse_cli(context)
  39. is_set := set_count = mandatory_count
  40. end
  41. undo_parse (context: COMMAND_LINE_CONTEXT)
  42. do
  43. args.for_each(agent {COMMAND_LINE_ARGUMENT}.undo_parse(context))
  44. end
  45. usage_summary (stream: OUTPUT_STREAM)
  46. local
  47. i: INTEGER
  48. do
  49. from
  50. i := args.lower
  51. until
  52. i > args.upper
  53. loop
  54. if i > args.lower then
  55. stream.put_character(' ')
  56. end
  57. args.item(i).usage_summary(stream)
  58. i := i + 1
  59. end
  60. detailed := False
  61. end
  62. usage_details (stream: OUTPUT_STREAM)
  63. do
  64. if not detailed then
  65. args.for_each(agent {COMMAND_LINE_ARGUMENT}.usage_details(stream))
  66. detailed := True
  67. end
  68. end
  69. feature {}
  70. parse_cli (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT
  71. local
  72. i: INTEGER; arg: COMMAND_LINE_ARGUMENT; ctx: COMMAND_LINE_CONTEXT
  73. do
  74. from
  75. Result := context
  76. i := args.lower
  77. until
  78. not Result.is_parsed or else i > args.upper
  79. loop
  80. arg := args.item(i)
  81. if not arg.is_set_at(Result) then
  82. ctx := arg.parse_command_line(Result)
  83. if ctx.is_parsed and then arg.is_set_at(Result) then
  84. ctx := parse_cli(ctx)
  85. check
  86. arg.is_set_at(Result)
  87. end
  88. if ctx.is_parsed then
  89. Result := ctx
  90. if arg.is_mandatory then
  91. set_count := set_count + 1
  92. end
  93. else
  94. arg.undo_parse(Result)
  95. end
  96. end
  97. end
  98. i := i + 1
  99. end
  100. end
  101. mandatory_count: INTEGER
  102. local
  103. i: INTEGER
  104. do
  105. Result := mandatory_count_memory
  106. if Result < 0 then
  107. from
  108. Result := 0
  109. i := args.lower
  110. invariant
  111. variant
  112. (args.upper - args.lower + 1) - i
  113. until
  114. i > args.upper
  115. loop
  116. if args.item(i).is_mandatory then
  117. Result := Result + 1
  118. end
  119. i := i + 1
  120. end
  121. mandatory_count_memory := Result
  122. end
  123. end
  124. feature {}
  125. args: FAST_ARRAY[COMMAND_LINE_ARGUMENT]
  126. set_count: INTEGER
  127. mandatory_count_memory: INTEGER
  128. make (a_left, a_right: COMMAND_LINE_ARGUMENT)
  129. require
  130. a_left /= Void
  131. a_right /= Void
  132. do
  133. args := {FAST_ARRAY[COMMAND_LINE_ARGUMENT] << a_left, a_right >> }
  134. mandatory_count_memory := -1
  135. end
  136. detailed: BOOLEAN
  137. invariant
  138. args.count >= 2
  139. end -- class CLARG_AND
  140. --
  141. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  142. --
  143. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  144. -- of this software and associated documentation files (the "Software"), to deal
  145. -- in the Software without restriction, including without limitation the rights
  146. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  147. -- copies of the Software, and to permit persons to whom the Software is
  148. -- furnished to do so, subject to the following conditions:
  149. --
  150. -- The above copyright notice and this permission notice shall be included in
  151. -- all copies or substantial portions of the Software.
  152. --
  153. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  154. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  155. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  156. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  157. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  158. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  159. -- THE SOFTWARE.