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 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", "~> 2.3.0"
11
- gem "bundler", "~> 1.0.0"
12
- gem "jeweler", "~> 1.6.4"
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
File without changes
data/Rakefile CHANGED
File without changes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 1.2.0
@@ -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
- # instead of you can access through hash
32
- puts("opt.mash.on => #{opt.mash.on}");
31
+ # You should access via short option instead.... :X
32
+ puts("opt.o => #{opt.o}");
@@ -1,132 +1,148 @@
1
- #!/bin/env ruby
2
- #-*- coding: utf-8 -*-
3
- require("rubygems");
4
- require("optparse");
5
- require("hashie");
6
-
7
- class OptionParshie < OptionParser
8
- def initialize()
9
- super;
10
- @mash = Hashie::Mash.new();
11
- end
12
-
13
- def hash_key_sanitize(switch)
14
- return switch.gsub(/^-+/,"").gsub(/\W/,"_");
15
- end
16
-
17
- def hash_sw_add(sw,val)
18
- (sw.short+sw.long).each { |switch| @mash.send(hash_key_sanitize(switch)+"=",val); }
19
- end
20
-
21
- def parse(*argv)
22
- @mash.clear();
23
- top.list.each { |sw| hash_sw_add(sw,nil); }
24
- argv = super;
25
-
26
- return @mash;
27
- end
28
-
29
- def parse!(*argv)
30
- @mash.clear();
31
- top.list.each { |sw| hash_sw_add(sw,nil); }
32
- argv = super;
33
-
34
- return [ argv,@mash ];
35
- end
36
-
37
- # ----------------------------------------------------------------------------
38
- # Borrowed from optparse.rb
39
- # (author of optparse.rb is Nobu Nakada-san, thanks!)
40
- # ----------------------------------------------------------------------------
41
- def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
42
- opt, arg, val, rest = nil
43
- nonopt ||= proc {|a| throw :terminate, a}
44
- argv.unshift(arg) if arg = catch(:terminate) {
45
- while arg = argv.shift
46
- case arg
47
- # long option
48
- when /\A--([^=]*)(?:=(.*))?/m
49
- opt, rest = $1, $2
50
- begin
51
- sw, = complete(:long, opt, true)
52
- rescue ParseError
53
- raise $!.set_option(arg, true)
54
- end
55
- begin
56
- opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
57
-
58
- # ------------------------------------------------------------------
59
- # optparshie code by l.p.m.11
60
- # ------------------------------------------------------------------
61
- hash_sw_add(sw,val);
62
-
63
- val = cb.call(val) if cb
64
- setter.call(sw.switch_name, val) if setter
65
- rescue ParseError
66
- raise $!.set_option(arg, rest)
67
- end
68
-
69
- # short option
70
- when /\A-(.)((=).*|.+)?/m
71
- opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
72
- begin
73
- sw, = search(:short, opt)
74
- unless sw
75
- begin
76
- sw, = complete(:short, opt)
77
- # short option matched.
78
- val = arg.sub(/\A-/, '')
79
- has_arg = true
80
- rescue InvalidOption
81
- # if no short options match, try completion with long
82
- # options.
83
- sw, = complete(:long, opt)
84
- eq ||= !rest
85
- end
86
- end
87
- rescue ParseError
88
- raise $!.set_option(arg, true)
89
- end
90
- begin
91
- opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
92
- raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
93
-
94
- # ------------------------------------------------------------------
95
- # optparshie code by l.p.m.11
96
- # ------------------------------------------------------------------
97
- hash_sw_add(sw,val);
98
-
99
- argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
100
- val = cb.call(val) if cb
101
- setter.call(sw.switch_name, val) if setter
102
- rescue ParseError
103
- raise $!.set_option(arg, arg.length > 2)
104
- end
105
-
106
- # non-option argument
107
- else
108
- catch(:prune) do
109
- visit(:each_option) do |sw0|
110
- sw = sw0
111
- sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
112
- end
113
- nonopt.call(arg)
114
- end
115
- end
116
- end
117
-
118
- nil
119
- }
120
-
121
- visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
122
-
123
- argv
124
- end
125
-
126
- def method_missing(name,*args)
127
- return @mash.send(name);
128
- end
129
-
130
- attr_reader :mash;
131
- private :hash_key_sanitize, :hash_sw_add;
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
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "optparshie"
8
- s.version = "1.1.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 = "2011-11-17"
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.10"
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.add_runtime_dependency(%q<hashie>, [">= 1.2.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"])
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<hashie>, [">= 1.2.0"])
50
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
51
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
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<hashie>, [">= 1.2.0"])
57
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
58
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
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
 
File without changes
File without changes
@@ -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.1.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: 2011-11-17 00:00:00.000000000 Z
12
+ date: 2012-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: hashie
16
- requirement: &4220220 !ruby/object:Gem::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: 1.2.0
22
- type: :runtime
21
+ version: 2.3.0
22
+ type: :development
23
23
  prerelease: false
24
- version_requirements: *4220220
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: &3798960 !ruby/object:Gem::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: *3798960
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: &3919400 !ruby/object:Gem::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: *3919400
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: '0'
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: -4175335511657504025
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.10
107
+ rubygems_version: 1.8.24
114
108
  signing_key:
115
109
  specification_version: 3
116
110
  summary: OptionParser with Hashie::Mash like accessibility.