-
Notifications
You must be signed in to change notification settings - Fork 3
/
Context.pm6
141 lines (114 loc) · 4.15 KB
/
Context.pm6
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
use Getopt::Advance::Utils;
unit module Getopt::Advance::Context;
role Context is export {
has $.success;
method TWEAK() {
$!success = False;
}
method mark-matched() {
$!success = True;
}
method match(ContextProcessor, $o) { ... }
method set(ContextProcessor, $o) { ... }
method gist() { ... }
}
class TheContext is export {
class Option does Context {
has $.prefix;
has $.name;
has $.hasarg;
has &.getarg;
has $.canskip;
method match(ContextProcessor $cp, $o) {
my $name-r = do given $!prefix {
when Prefix::LONG {
$o.long eq $!name;
}
when Prefix::SHORT {
$o.short eq $!name;
}
when Prefix::NULL {
$o.long eq $!name || $o.short eq $!name
}
default {
False;
}
};
Debug::debug(" - Name => { $name-r ?? 'OK' !! 'Failed' }");
my $value-r = False;
if $o.need-argument == $!hasarg {
Debug::debug(" - Value [{&!getarg()}] for [{$o.usage}]");
$value-r = &!getarg.defined ?? $o.match-value(&!getarg()) !! True;
}
Debug::debug(" - Match " ~ ($name-r && $value-r ?? "Okay!" !! "Failed!"));
return $name-r && $value-r;
}
method set(ContextProcessor $cp, $o) {
self.mark-matched();
$o.set-value(&!getarg(), :callback);
Debug::debug(" - OK! Set value {&!getarg()} for [{$o.usage}], shift args: {self.canskip}");
}
method gist() { "\{{$!prefix}, {$!name}{$!hasarg ?? ":" !! ""}\}" }
}
class DelayOption is Option {
method set(ContextProcessor $cp, $o) {
self.mark-matched();
Debug::debug(" - OK! Delay set value {self.getarg()()} for [{$o.usage}], shift args: {$o.need-argument}");
OptionValueSetter.new(
optref => $o,
value => self.getarg()(),
);
}
}
class NonOption does Context {
has @.argument;
has $.index;
method match(ContextProcessor $cp, $no) {
my $style-r = $no.match-style($cp.style);
Debug::debug(" - Style => { $style-r ?? 'OK' !! 'Failed' }");
my $name-r = $style-r && do given $cp.style {
when Style::MAIN {
$no.match-name("");
}
default {
$no.match-name(@!argument[$!index].Str);
}
};
Debug::debug(" - Name => { $name-r ?? 'OK' !! 'Failed' }");
my $index-r = $name-r && $no.match-index(+@!argument, $!index);
Debug::debug(" - Index => { $index-r ?? 'OK' !! 'Failed' }");
my $call-r = $index-r && do {
given $cp.style {
when Style::POS | Style::WHATEVERPOS {
Debug::debug(" - Try call {$cp.style} sub.");
$no.($no.owner, @!argument[$!index]);
}
when Style::CMD {
my @realargs = @!argument[1..*-1];
Debug::debug(" - Try call {$cp.style} sub.");
$no.($no.owner, @realargs);
}
default {
Debug::debug(" - Try call {$cp.style} sub.");
$no.($no.owner, @!argument);
}
}
};
Debug::debug(" - Match " ~ ($call-r ?? "Okay!" !! "Failed!"));
return $call-r;
}
method set(ContextProcessor $cp, $no) { }
method gist() {
my $gist = "\{";
$gist ~= [ "{.Str}\@{.index}" for self.argument ].join(",");
$gist ~ '}';
};
}
class Pos is NonOption {
method gist() {
given self.argument[self.index] {
"\{{.Str}\@{.index}\}";
}
}
}
}