PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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