-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathOption.java
More file actions
161 lines (133 loc) · 3.49 KB
/
Option.java
File metadata and controls
161 lines (133 loc) · 3.49 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
package org.docopt;
import static org.docopt.Python.bool;
import static org.docopt.Python.partition;
import static org.docopt.Python.repr;
import static org.docopt.Python.split;
import java.util.List;
import org.docopt.Python.Re;
final class Option extends LeafPattern {
private final String $short;
private final String $long;
private final int argCount;
// >>> def __init__(self, short=None, long=None, argcount=0, value=False)
public Option(final String $short, final String $long, final int argCount,
final Object value) {
super(
// >>> @property
// >>> def name(self):
// >>> return self.long or self.short
($long != null) ? $long : $short,
// >>> self.value = None if value is False and argcount else
// value
(Boolean.FALSE.equals(value) && argCount != 0) ? null : value);
assert argCount == 0 || argCount == 1;
this.$short = $short;
this.$long = $long;
this.argCount = argCount;
}
public Option(final String $short, final String $long, final int argCount) {
this($short, $long, argCount, false);
}
public Option(final String $short, final String $long) {
this($short, $long, 0);
}
public static Option parse(final String optionDescription) {
String $short = null;
String $long = null;
int argCount = 0;
Object value = false;
String options;
String description;
// >>> options, _, description = option_description.strip().partition('
// ')
{
final String[] a = partition(optionDescription.trim(), " ");
options = a[0];
description = a[2];
}
options = options.replaceAll(",", " ").replaceAll("=", " ");
for (final String s : split(options)) {
if (s.startsWith("--")) {
$long = s;
}
else if (s.startsWith("-")) {
$short = s;
}
else {
argCount = 1;
}
}
if (argCount != 0) {
final List<String> matched = Re.findAll("\\[default: (.*)\\]",
description, Re.IGNORECASE);
value = bool(matched) ? matched.get(0) : null;
}
return new Option($short, $long, argCount, value);
}
@Override
protected SingleMatchResult singleMatch(final List<LeafPattern> left) {
for (int n = 0; n < left.size(); n++) {
final LeafPattern pattern = left.get(n);
if (getName().equals(pattern.getName())) {
return new SingleMatchResult(n, pattern);
}
}
return new SingleMatchResult(null, null);
}
public String getShort() {
return $short;
}
public String getLong() {
return $long;
}
public int getArgCount() {
return argCount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (($long == null) ? 0 : $long.hashCode());
result = prime * result + (($short == null) ? 0 : $short.hashCode());
result = prime * result + argCount;
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Option other = (Option) obj;
if ($long == null) {
if (other.$long != null) {
return false;
}
}
else if (!$long.equals(other.$long)) {
return false;
}
if ($short == null) {
if (other.$short != null) {
return false;
}
}
else if (!$short.equals(other.$short)) {
return false;
}
if (argCount != other.argCount) {
return false;
}
return true;
}
@Override
public String toString() {
return String.format("%s(%s, %s, %s, %s)", getClass().getSimpleName(),
repr($short), repr($long), repr(argCount), repr(getValue()));
}
}