/src/lib/cli/command_line_context.e

http://github.com/tybor/Liberty · Specman e · 117 lines · 73 code · 13 blank · 31 comment · 3 complexity · 2a1f22c87b192c1f052cc1c47d74ca3d 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_CONTEXT
  5. --
  6. -- Internal command-line parsing context.
  7. --
  8. insert
  9. HASHABLE
  10. COMPARABLE
  11. feature {ANY}
  12. hash_code: INTEGER
  13. do
  14. if is_short then
  15. Result := short_index #+ (short_option_index #* 4096)
  16. if Result < 0 then
  17. Result := ~Result
  18. end
  19. else
  20. Result := index
  21. end
  22. end
  23. infix "<" (other: like Current): BOOLEAN
  24. do
  25. if index = other.index then
  26. Result := short_option_index < other.short_option_index
  27. else
  28. Result := index < other.index
  29. end
  30. end
  31. feature {ANY}
  32. index: INTEGER
  33. -- set to the next argument to read
  34. short_index: INTEGER
  35. -- set to the next option parameter position when parsing a short parameters sequence
  36. short_option_index: INTEGER
  37. -- position of the short option in the short option sequence
  38. is_parsed: BOOLEAN
  39. -- True if the parsing may continue. False if a grave parsing error occurs (such as an option present
  40. -- but with invalid data)
  41. do
  42. Result := index > 0
  43. end
  44. is_short: BOOLEAN
  45. do
  46. Result := short_option_index > 1
  47. end
  48. feature {COMMAND_LINE_ARGUMENTS}
  49. init
  50. do
  51. index := 1
  52. short_index := 2
  53. short_option_index := 1
  54. ensure
  55. index = 1
  56. is_parsed
  57. end
  58. feature {COMMAND_LINE_ARGUMENT, CLARG_PARSER}
  59. set_short (a_short_index: like short_index; a_short_option_index: like short_option_index)
  60. require
  61. a_short_index > index
  62. do
  63. short_index := a_short_index
  64. short_option_index := a_short_option_index
  65. ensure
  66. short_index = a_short_index
  67. short_option_index = a_short_option_index
  68. is_short
  69. end
  70. set_index (a_index: like index)
  71. require
  72. a_index > 0
  73. do
  74. index := a_index
  75. short_index := a_index + 1
  76. short_option_index := 1
  77. ensure
  78. index = a_index
  79. not is_short
  80. end
  81. invariant
  82. index > 0 implies short_index > index
  83. index > 0 implies short_option_index >= 1
  84. end -- class COMMAND_LINE_CONTEXT
  85. --
  86. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  87. --
  88. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  89. -- of this software and associated documentation files (the "Software"), to deal
  90. -- in the Software without restriction, including without limitation the rights
  91. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  92. -- copies of the Software, and to permit persons to whom the Software is
  93. -- furnished to do so, subject to the following conditions:
  94. --
  95. -- The above copyright notice and this permission notice shall be included in
  96. -- all copies or substantial portions of the Software.
  97. --
  98. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  99. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  100. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  101. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  102. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  103. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  104. -- THE SOFTWARE.