simple-cli-options 0.1.1 → 0.1.3
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 +35 -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: b3dc23fc107b31257b80f1152cea570f9908946c7374c015637d735c28609779
|
|
4
|
+
data.tar.gz: 13bb26bad01ba80f643b0d64fae26a19a7d6c6852ef034274809031c626b00a9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 98c31149080eef46edf5109578b988717fdd1e4e28a637f3b574e3ddc7af9203b44f99ea6c010d49ca012d2f0f58b1f8b7f37136212688a82222a5a8bd2a7416
|
|
7
|
+
data.tar.gz: 1aea1b272cb5e6aac5bb004222f187030b09eb66cfaa4dd358cc5d60e63173e6e34b064f4e1a5bb808e43a1cc7f15089d9af56969a8537e814f53fc69020f4dd
|
data/lib/option.rb
CHANGED
|
@@ -39,11 +39,31 @@ class Option
|
|
|
39
39
|
validate_structure!
|
|
40
40
|
|
|
41
41
|
# バリデーションは追加(Array)可能、変換は上書き
|
|
42
|
-
|
|
42
|
+
# 各バリデータは、成功時に nil、失敗時にエラーメッセージ(String)を返す想定
|
|
43
|
+
raw_validators = options[:validate]
|
|
44
|
+
validators = T.let(
|
|
45
|
+
[],
|
|
46
|
+
T::Array[T.proc.params(arg0: String).returns(T.nilable(String))]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
unless raw_validators.nil?
|
|
50
|
+
case raw_validators
|
|
51
|
+
when Proc
|
|
52
|
+
validators << raw_validators
|
|
53
|
+
when Array
|
|
54
|
+
raw_validators.each do |v|
|
|
55
|
+
validators << v
|
|
56
|
+
end
|
|
57
|
+
else
|
|
58
|
+
raise ArgumentError, "validate must be a Proc or an Array of Procs"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
@validators = validators
|
|
43
63
|
@converter = T.let(options[:convert] || ->(v) { v }, T.proc.params(arg0: String).returns(T.untyped))
|
|
44
64
|
end
|
|
45
65
|
|
|
46
|
-
sig { params(block: T.proc.params(arg0: String).returns(T
|
|
66
|
+
sig { params(block: T.proc.params(arg0: String).returns(T.nilable(String))).returns(T.self_type) }
|
|
47
67
|
def validate(&block)
|
|
48
68
|
@validators << block
|
|
49
69
|
self
|
|
@@ -58,8 +78,20 @@ class Option
|
|
|
58
78
|
sig { params(value: String).returns(T.untyped) }
|
|
59
79
|
def process(value)
|
|
60
80
|
@validators.each do |v|
|
|
61
|
-
|
|
81
|
+
msg = v.call(value)
|
|
82
|
+
next if msg.nil?
|
|
83
|
+
|
|
84
|
+
flags = [@short, @long].reject(&:empty?)
|
|
85
|
+
flags_part =
|
|
86
|
+
if flags.empty?
|
|
87
|
+
"option '#{@name}'"
|
|
88
|
+
else
|
|
89
|
+
"#{flags.join(' or ')} option"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
raise ArgumentError, "#{msg} for #{flags_part}"
|
|
62
93
|
end
|
|
94
|
+
|
|
63
95
|
@converter.call(value)
|
|
64
96
|
end
|
|
65
97
|
|
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
|