samovar 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df07fe3139c96d3f34b9e4d1ae5416e1f311eb2f
4
- data.tar.gz: e2ef640c6126fa551258c29bafca19bdeb92ff0c
3
+ metadata.gz: 0bd4d6e27c6922df8e1c114c97ebb3960df04473
4
+ data.tar.gz: 311072fee7aa41a8bb68cc36de8f7b3497c3c331
5
5
  SHA512:
6
- metadata.gz: fbb6dea46c1245ae3e471f67ae62b75d5ce20937dba2c06fd82671e6ebad78f8fbe136816bbc7afc902f18d9aedbb98a9547a13792e167d5e791b6bfd36c12e9
7
- data.tar.gz: 4ff9e33ab4b1cdc1969c547e8ba56733e9b406876409568d1ced9ee8b54d70ade58e456e66064f6055a8f6b0b818157308924de9060d83b1a807ed7d6d126f0e
6
+ metadata.gz: 49ca3a2ccefff661c04eede9d56c0904fa323987319093c321c419254f105e799698701bf699b0fee8c5bb73d9a414559def3c4ff2567397482066a16ac15464
7
+ data.tar.gz: dd069406b3e275b2233f237961c9e46b287329d302fc23ed9849f069acd77a051cdd141c41a17622be9f4bc9ba2ef7b3c5e23a496049f4da0bd08c7c5cc74e7f
data/README.md CHANGED
@@ -45,6 +45,9 @@ Or install it yourself as:
45
45
  option '-f/--frobulate <text>', "Frobulate the text"
46
46
  option '-x | -y', "Specify either x or y axis.", key: :axis
47
47
  option '-F/--yeah/--flag', "A boolean flag with several forms."
48
+ option '--things <a,b,c>', "A list of things" do |value|
49
+ value.split(/\s*,\s*/)
50
+ end
48
51
  end
49
52
  end
50
53
 
@@ -56,6 +59,9 @@ Or install it yourself as:
56
59
 
57
60
  application = Application.new(['-F'])
58
61
  application.options[:flag] # true
62
+
63
+ application = Application.new(['--things', 'x,y,z'])
64
+ application.options[:things] # ['x', 'y', 'z']
59
65
 
60
66
  ### Nested Commands
61
67
 
@@ -22,7 +22,7 @@ require_relative 'flags'
22
22
 
23
23
  module Samovar
24
24
  class Option
25
- def initialize(flags, description, key: nil, default: nil, value: nil)
25
+ def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, &block)
26
26
  @flags = Flags.new(flags)
27
27
  @description = description
28
28
 
@@ -36,6 +36,8 @@ module Samovar
36
36
 
37
37
  @value = value
38
38
  @value ||= true if @flags.boolean?
39
+
40
+ @type = block_given? ? block : type
39
41
  end
40
42
 
41
43
  attr :flags
@@ -46,9 +48,17 @@ module Samovar
46
48
 
47
49
  attr :key
48
50
 
51
+ def coerce(result)
52
+ if @type
53
+ @type.call(result)
54
+ else
55
+ result
56
+ end
57
+ end
58
+
49
59
  def parse(input)
50
60
  if result = @flags.parse(input)
51
- @value.nil? ? result : @value
61
+ @value.nil? ? coerce(result) : @value
52
62
  else
53
63
  @default
54
64
  end
@@ -91,8 +101,8 @@ module Samovar
91
101
  attr :key
92
102
  attr :defaults
93
103
 
94
- def option(*args, **options)
95
- self << Option.new(*args, **options)
104
+ def option(*args, **options, &block)
105
+ self << Option.new(*args, **options, &block)
96
106
  end
97
107
 
98
108
  def << option
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Samovar
22
- VERSION = "1.1.1"
22
+ VERSION = "1.2.0"
23
23
  end
@@ -0,0 +1,20 @@
1
+
2
+ require 'samovar'
3
+ require 'stringio'
4
+
5
+ module Command
6
+ class Coerce < Samovar::Command
7
+ options do
8
+ option '--things <array>', "A list of things" do |input|
9
+ input.split(/\s*,\s*/)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ describe Samovar::Command do
16
+ it "should use default value" do
17
+ top = Command::Coerce.parse(['--things', 'a,b,c'])
18
+ expect(top.options[:things]).to be == ['a', 'b', 'c']
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samovar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-20 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mapping
@@ -110,6 +110,7 @@ files:
110
110
  - lib/samovar/table.rb
111
111
  - lib/samovar/version.rb
112
112
  - spec/samovar/command_spec.rb
113
+ - spec/samovar/type_spec.rb
113
114
  - teapot.png
114
115
  homepage: https://github.com/ioquatix/samovar
115
116
  licenses:
@@ -138,3 +139,4 @@ summary: Samovar is a flexible option parser excellent support for sub-commands
138
139
  help documentation.
139
140
  test_files:
140
141
  - spec/samovar/command_spec.rb
142
+ - spec/samovar/type_spec.rb