optitron 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- optitron (0.1.8)
4
+ optitron (0.1.9)
5
5
  callsite (~> 0.0.4)
6
6
  ruby2ruby (~> 1.2.4)
7
7
  ruby_parser (~> 2.0)
@@ -168,6 +168,8 @@ This allows you to force an option to be required. False by default.
168
168
 
169
169
  This allows you to specify the type. Acceptable options are <tt>:numeric</tt>, <tt>:array</tt>, <tt>:hash</tt>, <tt>:string</tt> or <tt>:boolean</tt>.
170
170
 
171
+ With boolean type, you can additionally supply <tt>:use_no => true</tt> to be able to use --no-option style options.
172
+
171
173
  == Stand alone usage
172
174
 
173
175
  You can create parsers and parse using them.
@@ -84,7 +84,7 @@ class Optitron
84
84
 
85
85
  def help(desc = "Print help message")
86
86
  configure_with {
87
- opt 'help', desc, :short_name => '?', :run => proc{ |value, response|
87
+ opt 'help', desc, :short_name => '?', :suppress_no => true, :run => proc{ |value, response|
88
88
  if value
89
89
  puts @target.help
90
90
  exit(0)
@@ -19,7 +19,7 @@ class Optitron
19
19
 
20
20
  def help_line_for_opt(opt)
21
21
  opt_line = ''
22
- opt_line << [opt.short_name ? "-#{opt.short_name}" : nil, "--#{opt.name}"].compact.join('/')
22
+ opt_line << [opt.short_name ? "-#{opt.short_name}" : nil, opt.boolean? && opt.use_no ? "--(no-)#{opt.name}" : "--#{opt.name}"].compact.join('/')
23
23
  opt_line << "=[#{help_line_for_opt_value(opt)}]" unless opt.boolean?
24
24
  [opt_line, opt.desc]
25
25
  end
@@ -102,7 +102,7 @@ class Optitron
102
102
  end
103
103
 
104
104
  class Opt < Option
105
- attr_accessor :short_name, :run, :parent_cmd, :include_in_params
105
+ attr_accessor :short_name, :run, :parent_cmd, :include_in_params, :use_no
106
106
  alias_method :include_in_params?, :include_in_params
107
107
  def initialize(name, desc = nil, opts = nil)
108
108
  if desc.is_a?(Hash)
@@ -115,15 +115,16 @@ class Optitron
115
115
  self.inclusion_test = opts[:in] if opts && opts[:in]
116
116
  self.required = opts && opts.key?(:required) ? opts[:required] : false
117
117
  self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? false : nil)
118
+ self.use_no = opts && opts.key?(:use_no) ? opts[:use_no] : false
118
119
  end
119
120
 
120
121
  def match?(tok)
121
- tok.respond_to?(:name) and [name, short_name].include?(tok.name)
122
+ tok.respond_to?(:name) && (!use_no ? [name, short_name] : [name, short_name, "no-#{name}"]).include?(tok.name)
122
123
  end
123
124
 
124
125
  def find_matching_token(tokens)
125
126
  tokens.find do |t|
126
- if t.respond_to?(:name) and (t.name == name or t.name == short_name)
127
+ if t.respond_to?(:name) and (t.name == name or t.name == short_name or t.name == "no-#{name}")
127
128
  t.respond_to?(:value) ^ (t.name == short_name)
128
129
  end
129
130
  end
@@ -135,7 +136,9 @@ class Optitron
135
136
  tokens.delete_at(opt_tok_index)
136
137
  case @type
137
138
  when :boolean
138
- value = if opt_tok.respond_to?(:value)
139
+ value = if opt_tok.name == "no-#{name}"
140
+ default
141
+ elsif opt_tok.respond_to?(:value)
139
142
  opt_tok.value
140
143
  elsif opt_tok.name == short_name and tokens[opt_tok_index].respond_to?(:lit) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].lit)
141
144
  tokens.delete_at(opt_tok_index).lit
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "Optitron::Parser help" do
4
4
  it "generate help for command parsers" do
5
5
  @parser = Optitron.new {
6
- opt 'verbose', "Be very loud"
6
+ opt 'verbose', "Be very loud", :use_no => true
7
7
  cmd "install", "This installs things" do
8
8
  arg "file", "The file to install"
9
9
  end
@@ -20,7 +20,7 @@ describe "Optitron::Parser help" do
20
20
  arg "thing", "Stuff to join", :type => :greedy, :required => true
21
21
  end
22
22
  }
23
- @parser.help.should == "Commands\n\ninstall [file] # This installs things\n # file -- The file to install\nshow [first] <second> # This shows things\n # first -- The first thing to show\n # second -- The second optional thing to show\nkill # This kills things\n -p/--pids=[ARRAY] # A list of pids to kill\n -P/--pid=[NUMERIC] # A pid to kill\n -n/--names=[HASH] # Some sort of hash\njoin [thing1 thing2 ...] # This joins things\n # thing -- Stuff to join\n\nGlobal options\n\n-v/--verbose # Be very loud"
23
+ @parser.help.should == "Commands\n\ninstall [file] # This installs things\n # file -- The file to install\nshow [first] <second> # This shows things\n # first -- The first thing to show\n # second -- The second optional thing to show\nkill # This kills things\n -p/--pids=[ARRAY] # A list of pids to kill\n -P/--pid=[NUMERIC] # A pid to kill\n -n/--names=[HASH] # Some sort of hash\njoin [thing1 thing2 ...] # This joins things\n # thing -- Stuff to join\n\nGlobal options\n\n-v/--(no-)verbose # Be very loud"
24
24
  end
25
25
 
26
26
  it "generate help for non-command parsers" do
@@ -28,7 +28,7 @@ describe "Optitron::Parser options" do
28
28
  context "boolean long names vs short names" do
29
29
  before(:each) do
30
30
  @parser = Optitron.new {
31
- opt "verbose"
31
+ opt "verbose", :use_no => true
32
32
  }
33
33
  end
34
34
 
@@ -36,6 +36,11 @@ describe "Optitron::Parser options" do
36
36
  @parser.parse(%w(-v true)).valid?.should be_true
37
37
  end
38
38
 
39
+ it "should parse '--no-verbose'" do
40
+ @parser.parse(%w(--no-verbose)).valid?.should be_true
41
+ @parser.parse(%w(--no-verbose)).params['verbose'].should be_false
42
+ end
43
+
39
44
  it "shouldn't parse '-v that'" do
40
45
  @parser.parse(%w(-v that)).valid?.should be_false
41
46
  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: 9
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 9
10
- version: 0.1.9
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
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-10-02 00:00:00 -07:00
18
+ date: 2010-10-03 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency