micro-optparse 0.8.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task :default => :spec
@@ -1,19 +1 @@
1
- require 'micro-optparse/parser'
2
- #
3
- # options = Parser.new do |p|
4
- # # p.banner = "test"
5
- # p.version = "OptParseWrapper 0.8 (c) Florian Pilz 2011"
6
- # p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
7
- # p.option :verbose, "enable verbose output"
8
- # p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
9
- # p.option :plus_selection, "use plus-selection if set", :default => true
10
- # p.option :selection, "selection used", :default => "BestSelection"#, :short => "l"
11
- # p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
12
- # end.process!
13
- #
14
- # puts options[:severity]
15
- # puts options[:verbose]
16
- # puts options[:mutation]
17
- # puts options[:plus_selection]
18
- # puts options[:selection]
19
- # puts options[:chance]
1
+ require 'micro-optparse/parser'
@@ -16,23 +16,23 @@ class Parser
16
16
 
17
17
  def short_from(name)
18
18
  name.to_s.chars.each do |c|
19
- next if @used_short.include?(c)
19
+ next if @used_short.include?(c) || c == "_"
20
20
  return c # returns from short_from method
21
21
  end
22
22
  end
23
23
 
24
24
  def validate(options) # remove this method if you want fewer lines of code and don't need validations
25
25
  options.each_pair do |key, value|
26
- opt = nil
27
- @options.each { |o| opt = o if o[0] == key }
26
+ opt = @options.find_all{ |o| o[0] == key }.first
27
+ key = "--" << key.to_s.gsub("_", "-")
28
28
  unless opt[2][:value_in_set].nil? || opt[2][:value_in_set].include?(value)
29
- puts "Parameter for " << key.to_s << " must be in [" << opt[2][:value_in_set].join(",") << "]" ; exit(1)
29
+ puts "Parameter for #{key} must be in [" << opt[2][:value_in_set].join(",") << "]" ; exit(1)
30
30
  end
31
31
  unless opt[2][:value_matches].nil? || opt[2][:value_matches] =~ value
32
- puts "Parameter must match /" << opt[2][:value_matches].source << "/" ; exit(1)
32
+ puts "Parameter for #{key} must match /" << opt[2][:value_matches].source << "/" ; exit(1)
33
33
  end
34
34
  unless opt[2][:value_satisfies].nil? || opt[2][:value_satisfies].call(value)
35
- puts "Parameter must satisfy given conditions (see description)" ; exit(1)
35
+ puts "Parameter for #{key} must satisfy given conditions (see description)" ; exit(1)
36
36
  end
37
37
  end
38
38
  end
@@ -45,7 +45,7 @@ class Parser
45
45
  options[o[0]] = o[2][:default] || false # set default
46
46
  klass = o[2][:default].class == Fixnum ? Integer : o[2][:default].class
47
47
 
48
- if klass == TrueClass || klass == FalseClass || klass == NilClass # boolean switch
48
+ if [TrueClass, FalseClass, NilClass].include?(klass) # boolean switch
49
49
  p.on("-" << short, "--[no-]" << o[0].to_s.gsub("_", "-"), o[1]) {|x| options[o[0]] = x}
50
50
  else # argument with parameter
51
51
  p.on("-" << short, "--" << o[0].to_s.gsub("_", "-") << " " << o[2][:default].to_s, klass, o[1]) {|x| options[o[0]] = x}
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Optparse
3
- VERSION = "0.8.2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -10,12 +10,14 @@ Gem::Specification.new do |s|
10
10
  s.email = ["fpilz87@googlemail.com"]
11
11
  s.homepage = "http://florianpilz.github.com/micro-optparse/"
12
12
  s.summary = %q{A very small wrapper around optparse.}
13
- s.description = %q{This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have an command-line parser without injecting a gem dependency.}
14
-
15
- s.license = "MIT"
13
+ s.description = %q{This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have a command-line parser without injecting a gem dependency.}
16
14
 
17
15
  s.files = `git ls-files`.split("\n")
18
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
18
  s.require_paths = ["lib"]
19
+
20
+ s.license = "MIT"
21
+ s.has_rdoc = false
22
+ s.add_development_dependency("rspec")
21
23
  end
