optitron 0.1.2 → 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.
- data/Gemfile.lock +1 -1
 - data/README.rdoc +7 -0
 - data/lib/optitron/class_dsl.rb +6 -3
 - data/lib/optitron/option.rb +13 -3
 - data/lib/optitron/version.rb +1 -1
 - data/spec/cli_spec.rb +13 -2
 - data/spec/validation_spec.rb +64 -0
 - metadata +4 -4
 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.rdoc
    CHANGED
    
    | 
         @@ -102,6 +102,13 @@ Will produce the error <tt>Truth is invalid</tt>. Calling with 
     | 
|
| 
       102 
102 
     | 
    
         | 
| 
       103 
103 
     | 
    
         
             
            however, will correctly invoke the method.
         
     | 
| 
       104 
104 
     | 
    
         | 
| 
      
 105 
     | 
    
         
            +
            These same arg type hints can be provided by ending your arguments in _type. The accepted types are: float, string, int, numeric, array, hash. For example
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
              desc "Does something awesome"
         
     | 
| 
      
 108 
     | 
    
         
            +
              def start(number_int, something_string, opt_boolean)
         
     | 
| 
      
 109 
     | 
    
         
            +
                # ... does something
         
     | 
| 
      
 110 
     | 
    
         
            +
              end
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
       105 
112 
     | 
    
         | 
| 
       106 
113 
     | 
    
         
             
            === Options
         
     | 
| 
       107 
114 
     | 
    
         | 
    
        data/lib/optitron/class_dsl.rb
    CHANGED
    
    | 
         @@ -138,13 +138,16 @@ class Optitron 
     | 
|
| 
       138 
138 
     | 
    
         
             
                        optitron_dsl.root.cmd(cmd_name, cmd_desc) do
         
     | 
| 
       139 
139 
     | 
    
         
             
                          opts.each { |o| opt *o }
         
     | 
| 
       140 
140 
     | 
    
         
             
                          args.each do |(arg_name, arg_type, arg_default)|
         
     | 
| 
      
 141 
     | 
    
         
            +
                            possible_arg_type = arg_name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
         
     | 
| 
      
 142 
     | 
    
         
            +
                            possible_arg_type &&= possible_arg_type.to_sym
         
     | 
| 
      
 143 
     | 
    
         
            +
                            arg_opts = { :default => arg_default && eval(arg_default), :type => arg_types && arg_types.shift || possible_arg_type }
         
     | 
| 
       141 
144 
     | 
    
         
             
                            case arg_type
         
     | 
| 
       142 
145 
     | 
    
         
             
                            when :required
         
     | 
| 
       143 
     | 
    
         
            -
                              arg arg_name.to_s,  
     | 
| 
      
 146 
     | 
    
         
            +
                              arg arg_name.to_s, arg_opts
         
     | 
| 
       144 
147 
     | 
    
         
             
                            when :optional
         
     | 
| 
       145 
     | 
    
         
            -
                              arg arg_name.to_s,  
     | 
| 
      
 148 
     | 
    
         
            +
                              arg arg_name.to_s, arg_opts.merge(:required => false)
         
     | 
| 
       146 
149 
     | 
    
         
             
                            when :greedy
         
     | 
| 
       147 
     | 
    
         
            -
                              arg arg_name.to_s,  
     | 
| 
      
 150 
     | 
    
         
            +
                              arg arg_name.to_s, arg_opts.merge(:type => :greedy)
         
     | 
| 
       148 
151 
     | 
    
         
             
                            end
         
     | 
| 
       149 
152 
     | 
    
         
             
                          end
         
     | 
| 
       150 
153 
     | 
    
         
             
                        end
         
     | 
    
        data/lib/optitron/option.rb
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class Optitron
         
     | 
| 
       2 
2 
     | 
    
         
             
              class Option
         
     | 
| 
       3 
     | 
    
         
            -
                attr_reader :inclusion_test
         
     | 
| 
       4 
     | 
    
         
            -
                attr_accessor :required, :name, :default, :parameterize, : 
     | 
| 
      
 3 
     | 
    
         
            +
                attr_reader :inclusion_test, :type
         
     | 
| 
      
 4 
     | 
    
         
            +
                attr_accessor :required, :name, :default, :parameterize, :desc, :has_default
         
     | 
| 
       5 
