/src/lib/cli/internal/clarg_and.e
Specman e | 178 lines | 136 code | 19 blank | 23 comment | 9 complexity | d1027edda1a74238c6ea82bd5a31d5d5 MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4class CLARG_AND 5 6inherit 7 COMMAND_LINE_ARGUMENT 8 redefine 9 infix "and", infix "and then" 10 end 11 12create {COMMAND_LINE_ARGUMENT} 13 make 14 15feature {ANY} 16 is_repeatable: BOOLEAN False 17 18 infix "and", infix "and then" (other: COMMAND_LINE_ARGUMENT): COMMAND_LINE_ARGUMENT 19 do 20 args.add_last(other) 21 mandatory_count_memory := -1 22 Result := Current 23 end 24 25 is_set: BOOLEAN 26 27 is_mandatory: BOOLEAN 28 do 29 Result := args.exists(agent {COMMAND_LINE_ARGUMENT}.is_mandatory) 30 end 31 32 is_set_at (context: COMMAND_LINE_CONTEXT): BOOLEAN 33 do 34 Result := args.exists(agent {COMMAND_LINE_ARGUMENT}.is_set_at(context)) 35 end 36 37feature {COMMAND_LINE_ARGUMENTS, COMMAND_LINE_ARGUMENT} 38 prepare_parse 39 do 40 is_set := False 41 set_count := 0 42 args.for_each(agent {COMMAND_LINE_ARGUMENT}.prepare_parse) 43 end 44 45 parse_command_line (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT 46 do 47 Result := parse_cli(context) 48 is_set := set_count = mandatory_count 49 end 50 51 undo_parse (context: COMMAND_LINE_CONTEXT) 52 do 53 args.for_each(agent {COMMAND_LINE_ARGUMENT}.undo_parse(context)) 54 end 55 56 usage_summary (stream: OUTPUT_STREAM) 57 local 58 i: INTEGER 59 do 60 from 61 i := args.lower 62 until 63 i > args.upper 64 loop 65 if i > args.lower then 66 stream.put_character(' ') 67 end 68 args.item(i).usage_summary(stream) 69 i := i + 1 70 end 71 detailed := False 72 end 73 74 usage_details (stream: OUTPUT_STREAM) 75 do 76 if not detailed then 77 args.for_each(agent {COMMAND_LINE_ARGUMENT}.usage_details(stream)) 78 detailed := True 79 end 80 end 81 82feature {} 83 parse_cli (context: COMMAND_LINE_CONTEXT): COMMAND_LINE_CONTEXT 84 local 85 i: INTEGER; arg: COMMAND_LINE_ARGUMENT; ctx: COMMAND_LINE_CONTEXT 86 do 87 from 88 Result := context 89 i := args.lower 90 until 91 not Result.is_parsed or else i > args.upper 92 loop 93 arg := args.item(i) 94 if not arg.is_set_at(Result) then 95 ctx := arg.parse_command_line(Result) 96 if ctx.is_parsed and then arg.is_set_at(Result) then 97 ctx := parse_cli(ctx) 98 check 99 arg.is_set_at(Result) 100 end 101 if ctx.is_parsed then 102 Result := ctx 103 if arg.is_mandatory then 104 set_count := set_count + 1 105 end 106 else 107 arg.undo_parse(Result) 108 end 109 end 110 end 111 i := i + 1 112 end 113 end 114 115 mandatory_count: INTEGER 116 local 117 i: INTEGER 118 do 119 Result := mandatory_count_memory 120 if Result < 0 then 121 from 122 Result := 0 123 i := args.lower 124 invariant 125 variant 126 (args.upper - args.lower + 1) - i 127 until 128 i > args.upper 129 loop 130 if args.item(i).is_mandatory then 131 Result := Result + 1 132 end 133 i := i + 1 134 end 135 mandatory_count_memory := Result 136 end 137 end 138 139feature {} 140 args: FAST_ARRAY[COMMAND_LINE_ARGUMENT] 141 set_count: INTEGER 142 mandatory_count_memory: INTEGER 143 144 make (a_left, a_right: COMMAND_LINE_ARGUMENT) 145 require 146 a_left /= Void 147 a_right /= Void 148 do 149 args := {FAST_ARRAY[COMMAND_LINE_ARGUMENT] << a_left, a_right >> } 150 mandatory_count_memory := -1 151 end 152 153 detailed: BOOLEAN 154 155invariant 156 args.count >= 2 157 158end -- class CLARG_AND 159-- 160-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 161-- 162-- Permission is hereby granted, free of charge, to any person obtaining a copy 163-- of this software and associated documentation files (the "Software"), to deal 164-- in the Software without restriction, including without limitation the rights 165-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 166-- copies of the Software, and to permit persons to whom the Software is 167-- furnished to do so, subject to the following conditions: 168-- 169-- The above copyright notice and this permission notice shall be included in 170-- all copies or substantial portions of the Software. 171-- 172-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 173-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 174-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 175-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 176-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 177-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 178-- THE SOFTWARE.