-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_line_parser.e
More file actions
174 lines (147 loc) · 3.67 KB
/
command_line_parser.e
File metadata and controls
174 lines (147 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
note
description: "Summary description for {COMMAND_LINE_PARSER}."
date: "$Date$"
revision: "$Revision$"
class
COMMAND_LINE_PARSER
inherit
ARGUMENTS_32
create
make
feature {NONE} -- Initialization
make
-- Create a new command-line parser.
do
reset
end
feature -- Operations
reset
-- Reset internal option position to first option
-- must be called before first use.
do
next_option_position := 1
end
consume_option
-- Move `next_token_position' to the next token position.
do
next_option_position := next_option_position + 1
end
feature -- Status report
has_next_option: BOOLEAN
-- Is there an unconsumed token left?
do
Result := is_valid_option_position (next_option_position)
end
is_next_option_long_option: BOOLEAN
-- Is the next option a long option (with or without a value)?
local
arg: STRING_32
do
if has_next_option then
arg := next_option.as_string_32
Result := arg.count >= 2 and then
arg.substring (1, 2).same_string ("--")
end
end
has_next_option_value: BOOLEAN
-- Has the next option a value?
require
has_next_option: has_next_option
next_option_is_long_option: is_next_option_long_option
local
i: INTEGER
arg: STRING_32
do
arg := next_option.as_string_32
i := arg.index_of ('=', 1)
Result := (i >= 1 and i < arg.count)
end
feature -- Access
next_option: STRING_32
-- Next option on command-line
require
has_next_option: has_next_option
do
Result := argument (next_option_position).as_string_32
ensure
next_option_not_void: Result /= Void
end
next_option_value: STRING_32
-- Value of next option
require
next_option_is_long_option: is_next_option_long_option
has_next_option_value: has_next_option_value
local
i: INTEGER
arg: STRING_32
do
arg := next_option.as_string_32
i := arg.index_of ('=', 1)
Result := arg.substring (i + 1, arg.count)
ensure
next_option_value_not_void: Result /= Void
end
feature -- Matching
match_long_option (an_option_name: STRING): BOOLEAN
-- Is there a next option on the command-line and
-- is this option a long option whose name is
-- `an_option_name' (Note that `an_option_name'
-- does not contain the leading '--' characters)?
require
an_option_name_not_void: an_option_name /= Void
local
arg: STRING_32
nb: INTEGER
do
if has_next_option then
arg := next_option.as_string_32
nb := an_option_name.count + 2
if
arg.count >= nb and then
(arg.item (1) = '-' and
arg.item (2) = '-') and then
arg.substring (3, nb).same_string (an_option_name)
then
Result := (arg.count = nb or else arg.item (nb + 1) = '=')
end
end
end
feature {NONE} -- Implementation
next_option_position: INTEGER
-- Index of next option
is_valid_option_position (i: INTEGER): BOOLEAN
-- Is `i' a valid token position?
do
Result := (i >= 1 and i <= argument_count)
end
feature -- Status report
has_long_option (an_option_name: STRING): BOOLEAN
-- Is there a long option on the command-line whose name is
-- `an_option_name' (note that `an_option_name' does not
-- contain the leading '--' characters)?
require
an_option_name_not_void: an_option_name /= Void
local
i: INTEGER
arg: STRING_32
nb: INTEGER
do
from
i := 1
until
(i > argument_count) or Result
loop
arg := argument (i).as_string_32
nb := an_option_name.count + 2
if
arg.count >= nb and then
(arg.item (1) = '-' and
arg.item (2) = '-') and then
arg.substring (3, nb).same_string (an_option_name)
then
Result := (arg.count = nb or else arg.item (nb + 1) = '=')
end
i := i + 1
end
end
end