5 
     | 
    
         
             
                alias_method :required?, :required
         
     | 
| 
       6 
6 
     | 
    
         
             
                alias_method :has_default?, :has_default
         
     | 
| 
       7 
7 
     | 
    
         
             
                alias_method :parameterize?, :parameterize
         
     | 
| 
         @@ -21,6 +21,14 @@ class Optitron 
     | 
|
| 
       21 
21 
     | 
    
         
             
                  @inclusion_test = tester
         
     | 
| 
       22 
22 
     | 
    
         
             
                end
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
      
 24 
     | 
    
         
            +
                def type=(type)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  @type = case type
         
     | 
| 
      
 26 
     | 
    
         
            +
                  when :int then :numeric
         
     | 
| 
      
 27 
     | 
    
         
            +
                  when :float, :numeric, :string, :array, :hash, :boolean, :greedy, nil then type
         
     | 
| 
      
 28 
     | 
    
         
            +
                  else raise("Unknown type #{type.inspect}")
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
                
         
     | 
| 
       24 
32 
     | 
    
         
             
                def interpolate_type(default)
         
     | 
| 
       25 
33 
     | 
    
         
             
                  @type = case default
         
     | 
| 
       26 
34 
     | 
    
         
             
                  when nil
         
     | 
| 
         @@ -78,6 +86,8 @@ class Optitron 
     | 
|
| 
       78 
86 
     | 
    
         
             
                    Integer(val)
         
     | 
| 
       79 
87 
     | 
    
         
             
                  when :array
         
     | 
| 
       80 
88 
     | 
    
         
             
                    Array(val)
         
     | 
| 
      
 89 
     | 
    
         
            +
                  when :float
         
     | 
| 
      
 90 
     | 
    
         
            +
                    Float(val)
         
     | 
| 
       81 
91 
     | 
    
         
             
                  when :hash
         
     | 
| 
       82 
92 
     | 
    
         
             
                    val.is_a?(Hash) ? val : raise
         
     | 
| 
       83 
93 
     | 
    
         
             
                  when :greedy, nil, :string
         
     | 
| 
         @@ -129,7 +139,7 @@ class Optitron 
     | 
|
| 
       129 
139 
     | 
    
         
             
                          tokens.delete_at(opt_tok_index).lit
         
     | 
| 
       130 
140 
     | 
    
         
             
                        end
         
     | 
| 
       131 
141 
     | 
    
         
             
                        response.params_array << [self, value.nil? ? !default : value]
         
     | 
| 
       132 
     | 
    
         
            -
                      when :numeric, :string
         
     | 
| 
      
 142 
     | 
    
         
            +
                      when :numeric, :string, :float
         
     | 
| 
       133 
143 
     | 
    
         
             
                        value = if opt_tok.name == name
         
     | 
| 
       134 
144 
     | 
    
         
             
                          if opt_tok.respond_to?(:value)
         
     | 
| 
       135 
145 
     | 
    
         
             
                            opt_tok.value
         
     | 
    
        data/lib/optitron/version.rb
    CHANGED
    
    
    
        data/spec/cli_spec.rb
    CHANGED
    
    | 
         @@ -17,7 +17,7 @@ class CLIExample < Optitron::CLI 
     | 
|
| 
       17 
17 
     | 
    
         
             
              opt 'another_opt'
         
     | 
| 
       18 
18 
     | 
    
         
             
              arg_types :hash
         
     | 
| 
       19 
19 
     | 
    
         
             
              def use_too(one, two = 'three')
         
     | 
| 
       20 
     | 
    
         
            -
                puts "one: #{one.inspect} #{two.inspect}"
         
     | 
| 
      
 20 
     | 
    
         
            +
                puts "one: #{one.to_a.sort.inspect} #{two.inspect}"
         
     | 
| 
       21 
21 
     | 
    
         
             
              end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
              desc "Use this three"
         
     | 
| 
         @@ -58,6 +58,13 @@ class NoHelpExample < Optitron::CLI 
     | 
|
| 
       58 
58 
     | 
    
         
             
              end
         
     | 
| 
       59 
59 
     | 
    
         
             
            end
         
     | 
| 
       60 
60 
     | 
    
         | 
| 
      
 61 
     | 
    
         
            +
            class CLIExampleWithArgHinting < Optitron::CLI
         
     | 
