upoj-rb 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +164 -0
- data/VERSION +1 -1
- data/lib/upoj-rb.rb +6 -2
- data/lib/upoj-rb/opts.rb +41 -36
- data/upoj-rb.gemspec +3 -3
- metadata +20 -20
- data/README.rdoc +0 -9
data/README.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# upoj-rb
|
2
|
+
|
3
|
+
This gem is a collection of ruby dependencies which I have found useful
|
4
|
+
for scripting, as well as a few additions to existing classes such as
|
5
|
+
OptionParser.
|
6
|
+
|
7
|
+
## Using
|
8
|
+
|
9
|
+
You can install upoj-rb on the command line:
|
10
|
+
|
11
|
+
gem install upoj-rb
|
12
|
+
|
13
|
+
Or add it to your project's Gemfile:
|
14
|
+
|
15
|
+
gem 'upoj-rb'
|
16
|
+
|
17
|
+
Then require and use it:
|
18
|
+
|
19
|
+
require 'upoj-rb'
|
20
|
+
puts Upoj::VERSION
|
21
|
+
|
22
|
+
## Improved Option Parser
|
23
|
+
|
24
|
+
Customized version of ruby's [OptionParser](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html).
|
25
|
+
|
26
|
+
### Funnel
|
27
|
+
|
28
|
+
By default, all options that are defined without a block can
|
29
|
+
be retrieved with `#funnel`.
|
30
|
+
|
31
|
+
# define your options
|
32
|
+
opts = Upoj::Opts.new
|
33
|
+
opts.on('--option'){ # do whatever }
|
34
|
+
opts.on('-f', '--fubar')
|
35
|
+
opts.on('-v', '--value VALUE')
|
36
|
+
|
37
|
+
# parse
|
38
|
+
ARGV #=> [ '--option', '-f', '--value', '43' ]
|
39
|
+
opts.parse!
|
40
|
+
|
41
|
+
# retrieve options in funnel by default
|
42
|
+
opts.funnel #=> { 'fubar' => true, 'value' => 43 }
|
43
|
+
|
44
|
+
The funnel can be given at construction with initial values.
|
45
|
+
|
46
|
+
# starting funnel
|
47
|
+
funnel = { 'foo' => false }
|
48
|
+
|
49
|
+
# define your options
|
50
|
+
opts = Upoj::Opts.new
|
51
|
+
opts.on('-f', '--fubar')
|
52
|
+
|
53
|
+
# parse
|
54
|
+
ARGV #=> [ '--fubar' ]
|
55
|
+
opts.parse!
|
56
|
+
|
57
|
+
# retrieve the funnel with initial and new options
|
58
|
+
opts.funnel #=> { 'foo' => false, '--fubar' => true }
|
59
|
+
|
60
|
+
### Structured Banner
|
61
|
+
|
62
|
+
A hash can be given for the banner.
|
63
|
+
|
64
|
+
banner = {
|
65
|
+
:usage => '[OPTION]... ARG1 ARG2',
|
66
|
+
:description => 'does stuff with ARG1 and ARG2.'
|
67
|
+
}
|
68
|
+
|
69
|
+
opts = Upoj::Opts.new :banner => banner
|
70
|
+
opts.on('-f', '--fubar', 'do something awful')
|
71
|
+
|
72
|
+
# the generated banner will look like this,
|
73
|
+
# with USAGE, OPTIONS and my_script in bold
|
74
|
+
|
75
|
+
my_script does stuff with ARG1 and ARG2.
|
76
|
+
|
77
|
+
USAGE
|
78
|
+
my_script [OPTION]... ARG1 ARG2
|
79
|
+
|
80
|
+
OPTIONS
|
81
|
+
-f, --fubar do something awful
|
82
|
+
|
83
|
+
### Help and Usage
|
84
|
+
|
85
|
+
Automatically register `-h`, `--help`, `-u` and `--usage` switches
|
86
|
+
with `#help!` and `#usage!`.
|
87
|
+
|
88
|
+
opts = Upoj::Opts.new
|
89
|
+
|
90
|
+
# you can replace this:
|
91
|
+
opts.on('-u', '--usage', 'show this help and exit'){ puts opts; exit 0 }
|
92
|
+
|
93
|
+
# by this:
|
94
|
+
opts.usage!
|
95
|
+
|
96
|
+
## Included Dependencies
|
97
|
+
|
98
|
+
* __active_support/core_ext/array/extract_options__
|
99
|
+
|
100
|
+
Extraction of hash options from the end of an array.
|
101
|
+
|
102
|
+
# extract options
|
103
|
+
args = [ 'a', 'b', { 'c' => 'd', 'e' => 'f' } ]
|
104
|
+
options = args.extract_options! #=> { 'c' => 'd', 'e' => 'f' }
|
105
|
+
|
106
|
+
# returns an empty hash if there are no options
|
107
|
+
args = [ 'a', 'b', 'c' ]
|
108
|
+
options = args.extract_options! #=> {}
|
109
|
+
|
110
|
+
# can be used to easily pass an optional hash to methods
|
111
|
+
def method *args
|
112
|
+
options = args.extract_options!
|
113
|
+
end
|
114
|
+
|
115
|
+
* __active_support/core_ext/hash/indifferent_access__
|
116
|
+
|
117
|
+
Hash that makes no difference between strings and
|
118
|
+
symbols as keys.
|
119
|
+
|
120
|
+
h = HashWithIndifferentAccess.new 'a' => 'value a', :b => 'value b'
|
121
|
+
h['a'] #=> 'value a'
|
122
|
+
h[:a] #=> 'value a'
|
123
|
+
h['b'] #=> 'value b'
|
124
|
+
h[:b] #=> 'value b'
|
125
|
+
|
126
|
+
* __active_support/core_ext/object/blank__
|
127
|
+
|
128
|
+
`#blank?` and `#present?` methods to easily check
|
129
|
+
for empty strings, arrays, hashes, etc.
|
130
|
+
|
131
|
+
''.present? #=> false
|
132
|
+
''.blank? #=> true
|
133
|
+
[].blank? #=> true
|
134
|
+
{}.blank? #=> true
|
135
|
+
nil.blank? #=> true
|
136
|
+
|
137
|
+
* __active_support/core_ext/object/try__
|
138
|
+
|
139
|
+
Allows to attempt to call a method on an object
|
140
|
+
and not throw an error if it's nil.
|
141
|
+
|
142
|
+
s = 'FUBAR'
|
143
|
+
s.try :downcase #=> 'fubar'
|
144
|
+
s = nil
|
145
|
+
s.try :downcase #=> nil
|
146
|
+
|
147
|
+
* __active_support/core_ext/string/inflections__
|
148
|
+
|
149
|
+
Useful string transformations.
|
150
|
+
|
151
|
+
'Module1::Module2::MyClass'.demodulize #=> 'MyClass'
|
152
|
+
|
153
|
+
* __paint__
|
154
|
+
|
155
|
+
Command-line color output.
|
156
|
+
|
157
|
+
Paint['fubar', :green]
|
158
|
+
Paint['fubar', :bold, :underline]
|
159
|
+
|
160
|
+
## Copyright
|
161
|
+
|
162
|
+
Copyright (c) 2011 AlphaHydrae. See LICENSE.txt for
|
163
|
+
further details.
|
164
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/upoj-rb.rb
CHANGED
@@ -4,8 +4,12 @@ require 'active_support/core_ext/object/blank'
|
|
4
4
|
require 'active_support/core_ext/object/try'
|
5
5
|
require 'active_support/core_ext/string/inflections'
|
6
6
|
|
7
|
+
dirname = File.dirname __FILE__
|
8
|
+
deps_dir = File.join dirname, 'upoj-rb'
|
9
|
+
VERSION_FILE = File.join dirname, '..', 'VERSION'
|
10
|
+
|
7
11
|
module Upoj
|
8
|
-
|
12
|
+
VERSION = File.open(VERSION_FILE, 'r').read
|
9
13
|
end
|
10
14
|
|
11
|
-
%w( opts signals ).each{ |dep| require File.join(
|
15
|
+
%w( opts signals ).each{ |dep| require File.join(deps_dir, dep) }
|
data/lib/upoj-rb/opts.rb
CHANGED
@@ -3,7 +3,27 @@ require 'paint'
|
|
3
3
|
|
4
4
|
module Upoj
|
5
5
|
|
6
|
+
# Customized version of ruby's OptionParser.
|
6
7
|
class Opts < OptionParser
|
8
|
+
|
9
|
+
# Hash that will be filled with the values of all options
|
10
|
+
# that were defined without a block.
|
11
|
+
#
|
12
|
+
# ==== Examples
|
13
|
+
# opts = Upoj::Opts.new
|
14
|
+
# opts.on('--option'){ # do whatever }
|
15
|
+
# opts.on('-f', '--fubar')
|
16
|
+
# opts.on('--value VALUE')
|
17
|
+
#
|
18
|
+
# ARGV #=> [ '--option', '-f', '--value', 43 ]
|
19
|
+
# opts.parse!
|
20
|
+
#
|
21
|
+
# # retrieve options in funnel by default
|
22
|
+
# opts.funnel #=> { 'fubar' => true, 'value' => 43 }
|
23
|
+
#
|
24
|
+
# # a funnel with initial values can be given at construction
|
25
|
+
# funnel = { 'foo' => false }
|
26
|
+
# opts = Upoj::Opts.new :funnel => funnel
|
7
27
|
attr_accessor :funnel
|
8
28
|
|
9
29
|
def self.section_title title
|
@@ -38,6 +58,27 @@ module Upoj
|
|
38
58
|
super(*args, &block)
|
39
59
|
end
|
40
60
|
end
|
61
|
+
def program_name
|
62
|
+
@program_name || File.basename($0)
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_s
|
66
|
+
"#{super}#{summary_examples_section}#{@footer}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def help!
|
70
|
+
self.on('-h', '--help', 'show this help and exit'){ puts self; exit 0 }
|
71
|
+
end
|
72
|
+
|
73
|
+
def usage!
|
74
|
+
self.on('-u', '--usage', 'show this help and exit'){ puts self; exit 0 }
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def summary_program_name
|
80
|
+
Paint[program_name, :bold]
|
81
|
+
end
|
41
82
|
|
42
83
|
def summary_banner_section *args
|
43
84
|
options = args.extract_options!
|
@@ -58,41 +99,5 @@ module Upoj
|
|
58
99
|
end
|
59
100
|
end
|
60
101
|
end
|
61
|
-
|
62
|
-
def program_name
|
63
|
-
@program_name || File.basename($0)
|
64
|
-
end
|
65
|
-
|
66
|
-
def summary_program_name
|
67
|
-
Paint[program_name, :bold]
|
68
|
-
end
|
69
|
-
|
70
|
-
def parse_or_exit!
|
71
|
-
begin
|
72
|
-
parse!
|
73
|
-
rescue Exception => err
|
74
|
-
unless err.kind_of?(SystemExit)
|
75
|
-
puts
|
76
|
-
Kernel.warn Paint["Error: #{err.message}", :yellow]
|
77
|
-
puts
|
78
|
-
puts self
|
79
|
-
exit 2
|
80
|
-
else
|
81
|
-
exit err.status
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def to_s
|
87
|
-
"#{super}#{summary_examples_section}#{@footer}"
|
88
|
-
end
|
89
|
-
|
90
|
-
def help!
|
91
|
-
self.on('-h', '--help', 'show this help and exit'){ puts self; exit 0 }
|
92
|
-
end
|
93
|
-
|
94
|
-
def usage!
|
95
|
-
self.on('-u', '--usage', 'show this help and exit'){ puts self; exit 0 }
|
96
|
-
end
|
97
102
|
end
|
98
103
|
end
|
data/upoj-rb.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "upoj-rb"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["AlphaHydrae"]
|
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = "hydrae.alpha@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
"Gemfile",
|
24
24
|
"Gemfile.lock",
|
25
25
|
"LICENSE.txt",
|
26
|
-
"README.
|
26
|
+
"README.md",
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"lib/upoj-rb.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upoj-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-10-01 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: paint
|
16
|
-
requirement: &
|
16
|
+
requirement: &2158212740 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2158212740
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: active_support
|
27
|
-
requirement: &
|
27
|
+
requirement: &2158212260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2158212260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2158207680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2158207680
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: shoulda
|
49
|
-
requirement: &
|
49
|
+
requirement: &2158207080 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2158207080
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bundler
|
60
|
-
requirement: &
|
60
|
+
requirement: &2158206480 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.0.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2158206480
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: jeweler
|
71
|
-
requirement: &
|
71
|
+
requirement: &2158205880 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 1.6.4
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2158205880
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: gemcutter
|
82
|
-
requirement: &
|
82
|
+
requirement: &2158205280 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2158205280
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rdoc
|
93
|
-
requirement: &
|
93
|
+
requirement: &2158204560 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *2158204560
|
102
102
|
description: This contains common ruby extensions mostly taken from Rails, as well
|
103
103
|
as various command-line utilities.
|
104
104
|
email: hydrae.alpha@gmail.com
|
@@ -106,7 +106,7 @@ executables: []
|
|
106
106
|
extensions: []
|
107
107
|
extra_rdoc_files:
|
108
108
|
- LICENSE.txt
|
109
|
-
- README.
|
109
|
+
- README.md
|
110
110
|
files:
|
111
111
|
- .document
|
112
112
|
- .rspec
|
@@ -114,7 +114,7 @@ files:
|
|
114
114
|
- Gemfile
|
115
115
|
- Gemfile.lock
|
116
116
|
- LICENSE.txt
|
117
|
-
- README.
|
117
|
+
- README.md
|
118
118
|
- Rakefile
|
119
119
|
- VERSION
|
120
120
|
- lib/upoj-rb.rb
|
@@ -140,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
140
|
version: '0'
|
141
141
|
segments:
|
142
142
|
- 0
|
143
|
-
hash:
|
143
|
+
hash: -2066870811834978715
|
144
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
145
|
none: false
|
146
146
|
requirements:
|