optparshie 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +0 -0
- data/.rspec +0 -0
- data/Gemfile +3 -5
- data/LICENSE.txt +0 -0
- data/Rakefile +0 -0
- data/VERSION +1 -1
- data/example/example1.rb +2 -2
- data/lib/optparshie.rb +148 -132
- data/optparshie.gemspec +14 -19
- data/spec/optparshie_spec.rb +0 -0
- data/spec/spec_helper.rb +0 -0
- data/test/test1.rb +43 -0
- metadata +23 -29
data/.document
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/Gemfile
CHANGED
@@ -2,13 +2,11 @@ source "http://rubygems.org"
|
|
2
2
|
# Add dependencies required to use your gem here.
|
3
3
|
# Example:
|
4
4
|
# gem "activesupport", ">= 2.3.5"
|
5
|
-
gem "hashie", ">= 1.2.0"
|
6
5
|
|
7
6
|
# Add dependencies to develop your gem here.
|
8
7
|
# Include everything needed to run rake, tests, features, etc.
|
9
8
|
group :development do
|
10
|
-
gem "rspec", "
|
11
|
-
gem "bundler", "
|
12
|
-
gem "jeweler", "
|
13
|
-
gem "rcov", ">= 0"
|
9
|
+
gem "rspec", ">= 2.3.0"
|
10
|
+
gem "bundler", ">= 1.0.0"
|
11
|
+
gem "jeweler", ">= 1.6.4"
|
14
12
|
end
|
data/LICENSE.txt
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/example/example1.rb
CHANGED
@@ -28,5 +28,5 @@ puts("opt.waros => #{opt.waros}");
|
|
28
28
|
# Oops! It's already defined method....
|
29
29
|
# opt.on => Usage: example1 (blah blah blah)
|
30
30
|
|
31
|
-
#
|
32
|
-
puts("opt.
|
31
|
+
# You should access via short option instead.... :X
|
32
|
+
puts("opt.o => #{opt.o}");
|
data/lib/optparshie.rb
CHANGED
@@ -1,132 +1,148 @@
|
|
1
|
-
#!/bin/env ruby
|
2
|
-
#-*- coding: utf-8 -*-
|
3
|
-
require("rubygems");
|
4
|
-
require("optparse");
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
def parse
|
30
|
-
|
31
|
-
top.list.each { |sw| hash_sw_add(sw,nil); }
|
32
|
-
|
33
|
-
|
34
|
-
return
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
end
|
1
|
+
#!/bin/env ruby
|
2
|
+
#-*- coding: utf-8 -*-
|
3
|
+
require("rubygems");
|
4
|
+
require("optparse");
|
5
|
+
|
6
|
+
class OptionParshie < OptionParser
|
7
|
+
def initialize()
|
8
|
+
super;
|
9
|
+
hash_init();
|
10
|
+
end
|
11
|
+
|
12
|
+
def hash_init()
|
13
|
+
@opt_hash = Hash.new();
|
14
|
+
@opt_key = Hash.new();
|
15
|
+
end
|
16
|
+
|
17
|
+
def hash_key_sanitize(switch)
|
18
|
+
return switch.gsub(/^-+/,"").gsub(/\W/,"_");
|
19
|
+
end
|
20
|
+
|
21
|
+
def hash_sw_add(sw,val)
|
22
|
+
keys = (sw.short + sw.long).map { |x| hash_key_sanitize(x); };
|
23
|
+
primary_key = keys.first;
|
24
|
+
keys.each { |key| @opt_key[key] = primary_key; }
|
25
|
+
|
26
|
+
@opt_hash[primary_key] = val;
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(*argv)
|
30
|
+
hash_init();
|
31
|
+
top.list.each { |sw| hash_sw_add(sw,nil); }
|
32
|
+
x = super;
|
33
|
+
|
34
|
+
return x;
|
35
|
+
end
|
36
|
+
|
37
|
+
def parse!(*argv)
|
38
|
+
hash_init();
|
39
|
+
top.list.each { |sw| hash_sw_add(sw,nil); }
|
40
|
+
x = super;
|
41
|
+
|
42
|
+
return x;
|
43
|
+
end
|
44
|
+
|
45
|
+
# ----------------------------------------------------------------------------
|
46
|
+
# Borrowed from optparse.rb
|
47
|
+
# (author of optparse.rb is Nobu Nakada-san, thanks!)
|
48
|
+
# ----------------------------------------------------------------------------
|
49
|
+
def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
|
50
|
+
opt, arg, val, rest = nil
|
51
|
+
nonopt ||= proc {|a| throw :terminate, a}
|
52
|
+
argv.unshift(arg) if arg = catch(:terminate) {
|
53
|
+
while arg = argv.shift
|
54
|
+
case arg
|
55
|
+
# long option
|
56
|
+
when /\A--([^=]*)(?:=(.*))?/m
|
57
|
+
opt, rest = $1, $2
|
58
|
+
begin
|
59
|
+
sw, = complete(:long, opt, true)
|
60
|
+
rescue ParseError
|
61
|
+
raise $!.set_option(arg, true)
|
62
|
+
end
|
63
|
+
begin
|
64
|
+
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
65
|
+
|
66
|
+
# ------------------------------------------------------------------
|
67
|
+
# optparshie code by l.p.m.11
|
68
|
+
# ------------------------------------------------------------------
|
69
|
+
hash_sw_add(sw,val);
|
70
|
+
|
71
|
+
val = cb.call(val) if cb
|
72
|
+
setter.call(sw.switch_name, val) if setter
|
73
|
+
rescue ParseError
|
74
|
+
raise $!.set_option(arg, rest)
|
75
|
+
end
|
76
|
+
|
77
|
+
# short option
|
78
|
+
when /\A-(.)((=).*|.+)?/m
|
79
|
+
opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
|
80
|
+
begin
|
81
|
+
sw, = search(:short, opt)
|
82
|
+
unless sw
|
83
|
+
begin
|
84
|
+
sw, = complete(:short, opt)
|
85
|
+
# short option matched.
|
86
|
+
val = arg.sub(/\A-/, '')
|
87
|
+
has_arg = true
|
88
|
+
rescue InvalidOption
|
89
|
+
# if no short options match, try completion with long
|
90
|
+
# options.
|
91
|
+
sw, = complete(:long, opt)
|
92
|
+
eq ||= !rest
|
93
|
+
end
|
94
|
+
end
|
95
|
+
rescue ParseError
|
96
|
+
raise $!.set_option(arg, true)
|
97
|
+
end
|
98
|
+
begin
|
99
|
+
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
100
|
+
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
|
101
|
+
|
102
|
+
# ------------------------------------------------------------------
|
103
|
+
# optparshie code by l.p.m.11
|
104
|
+
# ------------------------------------------------------------------
|
105
|
+
hash_sw_add(sw,val);
|
106
|
+
|
107
|
+
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
108
|
+
val = cb.call(val) if cb
|
109
|
+
setter.call(sw.switch_name, val) if setter
|
110
|
+
rescue ParseError
|
111
|
+
raise $!.set_option(arg, arg.length > 2)
|
112
|
+
end
|
113
|
+
|
114
|
+
# non-option argument
|
115
|
+
else
|
116
|
+
catch(:prune) do
|
117
|
+
visit(:each_option) do |sw0|
|
118
|
+
sw = sw0
|
119
|
+
sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
|
120
|
+
end
|
121
|
+
nonopt.call(arg)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
nil
|
127
|
+
}
|
128
|
+
|
129
|
+
visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
|
130
|
+
|
131
|
+
argv
|
132
|
+
end
|
133
|
+
|
134
|
+
def method_missing(name,*args)
|
135
|
+
name = name.to_s();
|
136
|
+
subst = !(name.gsub!(/=$/,"").nil?);
|
137
|
+
raise if (!@opt_key.key?(name));
|
138
|
+
|
139
|
+
if (!subst)
|
140
|
+
return @opt_hash[@opt_key[name]];
|
141
|
+
else
|
142
|
+
return @opt_hash[@opt_key[name]] = args.first;
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
attr_reader :opt_hash;
|
147
|
+
private :hash_key_sanitize, :hash_sw_add, :hash_init;
|
148
|
+
end
|
data/optparshie.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "optparshie"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["lpm11"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2012-10-03"
|
13
13
|
s.description = "OptionParser with Hashie::Mash like accessibility."
|
14
14
|
s.email = "lpm11r@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,36 +28,31 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/optparshie.rb",
|
29
29
|
"optparshie.gemspec",
|
30
30
|
"spec/optparshie_spec.rb",
|
31
|
-
"spec/spec_helper.rb"
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"test/test1.rb"
|
32
33
|
]
|
33
34
|
s.homepage = "http://github.com/lpm11/optparshie"
|
34
35
|
s.licenses = ["WTFPL"]
|
35
36
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = "1.8.
|
37
|
+
s.rubygems_version = "1.8.24"
|
37
38
|
s.summary = "OptionParser with Hashie::Mash like accessibility."
|
38
39
|
|
39
40
|
if s.respond_to? :specification_version then
|
40
41
|
s.specification_version = 3
|
41
42
|
|
42
43
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
-
s.
|
44
|
-
s.add_development_dependency(%q<
|
45
|
-
s.add_development_dependency(%q<
|
46
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
47
|
-
s.add_development_dependency(%q<rcov>, [">= 0"])
|
44
|
+
s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
|
45
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.6.4"])
|
48
47
|
else
|
49
|
-
s.add_dependency(%q<
|
50
|
-
s.add_dependency(%q<
|
51
|
-
s.add_dependency(%q<
|
52
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
53
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
48
|
+
s.add_dependency(%q<rspec>, [">= 2.3.0"])
|
49
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
50
|
+
s.add_dependency(%q<jeweler>, [">= 1.6.4"])
|
54
51
|
end
|
55
52
|
else
|
56
|
-
s.add_dependency(%q<
|
57
|
-
s.add_dependency(%q<
|
58
|
-
s.add_dependency(%q<
|
59
|
-
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
53
|
+
s.add_dependency(%q<rspec>, [">= 2.3.0"])
|
54
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, [">= 1.6.4"])
|
61
56
|
end
|
62
57
|
end
|
63
58
|
|
data/spec/optparshie_spec.rb
CHANGED
File without changes
|
data/spec/spec_helper.rb
CHANGED
File without changes
|
data/test/test1.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
#-*- coding: utf-8 -*-
|
3
|
+
require("test/unit");
|
4
|
+
require("../lib/optparshie");
|
5
|
+
|
6
|
+
class TestCase_Main < Test::Unit::TestCase
|
7
|
+
def test_fundamental()
|
8
|
+
argv = "--exp --wa -a 2 ADDITION".split(/\s+/);
|
9
|
+
|
10
|
+
opt = OptionParshie.new();
|
11
|
+
opt.on("-a","--a-value=VAL");
|
12
|
+
opt.on("--exp");
|
13
|
+
opt.on("-w","--waros");
|
14
|
+
opt.on("-k");
|
15
|
+
opt.parse(argv);
|
16
|
+
|
17
|
+
assert_equal(opt.a, "2");
|
18
|
+
assert_equal(opt.a_value, "2");
|
19
|
+
assert(opt.exp);
|
20
|
+
assert(opt.w);
|
21
|
+
assert(opt.waros);
|
22
|
+
assert(!opt.k);
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_reference()
|
26
|
+
argv = "--test=128 --example".split(/\s+/);
|
27
|
+
|
28
|
+
opt = OptionParshie.new();
|
29
|
+
opt.on("-t","--test=VAL",Integer);
|
30
|
+
opt.on("-e","--example");
|
31
|
+
opt.parse(argv);
|
32
|
+
|
33
|
+
assert_equal(opt.t, 128);
|
34
|
+
assert_equal(opt.test, 128);
|
35
|
+
assert(opt.e);
|
36
|
+
opt.t = 129;
|
37
|
+
assert_equal(opt.t, 129);
|
38
|
+
assert_equal(opt.test, 129);
|
39
|
+
opt.e = "exp";
|
40
|
+
assert_equal(opt.e, "exp");
|
41
|
+
assert_equal(opt.example, "exp");
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optparshie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,63 +9,56 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
22
|
-
type: :
|
21
|
+
version: 2.3.0
|
22
|
+
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rspec
|
27
|
-
requirement: &3801980 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
25
|
none: false
|
29
26
|
requirements:
|
30
|
-
- -
|
27
|
+
- - ! '>='
|
31
28
|
- !ruby/object:Gem::Version
|
32
29
|
version: 2.3.0
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *3801980
|
36
30
|
- !ruby/object:Gem::Dependency
|
37
31
|
name: bundler
|
38
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
39
33
|
none: false
|
40
34
|
requirements:
|
41
|
-
- -
|
35
|
+
- - ! '>='
|
42
36
|
- !ruby/object:Gem::Version
|
43
37
|
version: 1.0.0
|
44
38
|
type: :development
|
45
39
|
prerelease: false
|
46
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
47
46
|
- !ruby/object:Gem::Dependency
|
48
47
|
name: jeweler
|
49
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
50
49
|
none: false
|
51
50
|
requirements:
|
52
|
-
- -
|
51
|
+
- - ! '>='
|
53
52
|
- !ruby/object:Gem::Version
|
54
53
|
version: 1.6.4
|
55
54
|
type: :development
|
56
55
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: rcov
|
60
|
-
requirement: &3913560 !ruby/object:Gem::Requirement
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
61
57
|
none: false
|
62
58
|
requirements:
|
63
59
|
- - ! '>='
|
64
60
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
66
|
-
type: :development
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: *3913560
|
61
|
+
version: 1.6.4
|
69
62
|
description: OptionParser with Hashie::Mash like accessibility.
|
70
63
|
email: lpm11r@gmail.com
|
71
64
|
executables: []
|
@@ -86,6 +79,7 @@ files:
|
|
86
79
|
- optparshie.gemspec
|
87
80
|
- spec/optparshie_spec.rb
|
88
81
|
- spec/spec_helper.rb
|
82
|
+
- test/test1.rb
|
89
83
|
homepage: http://github.com/lpm11/optparshie
|
90
84
|
licenses:
|
91
85
|
- WTFPL
|
@@ -101,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
95
|
version: '0'
|
102
96
|
segments:
|
103
97
|
- 0
|
104
|
-
hash: -
|
98
|
+
hash: -2936163973672300385
|
105
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
100
|
none: false
|
107
101
|
requirements:
|
@@ -110,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
104
|
version: '0'
|
111
105
|
requirements: []
|
112
106
|
rubyforge_project:
|
113
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.24
|
114
108
|
signing_key:
|
115
109
|
specification_version: 3
|
116
110
|
summary: OptionParser with Hashie::Mash like accessibility.
|