| 
      
 62 
     | 
    
         
            +
              desc "Use this too"
         
     | 
| 
      
 63 
     | 
    
         
            +
              def use_too(one_string, two_int)
         
     | 
| 
      
 64 
     | 
    
         
            +
                puts "using this too #{one_string.inspect} #{two_int.inspect}"
         
     | 
| 
      
 65 
     | 
    
         
            +
              end
         
     | 
| 
      
 66 
     | 
    
         
            +
            end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
       61 
68 
     | 
    
         | 
| 
       62 
69 
     | 
    
         
             
            describe "Optitron::Parser defaults" do
         
     | 
| 
       63 
70 
     | 
    
         
             
              it "should generate the correct help" do
         
     | 
| 
         @@ -70,7 +77,7 @@ describe "Optitron::Parser defaults" do 
     | 
|
| 
       70 
77 
     | 
    
         
             
              end
         
     | 
| 
       71 
78 
     | 
    
         | 
| 
       72 
79 
     | 
    
         
             
              it "should dispatch with the type hinting" do
         
     | 
| 
       73 
     | 
    
         
            -
                capture(:stdout) { CLIExample.dispatch(%w(use_too one:two three:four))}.should ==  
     | 
| 
      
 80 
     | 
    
         
            +
                capture(:stdout) { CLIExample.dispatch(%w(use_too one:two three:four))}.should == 'one: [["one", "two"], ["three", "four"]] "three"' + "\n"
         
     | 
| 
       74 
81 
     | 
    
         
             
              end
         
     | 
| 
       75 
82 
     | 
    
         | 
| 
       76 
83 
     | 
    
         
             
              it "should generate the correct help" do
         
     | 
| 
         @@ -85,4 +92,8 @@ describe "Optitron::Parser defaults" do 
     | 
|
| 
       85 
92 
     | 
    
         
             
              it "should be able to suppress help" do
         
     | 
| 
       86 
93 
     | 
    
         
             
                capture(:stdout) { NoHelpExample.dispatch(%w(--help)) }.should == "Unknown command\nHelp is unrecognized\n"
         
     | 
| 
       87 
94 
     | 
    
         
             
              end
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
              it "should get type hinting from arg names" do
         
     | 
| 
      
 97 
     | 
    
         
            +
                capture(:stdout) { CLIExampleWithArgHinting.dispatch(%w(use_too asd 123)) }.should == "using this too \"asd\" 123\n"
         
     | 
| 
      
 98 
     | 
    
         
            +
              end
         
     | 
| 
       88 
99 
     | 
    
         
             
            end
         
     | 
    
        data/spec/validation_spec.rb
    CHANGED
    
    | 
         @@ -51,6 +51,70 @@ describe "Optitron::Parser types" do 
     | 
|
| 
       51 
51 
     | 
    
         
             
                end
         
     | 
| 
       52 
52 
     | 
    
         
             
              end
         
     | 
| 
       53 
53 
     | 
    
         | 
| 
      
 54 
     | 
    
         
            +
              context "float" do
         
     | 
| 
      
 55 
     | 
    
         
            +
                context "on args" do
         
     | 
| 
      
 56 
     | 
    
         
            +
                  before(:each) {
         
     | 
| 
      
 57 
     | 
    
         
            +
                    @parser = Optitron.new {
         
     | 
| 
      
 58 
     | 
    
         
            +
                      cmd "kill" do
         
     | 
| 
      
 59 
     | 
    
         
            +
                        arg 'volumne', :type => :float
         
     | 
| 
      
 60 
     | 
    
         
            +
                      end
         
     | 
| 
      
 61 
     | 
    
         
            +
                    }
         
     | 
| 
      
 62 
     | 
    
         
            +
                  }
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                  it "should parse 'kill 123'" do
         
     | 
| 
      
 65 
     | 
    
         
            +
                    response = @parser.parse(%w(kill 123))
         
     | 
| 
      
 66 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 67 
     | 
    
         
            +
                    response.args.first.should == 123
         
     | 
| 
      
 68 
     | 
    
         
            +
                    response.valid?.should be_true
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                  it "should parse 'kill 123.345'" do
         
     | 
| 
      
 72 
     | 
    
         
            +
                    response = @parser.parse(%w(kill 123.345))
         
     | 
| 
      
 73 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 74 
     | 
    
         
            +
                    response.args.first.should == 123.345
         
     | 
