PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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