/src/lib/cli/command_line_typed_argument.e

http://github.com/tybor/Liberty · Specman e · 158 lines · 100 code · 17 blank · 41 comment · 4 complexity · 640a19fa6512a69a3ca88eb74aaa5f13 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class COMMAND_LINE_TYPED_ARGUMENT[E_]
  5. --
  6. -- A "single" argument which bears a value aka `item'.
  7. --
  8. inherit
  9. COMMAND_LINE_ARGUMENT
  10. feature {ANY}
  11. item: E_
  12. -- The argument value, if `is_set'
  13. deferred
  14. end
  15. as_mandatory, prefix "+": like Current
  16. -- Make the option mandatory (default for positionals)
  17. require
  18. can_be_mandatory
  19. do
  20. if is_mandatory then
  21. Result := Current
  22. else
  23. Result := twin
  24. Result.set_mandatory(Current, True)
  25. end
  26. ensure
  27. Result.is_mandatory
  28. end
  29. as_optional, prefix "-": like Current
  30. -- Make the option optional (default for options)
  31. require
  32. can_be_optional
  33. do
  34. if is_optional then
  35. Result := Current
  36. else
  37. Result := twin
  38. Result.set_mandatory(Current, False)
  39. end
  40. ensure
  41. not Result.is_mandatory
  42. end
  43. feature {ANY}
  44. is_mandatory: BOOLEAN
  45. -- True if the argument must be set at least once.
  46. deferred
  47. ensure
  48. Result implies can_be_mandatory
  49. end
  50. is_optional: BOOLEAN
  51. -- True if the argument may not be set.
  52. deferred
  53. ensure
  54. Result implies not is_positional
  55. is_mandatory implies not Result
  56. Result implies can_be_optional
  57. end
  58. is_positional: BOOLEAN
  59. -- True if the argument is not introduced by a flag. Such an argument is mandatory and may have an
  60. -- explicit position on the command line (see `force_index').
  61. deferred
  62. ensure
  63. Result implies not is_optional
  64. Result implies is_mandatory
  65. end
  66. can_be_mandatory: BOOLEAN
  67. -- True if the argument can be `set_mandatory'(True).
  68. deferred
  69. end
  70. can_be_optional: BOOLEAN
  71. -- True if the argument can be `set_mandatory'(False).
  72. deferred
  73. end
  74. feature {ANY}
  75. short: FIXED_STRING
  76. -- The short (one-letter) option flag introducing the argument, Void for positionals
  77. deferred
  78. ensure
  79. is_positional implies Result = Void
  80. Result /= Void implies Result.count = 1
  81. end
  82. long: FIXED_STRING
  83. -- The long option flag introducing the argument, Void for positionals
  84. deferred
  85. ensure
  86. is_positional implies Result = Void
  87. Result /= Void implies not Result.is_empty
  88. end
  89. usage: FIXED_STRING
  90. -- The option usage
  91. deferred
  92. end
  93. force_index (a_index: INTEGER)
  94. -- Force a positional parameter to be valid only at the given index
  95. require
  96. a_index > 0
  97. is_positional
  98. not is_repeatable
  99. deferred
  100. end
  101. feature {COMMAND_LINE_ARGUMENTS, COMMAND_LINE_ARGUMENT}
  102. set_mandatory (parent_option: like Current; enable: BOOLEAN)
  103. require
  104. parent_option /= Void
  105. enable /= is_mandatory
  106. enable implies can_be_mandatory
  107. ;(not enable) implies can_be_optional
  108. deferred
  109. ensure
  110. parent = parent_option
  111. is_mandatory = enable
  112. end
  113. feature {}
  114. parent: like Current
  115. -- Internal technical trick to ensure that arguments with a non-standard behaviour (`set_mandatory')
  116. -- are correctly managed.
  117. deferred
  118. end
  119. invariant
  120. is_optional or else is_positional or else is_mandatory
  121. end -- class COMMAND_LINE_TYPED_ARGUMENT
  122. --
  123. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  124. --
  125. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  126. -- of this software and associated documentation files (the "Software"), to deal
  127. -- in the Software without restriction, including without limitation the rights
  128. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  129. -- copies of the Software, and to permit persons to whom the Software is
  130. -- furnished to do so, subject to the following conditions:
  131. --
  132. -- The above copyright notice and this permission notice shall be included in
  133. -- all copies or substantial portions of the Software.
  134. --
  135. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  136. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  137. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  138. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  139. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  140. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  141. -- THE SOFTWARE.