@@ -0,0 +1,152 @@
1
+ require "micro-optparse"
2
+
3
+ describe Parser do
4
+ describe "help message" do
5
+ it "should show help message when called with --help or -h" do
6
+ results = [`ruby spec/programs/example.rb -h`, `ruby spec/programs/example.rb --help`]
7
+ results.each do |result|
8
+ result.should include("--help")
9
+ result.should include("Show this message")
10
+ end
11
+ end
12
+ end
13
+
14
+ describe "banner message" do
15
+ it "should include the banner info in the help message" do
16
+ result = `ruby spec/programs/example.rb --help`
17
+ result.should include("This is a banner")
18
+ end
19
+
20
+ it "should include the default banner info if no banner message was set" do
21
+ result = `ruby spec/programs/empty_example.rb --help`
22
+ result.should include("Usage: empty_example [options]")
23
+ end
24
+ end
25
+
26
+ describe "version information" do
27
+ it "should display the version when called with --version or -V" do
28
+ # here -V is used for version, as -v is already taken for the verbose switch
29
+ results = [`ruby spec/programs/example.rb -V`, `ruby spec/programs/example.rb --version`]
30
+ results.each do |result|
31
+ result.strip.should == "OptParseWrapper 0.8 (c) Florian Pilz 2011"
32
+ end
33
+ end
34
+
35
+ it "should display the version when called with -v" do
36
+ result = `ruby spec/programs/another_example.rb -v`
37
+ result.strip.should == "OptParseWrapper 0.8 (c) Florian Pilz 2011"
38
+ end
39
+
40
+ it "should display a warning when --version or -v was called but no version was set" do
41
+ results = [
42
+ `ruby spec/programs/empty_example.rb --version 2>&1`,
43
+ `ruby spec/programs/empty_example.rb -v 2>&1`
44
+ ]
45
+ results.each do |result|
46
+ result.strip.should == "empty_example: version unknown"
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "parsing of default values" do
52
+ it "should display the default values if called without arguments" do
53
+ result = `ruby spec/programs/example.rb`
54
+ result.should include(":severity => 4")
55
+ result.should include(":verbose => false")
56
+ result.should include(":mutation => MightyMutation")
57
+ result.should include(":plus_selection => true")
58
+ result.should include(":selection => BestSelection")
59
+ result.should include(":chance => 0.8")
60
+ end
61
+
62
+ it "should assume false as default value if no default value was given" do
63
+ result = `ruby spec/programs/another_example.rb`
64
+ result.should include(":eat_nothing => false")
65
+ end
66
+ end
67
+
68
+ describe "setting of custom values" do
69
+ it "should display overwritten values accordingly when long option names were used" do
70
+ args = "--severity 5 --verbose --mutation DumbMutation \
71
+ --no-plus-selection --selection WorstSelection --chance 0.1"
72
+ result = `ruby spec/programs/example.rb #{args}`
73
+ result.should include(":severity => 5")
74
+ result.should include(":verbose => true")
75
+ result.should include(":mutation => DumbMutation")
76
+ result.should include(":plus_selection => false")
77
+ result.should include(":selection => WorstSelection")
78
+ result.should include(":chance => 0.1")
79
+ end
80
+
81
+ it "should display overwritten values accordingly when short option names were used" do
82
+ # there is no short form to set switches to false
83
+ args = "-s 5 -v -m DumbMutation --no-plus-selection -l WorstSelection -c 0.1"
84
+ result = `ruby spec/programs/example.rb #{args}`
85
+ result.should include(":severity => 5")
86
+ result.should include(":verbose => true")
87
+ result.should include(":mutation => DumbMutation")
88
+ result.should include(":plus_selection => false")
89
+ result.should include(":selection => WorstSelection")
90
+ result.should include(":chance => 0.1")
91
+ end
92
+ end
93
+
94
+ describe "warnings from optparse" do
95
+ it "should display a warning if an argument was invalid" do
96
+ result = `ruby spec/programs/example.rb --free-beer`
97
+ result.strip.should == "invalid option: --free-beer"
98
+ end
99
+
100
+ it "should display a warning if another argument is needed" do
101
+ result = `ruby spec/programs/example.rb --mutation`
102
+ result.strip.should == "missing argument: --mutation"
103
+ end
104
+
105
+ it "should display a warning if an argument of the wrong type was given" do
106
+ result = `ruby spec/programs/example.rb --severity OMFG!!!`
107
+ result.strip.should == "invalid argument: --severity OMFG!!!"
108
+ end
109
+
110
+ it "should display a warning if autocompletion of an argument was ambiguous" do
111
+ result = `ruby spec/programs/example.rb --se 5`
112
+ result.strip.should == "ambiguous option: --se"
113
+ end
114
+ end
115
+
116
+ describe "warnings if validation failed" do
117
+ it "should display a warning if validation value_in_set failed" do
118
+ result = `ruby spec/programs/example.rb --severity 1`
119
+ result.strip.should == "Parameter for --severity must be in [4,5,6,7,8]"
120
+ end
121
+
122
+ it "should display a warning if validation value_matches failed" do
123
+ result = `ruby spec/programs/example.rb --mutation Bazinga`
124
+ result.strip.should == "Parameter for --mutation must match /Mutation/"
125
+ end
126
+
127
+ it "should display a warning if validation value_satisfies failed" do
128
+ result = `ruby spec/programs/example.rb --chance 300.21`
129
+ result.strip.should == "Parameter for --chance must satisfy given conditions (see description)"
130
+ end
131
+
132
+ it "should validate all given validations" do
133
+ result = `ruby spec/programs/another_example.rb --eat-cake VanillaBrownie`
134
+ result.strip.should == "Parameter for --eat-cake must match /Cake/"
135
+
136
+ result = `ruby spec/programs/another_example.rb --eat-cake 2VanillaCakes`
137
+ result.strip.should == "Parameter for --eat-cake must satisfy given conditions (see description)"
138
+ end
139
+ end
140
+
141
+ describe "automatic assignment of default accessors" do
142
+ it "should assign a different character for the short accessor if the first / second / ... is already taken" do
143
+ result = `ruby spec/programs/another_example.rb --help`
144
+ result.should include("--eat-cake")
145
+ result.should include("-a, --eat-salad")
146
+ result.should include("-t, --eat-bagel")
147
+ result.should include("-n, --[no-]eat-nothing")
148
+ result.should include("-m, --eat-marshmellow")
149
+ result.should include("-e, --eat-me")
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,16 @@
1
+ require "rubygems"
2
+ require "micro-optparse"
3
+
4
+ options = Parser.new do |p|
5
+ p.version = "OptParseWrapper 0.8 (c) Florian Pilz 2011"
6
+ p.option :eat_cake, "Eath the yummy cake!", :default => "PlainCake", :value_matches => /Cake/, :value_satisfies => lambda { |arg| arg.to_i == 0 }
7
+ p.option :eat_salad, "It's healty!", :default => "CucumberSalad"
8
+ p.option :eat_bagel, "You should try it with salmon.", :default => "SalmonBagel"
9
+ p.option :eat_nothing, "Stupid decision ..."
10
+ p.option :eat_marshmellow, "Filled with sugar.", :default => "Sugar"
11
+ p.option :eat_me, "WHAT?!?", :default => "TastyHuman"
12
+ end.process!
13
+
14
+ options.each_pair do |key, value|
15
+ puts ":#{key} => #{value}"
16
+ end
@@ -0,0 +1,9 @@
1
+ require "rubygems"
2
+ require "micro-optparse"
3
+
4
+ options = Parser.new do |p|
5
+ end.process!
6
+
7
+ options.each_pair do |key, value|
8
+ puts ":#{key} => #{value}"
9
+ end
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "micro-optparse"
3
+
4
+ options = Parser.new do |p|
5
+ p.banner = "This is a banner"
6
+ p.version = "OptParseWrapper 0.8 (c) Florian Pilz 2011"
7
+ p.option :severity, "set severity", :default => 4, :value_in_set => [4,5,6,7,8]
8
+ p.option :verbose, "enable verbose output"
9
+ p.option :mutation, "set mutation", :default => "MightyMutation", :value_matches => /Mutation/
10
+ p.option :plus_selection, "use plus-selection if set", :default => true
11
+ p.option :selection, "selection used", :default => "BestSelection", :short => "l"
12
+ p.option :chance, "set mutation chance", :default => 0.8, :value_satisfies => lambda {|x| x >= 0.0 && x <= 1.0}
13
+ end.process!
14
+
15
+ options.each_pair do |key, value|
16
+ puts ":#{key} => #{value}"
17
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micro-optparse
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 8
9
- - 2
10
- version: 0.8.2
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Florian Pilz
@@ -15,11 +15,24 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-27 00:00:00 +01:00
18
+ date: 2011-03-13 00:00:00 +01:00
19
19
  default_executable:
20
- dependencies: []
21
-
22
- description: This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have an command-line parser without injecting a gem dependency.
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: This gem wraps all the functionality of optparse into an easy to use, clear and short syntax. In addtion, strong validations are added. You can either use this gem as a lightweight alternative to trollop or copy all its 75 lines into your script to have a command-line parser without injecting a gem dependency.
23
36
  email:
24
37
  - fpilz87@googlemail.com
25
38
  executables: []
@@ -38,6 +51,10 @@ files:
38
51
  - lib/micro-optparse/parser.rb
39
52
  - lib/micro-optparse/version.rb
40
53
  - micro-optparse.gemspec
54
+ - spec/parser_spec.rb
55
+ - spec/programs/another_example.rb
56
+ - spec/programs/empty_example.rb
57
+ - spec/programs/example.rb
41
58
  has_rdoc: true
42
59
  homepage: http://florianpilz.github.com/micro-optparse/
43
60
  licenses:
@@ -72,5 +89,8 @@ rubygems_version: 1.4.2
72
89
  signing_key:
73
90
  specification_version: 3
74
91
  summary: A very small wrapper around optparse.
75
- test_files: []
76
-
92
+ test_files:
93
+ - spec/parser_spec.rb
94
+ - spec/programs/another_example.rb
95
+ - spec/programs/empty_example.rb
96
+ - spec/programs/example.rb