optitron 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,8 @@
1
1
  class Optitron
2
2
  class Option
3
- attr_accessor :required, :name, :default, :parameterize, :type, :desc
3
+ attr_accessor :required, :name, :default, :parameterize, :type, :desc, :has_default
4
4
  alias_method :required?, :required
5
+ alias_method :has_default?, :has_default
5
6
  alias_method :parameterize?, :parameterize
6
7
 
7
8
  TRUE_BOOLEAN_VALUES = [true, 't', 'T', 'true', 'TRUE']
@@ -9,6 +10,7 @@ class Optitron
9
10
  BOOLEAN_VALUES = TRUE_BOOLEAN_VALUES + FALSE_BOOLEAN_VALUES
10
11
 
11
12
  def default=(default)
13
+ @has_default = true unless default.nil?
12
14
  interpolate_type(default)
13
15
  @default = default
14
16
  end
@@ -21,6 +23,10 @@ class Optitron
21
23
  :boolean
22
24
  when Numeric
23
25
  :numeric
26
+ when Array
27
+ :array
28
+ when Hash
29
+ :hash
24
30
  end
25
31
  end
26
32
 
@@ -40,7 +46,7 @@ class Optitron
40
46
  Array(val)
41
47
  when :hash
42
48
  val.is_a?(Hash) ? val : raise
43
- when :greedy, nil
49
+ when :greedy, nil, :string
44
50
  val
45
51
  else
46
52
  raise
@@ -55,7 +61,7 @@ class Optitron
55
61
  end
56
62
  @name, @desc = name, desc
57
63
  @type = opts && opts[:type] || :boolean
58
- self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? true : nil)
64
+ self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? false : nil)
59
65
  end
60
66
 
61
67
  def match?(tok)
@@ -73,7 +79,7 @@ class Optitron
73
79
  elsif opt_tok.name == name and tokens[opt_tok_index].respond_to?(:val) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].val)
74
80
  tokens.delete_at(opt_tok_index).val
75
81
  end
76
- response.params_array << [self, value.nil? ? default : value]
82
+ response.params_array << [self, value.nil? ? !default : value]
77
83
  when :numeric
78
84
  value = if opt_tok.name == name and opt_tok.respond_to?(:value)
79
85
  opt_tok.value
@@ -32,6 +32,7 @@ class Optitron
32
32
 
33
33
  def parse_options(tokens, options, response)
34
34
  options.each do |opt|
35
+ response.params[opt.name] = opt.default if opt.has_default?
35
36
  if opt_tok = tokens.find { |tok| opt.match?(tok) }
36
37
  opt_tok_index = tokens.index(opt_tok)
37
38
  opt.consume(response, tokens)
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Optitron::Parser defaults" do
4
+ it "should always send defaults unless you override them" do
5
+ @parser = Optitron.new {
6
+ opt 'ten', :default => 10
7
+ opt 'string', :default => 'string'
8
+ opt 'list', :default => ['one', 'two', 'three']
9
+ opt 'hash', :default => {'hey' => 'you'}
10
+ }
11
+ @parser.parse([]).params.should == {"list"=>["one", "two", "three"], "hash"=>{"hey"=>"you"}, "ten"=>10, "string"=>"string"}
12
+ @parser.parse(%w(--ten=123)).params.should == {"list"=>["one", "two", "three"], "hash"=>{"hey"=>"you"}, "ten"=>123, "string"=>"string"}
13
+ @parser.parse(%w(--list=three two one)).params.should == {"list"=>["three", "two", "one"], "hash"=>{"hey"=>"you"}, "ten"=>10, "string"=>"string"}
14
+ end
15
+ end
@@ -23,13 +23,13 @@ describe "Optitron::Parser short_name generation" do
23
23
 
24
24
  it "should parse '-s'" do
25
25
  response = @parser.parse(%w(-s))
26
- response.params.should == {'something' => true}
26
+ response.params.should == {'something' => true, 'something-else' => false}
27
27
  response.valid?.should be_true
28
28
  end
29
29
 
30
30
  it "should parse '-S'" do
31
31
  response = @parser.parse(%w(-S))
32
- response.params.should == {'something-else' => true}
32
+ response.params.should == {'something' => false, 'something-else' => true}
33
33
  response.valid?.should be_true
34
34
  end
35
35
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optitron
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -59,6 +59,7 @@ files:
59
59
  - lib/optitron/version.rb
60
60
  - optitron.gemspec
61
61
  - spec/arg_spec.rb
62
+ - spec/default_spec.rb
62
63
  - spec/dispatch_spec.rb
63
64
  - spec/errors_spec.rb
64
65
  - spec/other_spec.rb