simple-cli-options 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/option.rb +19 -3
- data/lib/options.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a20b6b59fed0e7c00325c3df20d25fdd3e860bfb8e4635e824abf29ad413348a
|
|
4
|
+
data.tar.gz: 4e1dde6d25abb317503dd35eee37199886163c37d3b79f2a250b3b6c09749e49
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eea0745fe1eb1a4616f70e90f9d8f5397fc4ebea94018c1e99d97310a949c28656f46a32ecdba211189dba58689edb54bc5b022aea7a9502442cabc006923e62
|
|
7
|
+
data.tar.gz: 299417202d0fe3f9bdb7cc0ce4db4c92a6ab02843dc23fb9485f59ac10c41a4463efc689ffe421c8c1df38611455ff2b161829870b871b13a69c6ba9a7d64a7f
|
data/lib/option.rb
CHANGED
|
@@ -39,11 +39,15 @@ class Option
|
|
|
39
39
|
validate_structure!
|
|
40
40
|
|
|
41
41
|
# バリデーションは追加(Array)可能、変換は上書き
|
|
42
|
-
|
|
42
|
+
# 各バリデータは、成功時に nil、失敗時にエラーメッセージ(String)を返す想定
|
|
43
|
+
@validators = T.let(
|
|
44
|
+
[options[:validate]].compact,
|
|
45
|
+
T::Array[T.proc.params(arg0: String).returns(T.nilable(String))]
|
|
46
|
+
)
|
|
43
47
|
@converter = T.let(options[:convert] || ->(v) { v }, T.proc.params(arg0: String).returns(T.untyped))
|
|
44
48
|
end
|
|
45
49
|
|
|
46
|
-
sig { params(block: T.proc.params(arg0: String).returns(T
|
|
50
|
+
sig { params(block: T.proc.params(arg0: String).returns(T.nilable(String))).returns(T.self_type) }
|
|
47
51
|
def validate(&block)
|
|
48
52
|
@validators << block
|
|
49
53
|
self
|
|
@@ -58,8 +62,20 @@ class Option
|
|
|
58
62
|
sig { params(value: String).returns(T.untyped) }
|
|
59
63
|
def process(value)
|
|
60
64
|
@validators.each do |v|
|
|
61
|
-
|
|
65
|
+
msg = v.call(value)
|
|
66
|
+
next if msg.nil?
|
|
67
|
+
|
|
68
|
+
flags = [@short, @long].reject(&:empty?)
|
|
69
|
+
flags_part =
|
|
70
|
+
if flags.empty?
|
|
71
|
+
"option '#{@name}'"
|
|
72
|
+
else
|
|
73
|
+
"#{flags.join(' or ')} option"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
raise ArgumentError, "#{msg} for #{flags_part}"
|
|
62
77
|
end
|
|
78
|
+
|
|
63
79
|
@converter.call(value)
|
|
64
80
|
end
|
|
65
81
|
|
data/lib/options.rb
CHANGED
|
@@ -51,7 +51,12 @@ class Options
|
|
|
51
51
|
idx = argv.find_index { |arg| arg == opt.short || arg == opt.long }
|
|
52
52
|
|
|
53
53
|
if idx && argv[idx + 1]
|
|
54
|
-
|
|
54
|
+
begin
|
|
55
|
+
@values[opt.name] = opt.process(T.must(argv[idx + 1]))
|
|
56
|
+
rescue ArgumentError => e
|
|
57
|
+
warn "Error: #{e.message}"
|
|
58
|
+
exit 1
|
|
59
|
+
end
|
|
55
60
|
elsif opt.required_flag
|
|
56
61
|
warn "Error: Missing required option: #{opt.long.empty? ? opt.short : opt.long}"
|
|
57
62
|
exit 1
|