Commit 33d42c08

33d42c08e6b5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified

Description

Implement minimal OptionParser in lib/optparse.rb

Pure Ruby implementation compilable by Spinel. Supports:

  • on(flag, desc, &block) for --flag and --flag=VALUE
  • parse!(argv) with in-place argv mutation
  • ParseError exception class
  • banner=, separator, on_tail (display-only, no-ops)

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
33d42c08e6b5
Parent
faa00164f529

lib/optparse.rbmodifiedView file

@@ -1,2 +1,123 @@
11 # Spinel stub: optparse
2-# Placeholder — OptionParser requires full stdlib implementation.
2+#
3+# Minimal OptionParser implementation for Spinel compilation.
4+# Supports the subset of OptionParser used by lrama:
5+# - .on(flag, desc) { |v| ... } (2-4 args with block)
6+# - .banner=
7+# - .separator(text)
8+# - .on_tail(text)
9+# - .parse!(argv)
10+# - OptionParser::ParseError
11+
12+class OptionParser
13+ class ParseError
14+ def initialize(msg)
15+ @message = msg
16+ end
17+
18+ def message
19+ @message
20+ end
21+
22+ def to_s
23+ @message
24+ end
25+ end
26+
27+ def initialize
28+ @banner = ""
29+ @short_flags = []
30+ @long_flags = []
31+ @handlers = []
32+ @expects_value = []
33+ end
34+
35+ def banner=(text)
36+ @banner = text
37+ end
38+
39+ def separator(text)
40+ end
41+
42+ def on_tail(text)
43+ end
44+
45+ def on(a1, a2, &block)
46+ short = ""
47+ long = ""
48+ has_value = false
49+ if a1[0] == "-" && a1[1] == "-"
50+ long = a1
51+ else
52+ short = a1
53+ end
54+ if a2[0] == "-" && a2[1] == "-"
55+ long = a2
56+ end
57+ if long.include?("=")
58+ has_value = true
59+ end
60+ @short_flags.push(short)
61+ @long_flags.push(long)
62+ @handlers.push(block)
63+ @expects_value.push(has_value)
64+ end
65+
66+ def parse!(argv)
67+ new_argv = []
68+ i = 0
69+ while i < argv.length
70+ arg = argv[i]
71+ matched = false
72+ j = 0
73+ while j < @handlers.length
74+ sf = @short_flags[j]
75+ lf = @long_flags[j]
76+ ev = @expects_value[j]
77+ if ev && lf != ""
78+ # extract base flag name before "="
79+ eq_pos = lf.index("=")
80+ if eq_pos
81+ base = lf[0, eq_pos]
82+ else
83+ base = lf
84+ end
85+ # --flag=VALUE form
86+ if arg.length > base.length && arg[0, base.length] == base && arg[base.length] == "="
87+ value = arg[(base.length + 1), (arg.length - base.length - 1)]
88+ @handlers[j].call(value)
89+ matched = true
90+ # --flag VALUE form
91+ elsif arg == base
92+ i += 1
93+ if i < argv.length
94+ @handlers[j].call(argv[i])
95+ end
96+ matched = true
97+ end
98+ else
99+ if arg == sf || arg == lf
100+ @handlers[j].call(true)
101+ matched = true
102+ end
103+ end
104+ j += 1
105+ end
106+ if !matched
107+ new_argv.push(arg)
108+ end
109+ i += 1
110+ end
111+ # replace argv contents with unmatched args
112+ argv.clear
113+ k = 0
114+ while k < new_argv.length
115+ argv.push(new_argv[k])
116+ k += 1
117+ end
118+ end
119+
120+ def to_s
121+ @banner
122+ end
123+end