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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/option.rb +35 -3
  3. data/lib/options.rb +6 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb1ba82fcfd104b803bec7b4624c75416dc81322d33ee62732d19553a767b352
4
- data.tar.gz: dafc46da869982bc190036b552b63a74feb11523621c94c2408ecb7c1794cbb9
3
+ metadata.gz: b3dc23fc107b31257b80f1152cea570f9908946c7374c015637d735c28609779
4
+ data.tar.gz: 13bb26bad01ba80f643b0d64fae26a19a7d6c6852ef034274809031c626b00a9
5
5
  SHA512:
6
- metadata.gz: d7c549b1382a35a346f44da97ab71d0be5d3055c1ec140125a0e88fff4574e45e0cb473d342f9dcd5fd66b7542c4b21d2716f06e09e21427c69ed902261a428e
7
- data.tar.gz: a5cf0f1a11646bdc84fd73ca02c6779375852cb62086501e7a485d677f0a4cf70a80dba797192c1cfcec805a9c1e3a7ef3bd1673dcfd54696a2d8892944ee8d1
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
- @validators = T.let([options[:validate]].compact, T::Array[T.proc.params(arg0: String).returns(T::Boolean)])
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::Boolean)).returns(T.self_type) }
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
- raise "Validation failed for option '#{@name}' with value: #{value}" unless v.call(value)
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
- @values[opt.name] = opt.process(T.must(argv[idx + 1]))
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-cli-options
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - HIROAKI SATOU