| 
      
 75 
     | 
    
         
            +
                    response.valid?.should be_true
         
     | 
| 
      
 76 
     | 
    
         
            +
                  end
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                  it "shouldn't parse 'kill asd'" do
         
     | 
| 
      
 79 
     | 
    
         
            +
                    response = @parser.parse(%w(kill asd))
         
     | 
| 
      
 80 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 81 
     | 
    
         
            +
                    response.args.first.should == 'asd'
         
     | 
| 
      
 82 
     | 
    
         
            +
                    response.valid?.should be_false
         
     | 
| 
      
 83 
     | 
    
         
            +
                  end
         
     | 
| 
      
 84 
     | 
    
         
            +
                end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                context "on opts" do
         
     | 
| 
      
 87 
     | 
    
         
            +
                  before(:each) do
         
     | 
| 
      
 88 
     | 
    
         
            +
                    @parser = Optitron.new do
         
     | 
| 
      
 89 
     | 
    
         
            +
                      cmd "kill" do
         
     | 
| 
      
 90 
     | 
    
         
            +
                        opt 'pid', :type => :float
         
     | 
| 
      
 91 
     | 
    
         
            +
                      end
         
     | 
| 
      
 92 
     | 
    
         
            +
                    end
         
     | 
| 
      
 93 
     | 
    
         
            +
                  end
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
                  it "should parse 'kill --pid=123'" do
         
     | 
| 
      
 96 
     | 
    
         
            +
                    response = @parser.parse(%w(kill --pid=123))
         
     | 
| 
      
 97 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 98 
     | 
    
         
            +
                    response.params['pid'].should == 123
         
     | 
| 
      
 99 
     | 
    
         
            +
                    response.valid?.should be_true
         
     | 
| 
      
 100 
     | 
    
         
            +
                  end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                  it "should parse 'kill --pid=123.345'" do
         
     | 
| 
      
 103 
     | 
    
         
            +
                    response = @parser.parse(%w(kill --pid=123.345))
         
     | 
| 
      
 104 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 105 
     | 
    
         
            +
                    response.params['pid'].should == 123.345
         
     | 
| 
      
 106 
     | 
    
         
            +
                    response.valid?.should be_true
         
     | 
| 
      
 107 
     | 
    
         
            +
                  end
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                  it "shouldn't parse 'kill --pid=asd'" do
         
     | 
| 
      
 110 
     | 
    
         
            +
                    response = @parser.parse(%w(kill --pid=asd))
         
     | 
| 
      
 111 
     | 
    
         
            +
                    response.command.should == 'kill'
         
     | 
| 
      
 112 
     | 
    
         
            +
                    response.params['pid'].should == 'asd'
         
     | 
| 
      
 113 
     | 
    
         
            +
                    response.valid?.should be_false
         
     | 
| 
      
 114 
     | 
    
         
            +
                  end
         
     | 
| 
      
 115 
     | 
    
         
            +
                end
         
     | 
| 
      
 116 
     | 
    
         
            +
              end
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
       54 
118 
     | 
    
         
             
              context "array" do
         
     | 
| 
       55 
119 
     | 
    
         
             
                context "on opt" do
         
     | 
| 
       56 
120 
     | 
    
         
             
                  before(:each) {
         
     | 
    
        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:  
     | 
| 
      
 4 
     | 
    
         
            +
              hash: 29
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       6 
6 
     | 
    
         
             
              segments: 
         
     | 
| 
       7 
7 
     | 
    
         
             
              - 0
         
     | 
| 
       8 
8 
     | 
    
         
             
              - 1
         
     | 
| 
       9 
     | 
    
         
            -
              -  
     | 
| 
       10 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 9 
     | 
    
         
            +
              - 3
         
     | 
| 
      
 10 
     | 
    
         
            +
              version: 0.1.3
         
     | 
| 
       11 
11 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       12 
12 
     | 
    
         
             
            authors: 
         
     | 
| 
       13 
13 
     | 
    
         
             
            - Joshua Hull
         
     | 
| 
         @@ -15,7 +15,7 @@ autorequire: 
     | 
|
| 
       15 
15 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       16 
16 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
            date: 2010-08- 
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2010-08-23 00:00:00 -07:00
         
     | 
| 
       19 
19 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       21 
21 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     |