/src/lib/cli/command_line_arguments.e

http://github.com/tybor/Liberty · Specman e · 115 lines · 76 code · 13 blank · 26 comment · 4 complexity · 7f584235864004921957fb3f1f52714d MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class COMMAND_LINE_ARGUMENTS
  5. --
  6. -- An object of that class manages a command-line parsing.
  7. --
  8. insert
  9. ARGUMENTS
  10. create {ANY}
  11. make
  12. feature {ANY}
  13. set_helper (a_helper: like helper)
  14. do
  15. if a_helper /= Void then
  16. helper := a_helper
  17. else
  18. helper := agent default_helper(?)
  19. end
  20. ensure
  21. a_helper /= Void implies helper = a_helper
  22. end
  23. parse_command_line: BOOLEAN
  24. do
  25. Result := parse_argument(cli_argument)
  26. if not Result and then parse_argument(help_argument) then
  27. if help_argument.is_set then
  28. helper.call([Current])
  29. Result := True
  30. end
  31. end
  32. end
  33. usage (stream: OUTPUT_STREAM)
  34. do
  35. stream.put_line(once "Usage: ")
  36. stream.put_string(command_name)
  37. stream.put_character(' ')
  38. cli_argument.usage_summary(stream)
  39. stream.put_new_line
  40. stream.put_new_line
  41. stream.put_line(once "Options:")
  42. stream.put_new_line
  43. cli_argument.usage_details(stream)
  44. end
  45. feature {}
  46. parse_argument (a_argument: COMMAND_LINE_ARGUMENT): BOOLEAN
  47. local
  48. context: COMMAND_LINE_CONTEXT
  49. do
  50. context.init
  51. a_argument.prepare_parse
  52. context := a_argument.parse_command_line(context)
  53. Result := context.is_parsed
  54. and then (a_argument.is_mandatory implies a_argument.is_set)
  55. and then (context.index = argument_count + 1 or else (context.is_short and then context.short_index = argument_count + 1))
  56. end
  57. make (a_argument: like cli_argument)
  58. require
  59. a_argument /= Void
  60. do
  61. cli_argument := a_argument
  62. helper := agent default_helper(?)
  63. ensure
  64. cli_argument = a_argument
  65. end
  66. cli_argument: COMMAND_LINE_ARGUMENT
  67. help_argument: COMMAND_LINE_ARGUMENT
  68. local
  69. factory: COMMAND_LINE_ARGUMENT_FACTORY
  70. once
  71. Result := factory.option_boolean("h", "help", Void)
  72. end
  73. helper: PROCEDURE[TUPLE[COMMAND_LINE_ARGUMENTS]]
  74. default_helper (a_arguments: COMMAND_LINE_ARGUMENTS)
  75. do
  76. check a_arguments = Current end
  77. a_arguments.usage(std_output)
  78. die_with_code(0)
  79. end
  80. invariant
  81. cli_argument /= Void
  82. end -- COMMAND_LINE_ARGUMENTS
  83. --
  84. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  85. --
  86. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  87. -- of this software and associated documentation files (the "Software"), to deal
  88. -- in the Software without restriction, including without limitation the rights
  89. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  90. -- copies of the Software, and to permit persons to whom the Software is
  91. -- furnished to do so, subject to the following conditions:
  92. --
  93. -- The above copyright notice and this permission notice shall be included in
  94. -- all copies or substantial portions of the Software.
  95. --
  96. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  97. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  98. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  99. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  100. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  101. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  102. -- THE SOFTWARE.