FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mercury/java/runtime/Getopt.java at master · Mercury-Language/mercury · GitHub
Mercury-Language
/
mercury
Public
Notifications
You must be signed in to change notification settings
Fork
71
Star
1.1k
Code
Issues
13
Pull requests
8
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
mercury
/
java
/
runtime
/
Getopt.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
168 lines (148 loc) · 4.51 KB
Breadcrumbs
mercury
/
java
/
runtime
/
Getopt.java
Copy path
File metadata and controls
168 lines (148 loc) · 4.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// vim: ts=4 sw=4 expandtab ft=java
//
// Copyright (C) 2014, 2018 The Mercury Team
// This file is distributed under the terms specified in COPYING.LIB.
//
package
jmercury
.
runtime
;
import
java
.
util
.*;
/**
* Getopt style option passing.
* This is currently very simple, it does only what is necessary.
*/
public
class
Getopt
implements
Iterable
<
Getopt
.
Option
>
{
String
option_string
;
List
<
Option
>
options
;
List
<
String
>
arguments
;
/**
* Create a Getopt object to process the command line options.
*/
public
Getopt
(
String
option_string
)
{
this
.
option_string
=
option_string
;
this
.
options
=
null
;
}
public
Iterator
<
Option
>
iterator
()
{
if
(
options
==
null
) {
process
();
}
return
options
.
iterator
();
}
/**
* Process the options. This is called automatically and the result is
* cached.
*/
public
void
process
()
{
StringTokenizer
tok
=
new
StringTokenizer
(
option_string
);
Option
option
=
null
;
options
=
new
LinkedList
<
Option
>();
arguments
=
new
LinkedList
<
String
>();
while
(
tok
.
hasMoreTokens
()) {
String
str
;
str
=
tok
.
nextToken
();
if
(
str
.
startsWith
(
"--"
)) {
if
(
option
!=
null
) {
options
.
add
(
option
);
option
=
null
;
}
option
=
new
Option
(
str
.
substring
(
2
));
}
else
if
(
str
.
startsWith
(
"-"
)) {
if
(
option
!=
null
) {
options
.
add
(
option
);
option
=
null
;
}
option
=
new
Option
(
str
.
substring
(
1
));
}
else
{
if
(
option
!=
null
) {
option
.
setValue
(
str
);
options
.
add
(
option
);
option
=
null
;
}
else
{
arguments
.
add
(
str
);
// The token we have got is an argument, not an option.
// Store the remaining strings as arguments, and
// stop processing.
while
(
tok
.
hasMoreTokens
()) {
str
=
tok
.
nextToken
();
arguments
.
add
(
str
);
}
break
;
}
}
}
}
/**
* Get the arguments from the command line. Arguments are not options,
* they occur after the options list.
*/
public
List
<
String
>
getArguments
() {
return
arguments
;
}
/**
* An option. Call getopt.nextOption() to get each option.
*/
public
static
class
Option
{
// the name of the option. This is stored without either the - or
// -- that may proceed option names.
private
String
option
;
// the value of the option or null.
private
String
value
;
/**
* Create a new option.
* @param option The name of the option, excluding any leading - or
* -- substring.
* @param value The value of the option.
*/
public
Option
(
String
option
,
String
value
)
{
this
.
option
=
option
;
this
.
value
=
value
;
}
/**
* Create a new option.
* The option's value will be initialised to null.
* @param option The name of the option, excluding any leading - or
* -- substring.
*/
public
Option
(
String
option
)
{
this
(
option
,
null
);
}
public
void
setValue
(
String
value
)
{
this
.
value
=
value
;
}
/**
* True if this option matches the given option name.
* @param option The option name for comparison without leading --
* or -.
*/
public
boolean
optionIs
(
String
option
)
{
return
this
.
option
.
equals
(
option
);
}
public
int
getValueInt
()
throws
NumberFormatException
{
return
Integer
.
parseInt
(
value
);
}
public
String
toString
()
{
StringBuilder
builder
=
new
StringBuilder
();
// Add dashes.
if
(
option
.
length
() ==
1
) {
builder
.
append
(
"-"
);
}
else
{
// This must be a double dash argument.
builder
.
append
(
"--"
);
}
builder
.
append
(
option
);
builder
.
append
(
" "
);
builder
.
append
(
value
);
return
builder
.
toString
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL