optparshie 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ 本コードは、Ruby の lib/optparse.rb から拝借したコードが一部含まれています。
2
+ 拝借した部分は Ruby ライセンス (http://www.ruby-lang.org/ja/LICENSE.txt) に
3
+ 従って利用可能です。その他の部分は以下の WTFPL に従って下さい。
4
+
5
+ ---------------------------------------------------------------------
6
+
7
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8
+ Version 2, December 2004
9
+
10
+ Copyright (C) 2011 lpm11.
11
+
12
+ Everyone is permitted to copy and distribute verbatim or modified
13
+ copies of this license document, and changing it is allowed as long
14
+ as the name is changed.
15
+
16
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
17
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
18
+
19
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = optparshie
2
+
3
+ Enhanced OptionParser, it can access to parsed argv through Hashie::Mash like.
4
+
5
+ == Example
6
+
7
+ argv = "--test --wa -a 2 --on".split(/\s+/);
8
+ puts("test argv: #{argv}");
9
+
10
+ opt = OptionParshie.new();
11
+ opt.on("-a","--a-value=VAL");
12
+ opt.on("--test");
13
+ opt.on("-w","--waros");
14
+ opt.on("-o","--on");
15
+ opt.parse(argv);
16
+
17
+ # access with short option
18
+ puts("opt.a => #{opt.a}");
19
+ puts("opt.w => #{opt.w}");
20
+ puts("opt.o => #{opt.o}");
21
+
22
+ == Copyright
23
+
24
+ Copyright (c) 2011 lpm11. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "optparshie"
18
+ gem.homepage = "http://github.com/lpm11/optparshie"
19
+ gem.license = "WTFPL"
20
+ gem.summary = %Q{OptionParser with Hashie::Mash like accessibility.}
21
+ gem.description = %Q{OptionParser with Hashie::Mash like accessibility.}
22
+ gem.email = "lpm11r@gmail.com"
23
+ gem.authors = ["lpm11"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "optparshie #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,28 @@
1
+ #!/bin/env ruby
2
+ #-*- coding: utf-8 -*-
3
+ require("../lib/optparshie");
4
+
5
+ argv = "--test --wa -a 2 --on".split(/\s+/);
6
+ puts("test argv: #{argv}");
7
+
8
+ opt = OptionParshie.new();
9
+ opt.on("-a","--a-value=VAL");
10
+ opt.on("--test");
11
+ opt.on("-w","--waros");
12
+ opt.on("-o","--on");
13
+ opt.parse(argv);
14
+
15
+ # access with short option
16
+ puts("opt.a => #{opt.a}");
17
+ puts("opt.w => #{opt.w}");
18
+ puts("opt.o => #{opt.o}");
19
+
20
+ # access with long option
21
+ puts("opt.a_value => #{opt.a_value}");
22
+ puts("opt.test => #{opt.test}");
23
+ puts("opt.waros => #{opt.waros}");
24
+ # Oops! This is already defined method....
25
+ # opt.on => Usage: example1 (blah blah blah)
26
+
27
+ # instead of you can access through hash
28
+ puts("opt.hash['on'] => #{opt.hash['on']}");
data/lib/optparshie.rb ADDED
@@ -0,0 +1,132 @@
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 = {};
10
+ end
11
+
12
+ def hash_key_sanitize(switch)
13
+ return switch.gsub(/^-+/,"").gsub(/\W/,"_");
14
+ end
15
+
16
+ def hash_sw_add(sw,val)
17
+ (sw.short+sw.long).each { |switch| @hash[hash_key_sanitize(switch)] = val; }
18
+ end
19
+
20
+ def parse(*argv)
21
+ @hash = {};
22
+ top.list.each { |sw| hash_sw_add(sw,nil); }
23
+ super;
24
+ end
25
+
26
+ def parse!(*argv)
27
+ @hash = {};
28
+ top.list.each { |sw| hash_sw_add(sw,nil); }
29
+ super;
30
+ end
31
+
32
+ # ----------------------------------------------------------------------------
33
+ # Borrowed from optparse.rb
34
+ # (author of optparse.rb is Nobu Nakada-san, thank you!)
35
+ # ----------------------------------------------------------------------------
36
+ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
37
+ opt, arg, val, rest = nil
38
+ nonopt ||= proc {|a| throw :terminate, a}
39
+ argv.unshift(arg) if arg = catch(:terminate) {
40
+ while arg = argv.shift
41
+ case arg
42
+ # long option
43
+ when /\A--([^=]*)(?:=(.*))?/m
44
+ opt, rest = $1, $2
45
+ begin
46
+ sw, = complete(:long, opt, true)
47
+ rescue ParseError
48
+ raise $!.set_option(arg, true)
49
+ end
50
+ begin
51
+ opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
52
+
53
+ # ------------------------------------------------------------------
54
+ # optparshie code by l.p.m.11
55
+ # ------------------------------------------------------------------
56
+ hash_sw_add(sw,val);
57
+
58
+ val = cb.call(val) if cb
59
+ setter.call(sw.switch_name, val) if setter
60
+ rescue ParseError
61
+ raise $!.set_option(arg, rest)
62
+ end
63
+
64
+ # short option
65
+ when /\A-(.)((=).*|.+)?/m
66
+ opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
67
+ begin
68
+ sw, = search(:short, opt)
69
+ unless sw
70
+ begin
71
+ sw, = complete(:short, opt)
72
+ # short option matched.
73
+ val = arg.sub(/\A-/, '')
74
+ has_arg = true
75
+ rescue InvalidOption
76
+ # if no short options match, try completion with long
77
+ # options.
78
+ sw, = complete(:long, opt)
79
+ eq ||= !rest
80
+ end
81
+ end
82
+ rescue ParseError
83
+ raise $!.set_option(arg, true)
84
+ end
85
+ begin
86
+ opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
87
+ raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
88
+
89
+ # ------------------------------------------------------------------
90
+ # optparshie code by l.p.m.11
91
+ # ------------------------------------------------------------------
92
+ hash_sw_add(sw,val);
93
+
94
+ argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
95
+ val = cb.call(val) if cb
96
+ setter.call(sw.switch_name, val) if setter
97
+ rescue ParseError
98
+ raise $!.set_option(arg, arg.length > 2)
99
+ end
100
+
101
+ # non-option argument
102
+ else
103
+ catch(:prune) do
104
+ visit(:each_option) do |sw0|
105
+ sw = sw0
106
+ sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
107
+ end
108
+ nonopt.call(arg)
109
+ end
110
+ end
111
+ end
112
+
113
+ nil
114
+ }
115
+
116
+ visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
117
+
118
+ argv
119
+ end
120
+
121
+ def method_missing(name,*args)
122
+ name = name.to_s();
123
+ if (@hash.key?(name))
124
+ return @hash[name];
125
+ else
126
+ super;
127
+ end
128
+ end
129
+
130
+ attr_reader :hash;
131
+ private :hash_key_sanitize, :hash_sw_add;
132
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Optparshie" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'optparshie'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optparshie
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - lpm11
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &11252080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *11252080
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &11251600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *11251600
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &11251120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *11251120
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &11250640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *11250640
58
+ description: OptionParser with Hashie::Mash like accessibility.
59
+ email: lpm11r@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - LICENSE.txt
64
+ - README.rdoc
65
+ files:
66
+ - .document
67
+ - .rspec
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - VERSION
73
+ - example/example1.rb
74
+ - lib/optparshie.rb
75
+ - spec/optparshie_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: http://github.com/lpm11/optparshie
78
+ licenses:
79
+ - WTFPL
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: 2050884602491138113
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: OptionParser with Hashie::Mash like accessibility.
105
+ test_files: []