pickled_optparse 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ === Version 0.1.1
2
+
3
+ No functional differences just some internal refactoring and clean up.
4
+
5
+ * Replaced intrusive replacement of make_switch with a wrapper and alias.
6
+ * Minor reorginization of directories to follow best practices.
7
+ * Added Jeweler style versioning.
8
+ * Cleaned up and moved example.rb to examples directory (best practices?).
9
+ * Cleaned up rakefile.
10
+ * Kludged the hell out of rdoc to make it allow example ruby files in the files section.
11
+
12
+ === Version 0.1.0
13
+
14
+ Initial release.
@@ -16,9 +16,10 @@ Then at the end of your OptionParser block add something like this:
16
16
  exit
17
17
  end
18
18
 
19
- You of course need to require the "pickled_optparse" gem but that's it. You don't need to require the opt_parse file because that's already handled for you.
19
+ You of course need to require the "pickled_optparse" gem but that's it.
20
+ You don't need to require the opt_parse file because that's already handled for you.
20
21
 
21
- For a complete usage example see the lib/example.rb file.
22
+ For a complete usage example see the examples/example.rb file.
22
23
 
23
24
  == Note on Patches/Pull Requests
24
25
 
data/Rakefile CHANGED
@@ -1,17 +1,23 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
 
4
+ $:.unshift 'lib'
5
+ require 'pickled_optparse/version'
6
+ version = PickledOptparse::Version::STRING
7
+
4
8
  begin
5
9
  require 'jeweler'
6
10
  Jeweler::Tasks.new do |gem|
7
11
  gem.required_ruby_version = '~> 1.9.2' # Everyone should upgrade, now!
8
12
  gem.name = "pickled_optparse"
