PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/cli/internal/clarg_counter.e

http://github.com/tybor/Liberty
Specman e | 197 lines | 151 code | 23 blank | 23 comment | 12 complexity | c136e485466e1b7a86c6e578f268b366 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class CLARG_COUNTER
  5
  6inherit
  7   COMMAND_LINE_TYPED_ARGUMENT[INTEGER]
  8      redefine
  9         out_in_tagged_out_memory
 10      end
 11
 12insert
 13   ARGUMENTS
 14      redefine
 15         out_in_tagged_out_memory
 16      end
 17   CLARG_OPTIONS
 18      redefine
 19         out_in_tagged_out_memory
 20      end
 21
 22create {COMMAND_LINE_ARGUMENT_FACTORY}
 23   make
 24
 25feature {ANY}
 26   item: INTEGER
 27      do
 28         Result := set.count
 29      end
 30
 31   usage: FIXED_STRING
 32
 33   is_mandatory: BOOLEAN False
 34   can_be_mandatory: BOOLEAN False
 35   is_optional: BOOLEAN True
 36   can_be_optional: BOOLEAN True
 37   is_positional: BOOLEAN False
 38
 39   is_repeatable: BOOLEAN True
 40
 41   is_set: BOOLEAN
 42      do
 43         Result := not set.is_empty
 44      end
 45
 46   force_index (a_index: INTEGER)
 47      do
 48         check False end
 49      end
 50
 51   out_in_tagged_out_memory
 52      do
 53         if long /= Void then
 54            tagged_out_memory.append(once "--")
 55            tagged_out_memory.append(long)
 56         else
 57            tagged_out_memory.append(once "-")
 58            tagged_out_memory.append(short)
 59         end
 60      end
 61
 62feature {COMMAND_LINE_ARGUMENTS, COMMAND_LINE_ARGUMENT}
 63   prepare_parse
 64      do
 65         set.clear_count
 66      end
 67
 68   parse_command_line (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT
 69      local
 70         arg: STRING
 71      do
 72         Result := context
 73         if context.is_short then
 74            if is_short(argument(context.index), context.short_option_index) then
 75               set.add(context)
 76               Result.set_index(context.index)
 77               Result.set_short(context.short_index, context.short_option_index + 1)
 78            elseif argument_count >= context.short_index and then is_long(argument(context.short_index)) then
 79               set.add(context)
 80               Result.set_index(context.short_index + 1)
 81            end
 82         elseif argument_count >= context.index then
 83            arg := argument(context.index)
 84            if is_short(arg, 1) then
 85               set.add(context)
 86               Result.set_index(context.index)
 87               Result.set_short(context.index + 1, context.short_option_index + 1)
 88            elseif is_long(arg) then
 89               set.add(context)
 90               Result.set_index(context.index + 1)
 91            end
 92         end
 93      end
 94
 95   usage_summary (stream: OUTPUT_STREAM)
 96      do
 97         if short /= Void then
 98            if long /= Void then
 99               stream.put_character('(')
100               put_short(stream)
101               stream.put_character('|')
102               put_long(stream)
103               stream.put_character(')')
104            else
105               put_short(stream)
106            end
107         elseif long /= Void then
108            put_long(stream)
109         end
110         stream.put_string(once "...")
111         detailed := False
112      end
113
114   usage_details (stream: OUTPUT_STREAM)
115      do
116         if not detailed then
117            usage_summary(stream)
118            stream.put_character(':')
119            stream.put_character(' ')
120            if usage /= Void then
121               stream.put_line(usage)
122            else
123               stream.put_line(once "Increment the counter.")
124            end
125            detailed := True
126         end
127      end
128
129   set_mandatory (parent_option: like Current; enable: BOOLEAN)
130      do
131         check False end
132      end
133
134   is_set_at (context: COMMAND_LINE_CONTEXT): BOOLEAN
135      do
136         Result := set.has(context)
137      end
138
139   undo_parse (context: COMMAND_LINE_CONTEXT)
140      do
141         set.remove(context)
142      end
143
144feature {}
145   make (a_short, a_long, a_usage: ABSTRACT_STRING)
146      require
147         a_short /= Void implies a_short.count = 1
148         a_short /= Void or else a_long /= Void
149      do
150         create set.make
151         if a_short /= Void then
152            short := a_short.intern
153         end
154         if a_long /= Void then
155            long := a_long.intern
156         end
157         if a_usage /= Void then
158            usage := a_usage.intern
159         end
160      ensure
161         a_short /= Void implies short.is_equal(a_short)
162         a_long /= Void implies long.is_equal(a_long)
163         a_usage /= Void implies usage.is_equal(a_usage)
164      end
165
166   parent: like Current
167
168   set: HASHED_SET[COMMAND_LINE_CONTEXT]
169
170   detailed: BOOLEAN
171
172invariant
173   parent = Void
174   is_optional
175   set /= Void
176
177end -- class CLARG_COUNTER
178--
179-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
180--
181-- Permission is hereby granted, free of charge, to any person obtaining a copy
182-- of this software and associated documentation files (the "Software"), to deal
183-- in the Software without restriction, including without limitation the rights
184-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
185-- copies of the Software, and to permit persons to whom the Software is
186-- furnished to do so, subject to the following conditions:
187--
188-- The above copyright notice and this permission notice shall be included in
189-- all copies or substantial portions of the Software.
190--
191-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
194-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
195-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
196-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
197-- THE SOFTWARE.