/
ModeCondition.winxed
142 lines (124 loc) · 3.33 KB
/
ModeCondition.winxed
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
/* A condition for a ProgramMode
Each ProgramMode besides the default mode may have one or more conditions
that must be met before that ProgramMode can be activated. This is the
abstract parent class of specialized conditions.
*/
class Rosella.CommandLine.ModeCondition
{
// Determine if this condition is satisifed by the given list of parsed
// arguments
function is_satisfied(var args)
{
return false;
}
// Get a description of this condition, for use in a help/usage message
function get_description(var sb)
{
}
function __get_flag_name(string name)
{
if (length(name) == 1)
return "-" + name;
return "--" + name;
}
}
// Condition that requires the presence of a given flag
class Rosella.CommandLine.ModeCondition.FlagSet : Rosella.CommandLine.ModeCondition
{
var flagname;
function FlagSet(string flagname)
{
self.flagname = flagname;
}
function is_satisfied(var args)
{
string flagname = string(self.flagname);
int value = args[flagname];
return value;
}
function get_description(var sb)
{
string flagname = self.flagname;
push(sb, " ");
push(sb, self.__get_flag_name(flagname));
}
}
// Condition that requires a given scalar-valued flag
class Rosella.CommandLine.ModeCondition.ScalarValue : Rosella.CommandLine.ModeCondition
{
var name;
var value;
function ScalarValue(string name, string value)
{
self.name = name;
self.value = value;
}
function is_satisfied(var args)
{
string name = self.name;
string expectedvalue = self.value;
string actualvalue = args[name];
if (expectedvalue == actualvalue)
return true;
return false;
}
function get_description(var sb)
{
string name = self.name;
string value = self.value;
push(sb, " ");
push(sb, self.__get_flag_name(name));
push(sb, "=");
push(sb, value);
}
}
// Condition that uses a user-specified predicate function
class Rosella.CommandLine.ModeCondition.GenericFunc : Rosella.CommandLine.ModeCondition
{
var func;
var desc;
function GenericFunc(var f, string desc)
{
self.func = f;
self.desc = desc;
}
function is_satisfied(var args)
{
var f = self.func;
return f(args);
}
function get_description(var sb)
{
string desc = self.desc;
push(sb, " ");
push(sb, desc);
}
}
class Rosella.CommandLine.ModeCondition.RequiredPositionals : Rosella.CommandLine.ModeCondition
{
var required_args;
var can_have_more;
function RequiredPositionals(int can_have_more, var args)
{
self.required_args = args;
self.can_have_more = can_have_more;
}
function is_satisfied(var args)
{
if (self.can_have_more) {
return elements(args.pos_cache) >= elements(self.required_args);
} else {
return elements(args.pos_cache) == elements(self.required_args);
}
}
function get_description(var sb)
{
for (string arg in self.required_args) {
push(sb, " <");
push(sb, arg);
push(sb, ">");
}
if (self.can_have_more)
push(sb, " ...");
}
}