9
- gem.summary = %Q{Adds required switches to the OptionParser class}
13
+ gem.version = version
14
+ gem.summary = %Q{Adds required switches to Ruby's OptionParser class}
10
15
  gem.description = %Q{Adds the ability to easily specify and test for required switches in Ruby's built-in OptionParser class}
11
16
  gem.email = "picklepumpers@gmail.com"
12
17
  gem.homepage = "http://github.com/PicklePumpers/pickled_optparse"
13
18
  gem.authors = ["Mike Bethany"]
14
19
  gem.add_development_dependency "rspec", ">= 2.0.1"
20
+ gem.add_development_dependency "syntax", ">= 1.0.0"
15
21
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
22
  end
17
23
  Jeweler::GemcutterTasks.new
@@ -20,25 +26,50 @@ rescue LoadError
20
26
  end
21
27
 
22
28
  require 'rspec/core/rake_task'
23
- RSpec::Core::RakeTask.new do |t|
29
+ RSpec::Core::RakeTask.new(:spec) do |t|
24
30
  t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
25
31
  t.pattern = 'spec/*/*_spec.rb'
26
32
  end
27
-
28
- RSpec::Core::RakeTask.new(:rcov) do |t|
29
- t.rcov_opts = %q[--exclude "spec"]
30
- end
31
-
32
33
  task :spec => :check_dependencies
33
34
 
34
- task :default => :spec
35
-
35
+ # Build rdocs
36
36
  require 'rake/rdoctask'
37
- Rake::RDocTask.new do |rdoc|
38
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+ require 'syntax/convertors/html'
38
+ rdoc_dir = 'rdoc'
39
+ # This is rdoc1 but it doesn't work unless you DON'T wrap it in a task
40
+ # Generate html files from example ruby files
41
+ convertor = Syntax::Convertors::HTML.for_syntax "ruby"
42
+ replacement_key = "REPLACE_THIS_TEXT_WITH_PROPER_HTML"
43
+ # Create dummy files
44
+ Dir.glob('examples/*.rb').each do |file|
45
+ File.open("#{file}.txt", "w") do |dummy_file|
46
+ dummy_file.write(replacement_key)
47
+ end
48
+ end
39
49
 
40
- rdoc.rdoc_dir = 'rdoc'
50
+ # Call the rdoc task
51
+ Rake::RDocTask.new(:rdoc2) do |rdoc|
52
+ rdoc.rdoc_dir = rdoc_dir
41
53
  rdoc.title = "pickled_optparse #{version}"
42
54
  rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('HISTORY*')
56
+ rdoc.rdoc_files.include('examples/*.txt')
43
57
  rdoc.rdoc_files.include('lib/**/*.rb')
44
58
  end
59
+
60
+ task :rdoc3 do
61
+ # Now use a hammer to replace the dummy text with the
62
+ # html we want to use in our ruby example code file.
63
+ Dir.glob('examples/*.rb').each do |file|
64
+ html_header = File.read('rake_reqs/html_header.html')
65
+ html_ruby = convertor.convert(File.read(file))
66
+ rdoc_file = "#{rdoc_dir}/examples/#{File.basename(file,".rb")}_rb_txt.html"
67
+ fixed_html = File.read(rdoc_file).gsub!(replacement_key, "#{html_header}#{html_ruby}")
68
+ File.open(rdoc_file, "w") {|f| f.write(fixed_html)}
69
+ File.delete("#{file}.txt")
70
+ end
71
+ end
72
+
73
+ task :rdoc => [:rdoc2, :rdoc3]
74
+
75
+ task :default => :spec
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- require_relative 'pickled_optparse'
2
+ require_relative '../lib/pickled_optparse'
3
3
 
4
4
  # Configure options based on command line options
5
5
  @options = {}
6
6
  OptionParser.new do |opts|
7
- opts.banner = "Usage: test [options] in_file[.srt] out_file[.srt]"
7
+ opts.banner = "Usage: example.rb [options]"
8
8
 
9
9
  # Note that :required can be anywhere in the parameters
10
10
 
@@ -40,4 +40,5 @@ OptionParser.new do |opts|
40
40
  exit
41
41
  end
42
42
 
43
- end.parse!
43
+ end.parse!
44
+
@@ -1,156 +1,2 @@
1
- require 'optparse'
2
-
3
- # Add the ability to specify switches as required to OptionParser
4
- class OptionParser
5
-
6
- # An array of messages describing any missing required switches
7
- attr_reader :missing_switches
8
-
9
- # Convenience method to test if we're missing any required switches
10
- def missing_switches?
11
- !@missing_switches.nil?
12
- end
13
-
14
- def make_switch(opts, block = nil)
15
- short, long, nolong, style, pattern, conv, not_pattern, not_conv, not_style = [], [], []
16
- ldesc, sdesc, desc, arg = [], [], []
17
- default_style = Switch::NoArgument
18
- default_pattern = nil
19
- klass = nil
20
- n, q, a = nil
21
-
22
- # Check for required switches
23
- required = opts.delete(:required)
24
-
25
- opts.each do |o|
26
-
27
- # argument class
28
- next if search(:atype, o) do |pat, c|
29
- klass = notwice(o, klass, 'type')
30
- if not_style and not_style != Switch::NoArgument
31
- not_pattern, not_conv = pat, c
32
- else
33
- default_pattern, conv = pat, c
34
- end
35
- end
36
-
37
- # directly specified pattern(any object possible to match)
38
- if (!(String === o || Symbol === o)) and o.respond_to?(:match)
39
- pattern = notwice(o, pattern, 'pattern')
40
- if pattern.respond_to?(:convert)
41
- conv = pattern.method(:convert).to_proc
42
- else
43
- conv = SPLAT_PROC
44
- end
45
- next
46
- end
47
-
48
- # anything others
49
- case o
50
- when Proc, Method
51
- block = notwice(o, block, 'block')
52
- when Array, Hash
53
- case pattern
54
- when CompletingHash
55
- when nil
56
- pattern = CompletingHash.new
57
- conv = pattern.method(:convert).to_proc if pattern.respond_to?(:convert)
58
- else
59
- raise ArgumentError, "argument pattern given twice"
60
- end
61
- o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
62
- when Module
63
- raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
64
- when *ArgumentStyle.keys
65
- style = notwice(ArgumentStyle[o], style, 'style')
66
- when /^--no-([^\[\]=\s]*)(.+)?/
67
- q, a = $1, $2
68
- o = notwice(a ? Object : TrueClass, klass, 'type')
69
- not_pattern, not_conv = search(:atype, o) unless not_style
70
- not_style = (not_style || default_style).guess(arg = a) if a
71
- default_style = Switch::NoArgument
72
- default_pattern, conv = search(:atype, FalseClass) unless default_pattern
73
- ldesc << "--no-#{q}"
74
- long << 'no-' + (q = q.downcase)
75
- nolong << q
76
- when /^--\[no-\]([^\[\]=\s]*)(.+)?/
77
- q, a = $1, $2
78
- o = notwice(a ? Object : TrueClass, klass, 'type')
79
- if a
80
- default_style = default_style.guess(arg = a)
81
- default_pattern, conv = search(:atype, o) unless default_pattern
82
- end
83
- ldesc << "--[no-]#{q}"
84
- long << (o = q.downcase)
85
- not_pattern, not_conv = search(:atype, FalseClass) unless not_style
86
- not_style = Switch::NoArgument
87
- nolong << 'no-' + o
88
- when /^--([^\[\]=\s]*)(.+)?/
89
- q, a = $1, $2
90
- if a
91
- o = notwice(NilClass, klass, 'type')
92
- default_style = default_style.guess(arg = a)
93
- default_pattern, conv = search(:atype, o) unless default_pattern
94
- end
95
- ldesc << "--#{q}"
96
- long << (o = q.downcase)
97
- when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
98
- q, a = $1, $2
99
- o = notwice(Object, klass, 'type')
100
- if a
101
- default_style = default_style.guess(arg = a)
102
- default_pattern, conv = search(:atype, o) unless default_pattern
103
- end
104
- sdesc << "-#{q}"
105
- short << Regexp.new(q)
106
- when /^-(.)(.+)?/
107
- q, a = $1, $2
108
- if a
109
- o = notwice(NilClass, klass, 'type')
110
- default_style = default_style.guess(arg = a)
111
- default_pattern, conv = search(:atype, o) unless default_pattern
112
- end
113
- sdesc << "-#{q}"
114
- short << q
115
- when /^=/
116
- style = notwice(default_style.guess(arg = o), style, 'style')
117
- default_pattern, conv = search(:atype, Object) unless default_pattern
118
- else
119
- desc.push(o)
120
- end
121
-
122
- end
123
-
124
- default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
125
- if !(short.empty? and long.empty?)
126
- s = (style || default_style).new(pattern || default_pattern, conv, sdesc, ldesc, arg, desc, block)
127
- elsif !block
128
- if style or pattern
129
- raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller)
130
- end
131
- s = desc
132
- else
133
- short << pattern
134
- s = (style || default_style).new(pattern, conv, nil, nil, arg, desc, block)
135
- end
136
-
137
- # Make sure required switches are given
138
- if required && !(default_argv.include?("-#{short[0]}") || default_argv.include?("--#{long[0]}"))
139
- @missing_switches ||= [] # Should be placed in initialize if incorporated into Ruby proper
140
-
141
- # This is ugly, long, and not very DRY but it is easy to understand
142
- #missing = "-#{short[0]}" if !short.empty?
143
- #missing = "#{missing} or " if !short.empty? && !long.empty?
144
- #missing = "#{missing}--#{long[0]}" if !long.empty?
145
-
146
- # This is even uglier, and really hard to read, but it is shorter and
147
- # as DRY as I could figure out how to make it... but it still stinks.
148
- @missing_switches << "Missing switch: #{"-#{short[0]}" if !short.empty?}#{" or " if !short.empty? && !long.empty?}#{"--#{long[0]}" if !long.empty?}"
149
- end
150
-
151
- return s, short, long,
152
- (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
153
- nolong
154
- end
155
-
156
- end
1
+ require_relative 'pickled_optparse/pickled_optparse'
2
+ require_relative 'pickled_optparse/version'
@@ -0,0 +1,42 @@
1
+ require 'optparse'
2
+
3
+ # Add the ability to specify switches as required to OptionParser
4
+ class OptionParser
5
+
6
+ # An array of messages describing any missing required switches
7
+ attr_reader :missing_switches
8
+
9
+ # Convenience method to test if we're missing any required switches
10
+ def missing_switches?
11
+ !@missing_switches.nil?
12
+ end
13
+
14
+ # Alias the OptionParser::make_switch function
15
+ # (instead of directly modifying it like I did in 0.1.0)
16
+ alias :pickled_make_switch :make_switch
17
+
18
+ # Wrapper for OptionParser::make_switch to allow for required switches
19
+ def make_switch(opts, block = nil)
20
+
21
+ # Test if a switch is required
22
+ required = opts.delete(:required)
23
+
24
+ return_values = pickled_make_switch(opts, block)
25
+
26
+ # Make sure required switches are given
27
+ if required
28
+ short = return_values[1][0].nil? ? nil : "-#{return_values[1][0]}"
29
+ long = return_values[2][0].nil? ? nil : "--#{return_values[2][0]}"
30
+
31
+ if !(default_argv.include?(short) || default_argv.include?(long))
32
+ @missing_switches ||= [] # Should be placed in initialize if incorporated into Ruby proper
33
+
34
+ # Ugly and hard to read, should figure out a prettier way of doing this
35
+ @missing_switches << "Missing switch: #{short if !short.nil?}#{" or " if !short.nil? && !long.nil?}#{long if !long.nil?}"
36
+ end
37
+ end
38
+
39
+ return return_values
40
+ end
41
+
42
+ end
@@ -0,0 +1,12 @@
1
+ # Contains any pickled_optparse specific settings
2
+ module PickledOptparse
3
+ # The current version of the software
4
+ module Version
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ PATCH = 1
8
+ BUILD = nil
9
+
10
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
11
+ end
12
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mike Bethany
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-30 00:00:00 -04:00
17
+ date: 2010-11-02 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,21 @@ dependencies:
32
32
  version: 2.0.1
33
33
  type: :development
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: syntax
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ type: :development
49
+ version_requirements: *id002
35
50
  description: Adds the ability to easily specify and test for required switches in Ruby's built-in OptionParser class
36
51
  email: picklepumpers@gmail.com
37
52
  executables: []
@@ -44,13 +59,15 @@ extra_rdoc_files:
44
59
  files:
45
60
  - .document
46
61
  - .rspec
62
+ - HISTORY.rdoc
47
63
  - LICENSE
48
64
  - README.rdoc
49
65
  - Rakefile
50
- - VERSION
51
66
  - autotest/discover.rb
52
- - lib/example.rb
67
+ - examples/example.rb
53
68
  - lib/pickled_optparse.rb
69
+ - lib/pickled_optparse/pickled_optparse.rb
70
+ - lib/pickled_optparse/version.rb
54
71
  - spec/pickled_optparse/pickled_optparse_spec.rb
55
72
  - spec/spec_helper.rb
56
73
  has_rdoc: true
@@ -86,7 +103,8 @@ rubyforge_project:
86
103
  rubygems_version: 1.3.7
87
104
  signing_key:
88
105
  specification_version: 3
89
- summary: Adds required switches to the OptionParser class
106
+ summary: Adds required switches to Ruby's OptionParser class
90
107
  test_files:
108
+ - examples/example.rb
91
109
  - spec/pickled_optparse/pickled_optparse_spec.rb
92
110
  - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0