optparse_plus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +114 -0
- data/Rakefile +6 -0
- data/lib/optparse_plus/version.rb +3 -0
- data/lib/optparse_plus.rb +157 -0
- data/optparse_plus.gemspec +24 -0
- data/spec/optparse_plus_spec.rb +249 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/test_scripts/cli.rb +67 -0
- data/spec/test_scripts/main.rb +9 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c10b81ae4f69b69d772aa4485b9958f025f3bc1c
|
4
|
+
data.tar.gz: 57164625aa775c5c1c2cd151fd3be1b1aeacc48d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 73c3e461022b46a4682711eb0938c2b0851968243091017adc3c29d41e3209df0ac950efd0343b50c414bd09849ea2984025ed68502ad9e6cdf0c0015a30a220
|
7
|
+
data.tar.gz: 17ac8719eb55a7d1e9303b98d025b5c3f31e81ce627bdecbc93bfe4163f46800bf469180404d8a7d93223cafe39988e0e2f46054527fd07908bfe18c84b2f432
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 HASHIMOTO, Naoki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# OptparsePlus
|
2
|
+
|
3
|
+
OptParsePlus adds some helper methods to OptionParser, and let you define command line options more easily.
|
4
|
+
|
5
|
+
In your script, simply require 'optparse_plus' in stead of 'optparse', then the new methods of OptionParser are available for you.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'optparse_plus'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install optparse_plus
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
For example, save the following script as `test_program.rb`:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
#!/usr/bin/env ruby
|
27
|
+
|
28
|
+
require 'optparse_plus'
|
29
|
+
|
30
|
+
first_value, second_value = nil, nil
|
31
|
+
|
32
|
+
OptionParser.new_with_yaml do |opt|
|
33
|
+
opt.on(:first_option) {|status| first_value = status }
|
34
|
+
opt.on(:second_option) {|arg| second_value = arg }
|
35
|
+
opt.parse!
|
36
|
+
end
|
37
|
+
|
38
|
+
puts "The value returned from the first option is '#{first_value}'"
|
39
|
+
puts "The value returned from the second option is '#{second_value}'"
|
40
|
+
|
41
|
+
__END__
|
42
|
+
banner: test_program.rb [OPTION]
|
43
|
+
first_option:
|
44
|
+
short: -f
|
45
|
+
long: --first
|
46
|
+
description: First option for a test script
|
47
|
+
second_option:
|
48
|
+
short: -s [arg]
|
49
|
+
long: --second [=arg]
|
50
|
+
description: Second option for a test script
|
51
|
+
```
|
52
|
+
|
53
|
+
And if you execute:
|
54
|
+
|
55
|
+
$ ruby test_program.rb --help
|
56
|
+
|
57
|
+
You will see the following message:
|
58
|
+
|
59
|
+
```
|
60
|
+
test_program [OPTION]
|
61
|
+
-E, --encoding=ex[:in] specify the default external and internal character encodings
|
62
|
+
-f, --first First option for a test script
|
63
|
+
-s, --second [=arg] Second option for a test script
|
64
|
+
```
|
65
|
+
|
66
|
+
Or if you execute:
|
67
|
+
|
68
|
+
$ ruby test_program.rb --first --second=something
|
69
|
+
|
70
|
+
You will get the following result:
|
71
|
+
|
72
|
+
```
|
73
|
+
The value returned from the first option is 'true'
|
74
|
+
The value returned from the second option is 'something'
|
75
|
+
```
|
76
|
+
|
77
|
+
You can also pass YAML data as a string:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
#!/usr/bin/env ruby
|
81
|
+
|
82
|
+
require 'optparse_plus'
|
83
|
+
|
84
|
+
first_value, second_value = nil, nil
|
85
|
+
|
86
|
+
config_yaml =<<YAML
|
87
|
+
banner: #{File.basename($0)} [OPTION]
|
88
|
+
first_option:
|
89
|
+
short: -f
|
90
|
+
long: --first
|
91
|
+
description: First option for a test script
|
92
|
+
second_option:
|
93
|
+
short: -s [arg]
|
94
|
+
long: --second [=arg]
|
95
|
+
description: Second option for a test script
|
96
|
+
YAML
|
97
|
+
|
98
|
+
OptionParser.new_with_yaml(config_yaml) do |opt|
|
99
|
+
opt.on(:first_option) {|status| first_value = status }
|
100
|
+
opt.on(:second_option) {|arg| second_value = arg }
|
101
|
+
opt.parse!
|
102
|
+
end
|
103
|
+
|
104
|
+
puts "The value returned from the first option is '#{first_value}'"
|
105
|
+
puts "The value returned from the second option is '#{second_value}'"
|
106
|
+
```
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
1. Fork it
|
111
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
112
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
113
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
114
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
require "optparse_plus/version"
|
2
|
+
require 'optparse'
|
3
|
+
require 'yaml'
|
4
|
+
require 'ripper'
|
5
|
+
|
6
|
+
module OptparsePlus
|
7
|
+
attr_reader :opt_plus
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def read_after_program_end(cur_file)
|
11
|
+
own_source = File.read(cur_file)
|
12
|
+
last_token = Ripper.lex(own_source)[-1]
|
13
|
+
return nil unless last_token[1] == :on___end__
|
14
|
+
start = last_token[0][0]
|
15
|
+
own_source.lines[start..-1].join
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_with_yaml(config_yaml_source=nil)
|
19
|
+
cur_file = caller[0].split(/:/)[0]
|
20
|
+
config_yaml_source ||= read_after_program_end(cur_file)
|
21
|
+
opt = OptPlus.create_opt(config_yaml_source)
|
22
|
+
|
23
|
+
if block_given?
|
24
|
+
yield opt
|
25
|
+
else
|
26
|
+
opt
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class OptPlus
|
32
|
+
attr_reader :config, :config_source, :callbacks
|
33
|
+
attr_writer :opt
|
34
|
+
|
35
|
+
ruby_options_yaml_source = <<YAML
|
36
|
+
optplus_ruby_encoding:
|
37
|
+
short: -Eex[:in]
|
38
|
+
long: --encoding=ex[:in]
|
39
|
+
description: specify the default external and internal character encodings
|
40
|
+
optplus_ruby_debug:
|
41
|
+
short: -d
|
42
|
+
long: --debug
|
43
|
+
description: set debugging flags (set $DEBUG to true)
|
44
|
+
YAML
|
45
|
+
|
46
|
+
RUBY_OPTIONS = YAML.load(ruby_options_yaml_source)
|
47
|
+
RUBY_OPTION_TO_LABEL = {
|
48
|
+
"E" => :optplus_ruby_encoding,
|
49
|
+
"d" => :optplus_ruby_debug,
|
50
|
+
}
|
51
|
+
|
52
|
+
def self.create_opt(config_yaml_source)
|
53
|
+
opt_plus = OptPlus.new(config_yaml_source)
|
54
|
+
|
55
|
+
if banner = opt_plus.config["banner"]
|
56
|
+
opt = ::OptionParser.new(banner)
|
57
|
+
else
|
58
|
+
opt = ::OptionParser.new
|
59
|
+
end
|
60
|
+
|
61
|
+
opt.setup_opt_plus(opt_plus)
|
62
|
+
opt
|
63
|
+
end
|
64
|
+
|
65
|
+
def initialize(config_yaml_source)
|
66
|
+
@config_source = config_yaml_source
|
67
|
+
@config = YAML.load(config_yaml_source)
|
68
|
+
@ruby_option_callbacks = ruby_option_callbacks
|
69
|
+
@callbacks = {}
|
70
|
+
@opt = nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def opt_on
|
74
|
+
@callbacks.keys.each {|label| reflect_callback(label) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def reflect_callback(label)
|
78
|
+
callback = @callbacks[label]
|
79
|
+
args = config_to_args(label)
|
80
|
+
@opt.on(*args, callback)
|
81
|
+
end
|
82
|
+
|
83
|
+
def config_to_args(label, config=@config)
|
84
|
+
options = config[label.to_s]
|
85
|
+
%w(short long desc description).map {|type| options[type] }
|
86
|
+
.select {|option| not option.nil? }.flatten
|
87
|
+
end
|
88
|
+
|
89
|
+
def inherit_ruby_options(*short_option_names)
|
90
|
+
short_option_names.each do |opt_name|
|
91
|
+
label = RUBY_OPTION_TO_LABEL[opt_name]
|
92
|
+
args = config_to_args(label, RUBY_OPTIONS)
|
93
|
+
callback = @ruby_option_callbacks[label]
|
94
|
+
@opt.on(*args, callback)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def ruby_set_encoding(given_opt)
|
101
|
+
external, internal = given_opt.split(/:/o, 2)
|
102
|
+
Encoding.default_external = external if external and not external.empty?
|
103
|
+
Encoding.default_internal = internal if internal and not internal.empty?
|
104
|
+
end
|
105
|
+
|
106
|
+
def ruby_set_debug
|
107
|
+
$DEBUG = true
|
108
|
+
end
|
109
|
+
|
110
|
+
def ruby_option_callbacks
|
111
|
+
{}.tap do |callbacks|
|
112
|
+
callbacks[:optplus_ruby_encoding] = proc {|given_opt| ruby_set_encoding(given_opt) }
|
113
|
+
callbacks[:optplus_ruby_debug] = proc {|given_opt| ruby_set_debug }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def with(option_label, &callback)
|
119
|
+
@opt_plus.callbacks[option_label] = callback
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
def on_with(option_label, &callback)
|
124
|
+
args = @opt_plus.config_to_args(option_label)
|
125
|
+
_orig_on(*args, callback)
|
126
|
+
end
|
127
|
+
|
128
|
+
def on(*args, &callback)
|
129
|
+
if args.length == 1 and args[0].kind_of? Symbol
|
130
|
+
on_with(*args, &callback)
|
131
|
+
else
|
132
|
+
super(*args)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def parse!
|
137
|
+
@opt_plus.opt_on
|
138
|
+
super
|
139
|
+
end
|
140
|
+
|
141
|
+
def setup_opt_plus(opt_plus)
|
142
|
+
@opt_plus = opt_plus
|
143
|
+
opt_plus.opt = self
|
144
|
+
end
|
145
|
+
|
146
|
+
def inherit_ruby_options(*short_option_names)
|
147
|
+
@opt_plus.inherit_ruby_options(*short_option_names)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class OptionParser
|
152
|
+
alias :_orig_on :on
|
153
|
+
private :_orig_on
|
154
|
+
prepend OptparsePlus
|
155
|
+
extend OptparsePlus::ClassMethods
|
156
|
+
private_class_method :read_after_program_end
|
157
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'optparse_plus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "optparse_plus"
|
8
|
+
spec.version = OptparsePlus::VERSION
|
9
|
+
spec.authors = ["HASHIMOTO, Naoki"]
|
10
|
+
spec.email = ["hashimoto.naoki@gmail.com"]
|
11
|
+
spec.description = %q{"optparse_plus" will let you define command line options more easily.}
|
12
|
+
spec.summary = %q{"optparse_plus" adds some helper methods to OptionParser. In your script, simply require 'optparse_plus' in stead of 'optparse', then the new methods of OptionParser are available for you.}
|
13
|
+
spec.homepage = "https://github.com/nico-hn/optparse_plus"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,249 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require File.join(__dir__, 'test_scripts/main')
|
3
|
+
|
4
|
+
describe OptparsePlus do
|
5
|
+
it 'should have a version number' do
|
6
|
+
expect(OptparsePlus::VERSION).to_not be_nil
|
7
|
+
end
|
8
|
+
|
9
|
+
context("when options are described under __END__") do
|
10
|
+
it 'accepts --second option with an argument' do
|
11
|
+
parser = OptparsePlusTest.new
|
12
|
+
set_argv("--first --second=something")
|
13
|
+
parser.parse_with_after_end
|
14
|
+
expect(parser.second_option_value).to eq("something")
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts --first option' do
|
18
|
+
parser = OptparsePlusTest.new
|
19
|
+
set_argv("--first --second=something")
|
20
|
+
parser.parse_with_after_end
|
21
|
+
expect(parser.first_option_given?).to be_truthy
|
22
|
+
end
|
23
|
+
|
24
|
+
it '--second option may not be given' do
|
25
|
+
parser = OptparsePlusTest.new
|
26
|
+
set_argv("--first")
|
27
|
+
parser.parse_with_after_end
|
28
|
+
expect(parser.second_option_value).to be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it '--first option may not be given' do
|
32
|
+
parser = OptparsePlusTest.new
|
33
|
+
set_argv("--second=something")
|
34
|
+
parser.parse_with_after_end
|
35
|
+
expect(parser.first_option_given?).to be_falsy
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context("when options are described in a multi-lines string") do
|
40
|
+
it 'accepts --second option with an argument' do
|
41
|
+
parser = OptparsePlusTest.new
|
42
|
+
set_argv("--first --second=something")
|
43
|
+
parser.parse_with_string_as_source
|
44
|
+
expect(parser.second_option_value).to eq("something")
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'accepts --first option' do
|
48
|
+
parser = OptparsePlusTest.new
|
49
|
+
set_argv("--first --second=something")
|
50
|
+
parser.parse_with_string_as_source
|
51
|
+
expect(parser.first_option_given?).to be_truthy
|
52
|
+
end
|
53
|
+
|
54
|
+
it '--second option may not be given' do
|
55
|
+
parser = OptparsePlusTest.new
|
56
|
+
set_argv("--first")
|
57
|
+
parser.parse_with_string_as_source
|
58
|
+
expect(parser.second_option_value).to be_nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it '--first option may not be given' do
|
62
|
+
parser = OptparsePlusTest.new
|
63
|
+
set_argv("--second=something")
|
64
|
+
parser.parse_with_string_as_source
|
65
|
+
expect(parser.first_option_given?).to be_falsy
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context("when options are registered using OptionParser#on_with") do
|
70
|
+
it 'accepts --second option with an argument' do
|
71
|
+
parser = OptparsePlusTest.new
|
72
|
+
set_argv("--first --second=something")
|
73
|
+
parser.parse_using_on_with
|
74
|
+
expect(parser.second_option_value).to eq("something")
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'accepts --first option' do
|
78
|
+
parser = OptparsePlusTest.new
|
79
|
+
set_argv("--first --second=something")
|
80
|
+
parser.parse_using_on_with
|
81
|
+
expect(parser.first_option_given?).to be_truthy
|
82
|
+
end
|
83
|
+
|
84
|
+
it '--second option may not be given' do
|
85
|
+
parser = OptparsePlusTest.new
|
86
|
+
set_argv("--first")
|
87
|
+
parser.parse_using_on_with
|
88
|
+
expect(parser.second_option_value).to be_nil
|
89
|
+
end
|
90
|
+
|
91
|
+
it '--first option may not be given' do
|
92
|
+
parser = OptparsePlusTest.new
|
93
|
+
set_argv("--second=something")
|
94
|
+
parser.parse_using_on_with
|
95
|
+
expect(parser.first_option_given?).to be_falsy
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context("when options are registered using OptionParser#on") do
|
100
|
+
it 'accepts --second option with an argument' do
|
101
|
+
parser = OptparsePlusTest.new
|
102
|
+
set_argv("--first --second=something")
|
103
|
+
parser.parse_using_on
|
104
|
+
expect(parser.second_option_value).to eq("something")
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'accepts --first option' do
|
108
|
+
parser = OptparsePlusTest.new
|
109
|
+
set_argv("--first --second=something")
|
110
|
+
parser.parse_using_on
|
111
|
+
expect(parser.first_option_given?).to be_truthy
|
112
|
+
end
|
113
|
+
|
114
|
+
it '--second option may not be given' do
|
115
|
+
parser = OptparsePlusTest.new
|
116
|
+
set_argv("--first")
|
117
|
+
parser.parse_using_on
|
118
|
+
expect(parser.second_option_value).to be_nil
|
119
|
+
end
|
120
|
+
|
121
|
+
it '--first option may not be given' do
|
122
|
+
parser = OptparsePlusTest.new
|
123
|
+
set_argv("--second=something")
|
124
|
+
parser.parse_using_on
|
125
|
+
expect(parser.first_option_given?).to be_falsy
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'creation of an instance of OptionParser' do
|
130
|
+
before do
|
131
|
+
@config_yaml_with_banner = <<YAML
|
132
|
+
banner: #{File.basename($0)} [OPTION]
|
133
|
+
first_option:
|
134
|
+
long: --first
|
135
|
+
short: -f
|
136
|
+
description: First option for a test script
|
137
|
+
second_option:
|
138
|
+
short: -s [arg]
|
139
|
+
description: Second option for a test script
|
140
|
+
YAML
|
141
|
+
|
142
|
+
@config_yaml_without_banner = <<YAML
|
143
|
+
first_option:
|
144
|
+
short: -f
|
145
|
+
long: --first
|
146
|
+
description: First option for a test script
|
147
|
+
second_option:
|
148
|
+
short: -s [arg]
|
149
|
+
long: --second [=arg]
|
150
|
+
description: Second option for a test script
|
151
|
+
YAML
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'expects to assign OptionParser#banner if the value is in the configuration' do
|
155
|
+
opt = OptionParser::OptPlus.create_opt(@config_yaml_with_banner)
|
156
|
+
expect(opt.banner).to eq('rspec [OPTION]')
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'expects to assign a default value to OptionParser#banner unless the value is specified in the configuration' do
|
160
|
+
opt = OptionParser::OptPlus.create_opt(@config_yaml_without_banner)
|
161
|
+
expect(opt.banner).to eq('Usage: rspec [options]')
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'expects to assign an instance of OptPlus to OptionParser#opt_plus' do
|
165
|
+
opt = OptionParser::OptPlus.create_opt(@config_yaml_without_banner)
|
166
|
+
expect(opt.opt_plus).to be_instance_of(OptionParser::OptPlus)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe OptionParser::OptPlus do
|
171
|
+
before do
|
172
|
+
@config_yaml_with_banner = <<YAML
|
173
|
+
banner: #{File.basename($0)} [OPTION]
|
174
|
+
first_option:
|
175
|
+
long: --first
|
176
|
+
short: -f
|
177
|
+
description: First option for a test script
|
178
|
+
second_option:
|
179
|
+
short: -s [arg]
|
180
|
+
description: Second option for a test script
|
181
|
+
YAML
|
182
|
+
|
183
|
+
@config_yaml_without_banner = <<YAML
|
184
|
+
first_option:
|
185
|
+
short: -f
|
186
|
+
long: --first
|
187
|
+
description: First option for a test script
|
188
|
+
second_option:
|
189
|
+
short: -s [arg]
|
190
|
+
long: --second [=arg]
|
191
|
+
description: Second option for a test script
|
192
|
+
YAML
|
193
|
+
end
|
194
|
+
|
195
|
+
describe '#config_to_args' do
|
196
|
+
it 'can convert config in yaml to args of OptionParser#on' do
|
197
|
+
opt_plus = OptionParser::OptPlus.new(@config_yaml_without_banner)
|
198
|
+
args = opt_plus.config_to_args(:first_option)
|
199
|
+
expect(args).to eq(["-f", "--first", "First option for a test script"])
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'expects to sort the order of arguments (short long desc)' do
|
203
|
+
opt_plus = OptionParser::OptPlus.new(@config_yaml_with_banner)
|
204
|
+
args = opt_plus.config_to_args(:first_option)
|
205
|
+
expect(args).to eq(["-f", "--first", "First option for a test script"])
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'may ignore missing arguments' do
|
209
|
+
opt_plus = OptionParser::OptPlus.new(@config_yaml_with_banner)
|
210
|
+
args = opt_plus.config_to_args(:second_option)
|
211
|
+
expect(args).to eq(["-s [arg]", "Second option for a test script"])
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
describe '#inherit_ruby_options' do
|
216
|
+
it 'can add -E option' do
|
217
|
+
external_encoding = Encoding.default_external
|
218
|
+
internal_encoding = Encoding.default_internal
|
219
|
+
|
220
|
+
Encoding.default_external = 'UTF-8'
|
221
|
+
expect(Encoding.default_external).to eq(Encoding::UTF_8)
|
222
|
+
|
223
|
+
set_argv("-EWindows-31J")
|
224
|
+
OptionParser.new_with_yaml(@config_yaml_with_banner) do |opt|
|
225
|
+
opt.inherit_ruby_options("E")
|
226
|
+
opt.parse!
|
227
|
+
end
|
228
|
+
|
229
|
+
expect(Encoding.default_external).to eq(Encoding::Windows_31J)
|
230
|
+
Encoding.default_external = external_encoding
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'expect to print help message' do
|
234
|
+
expected_help_message = <<HELP
|
235
|
+
rspec [OPTION]
|
236
|
+
-E, --encoding=ex[:in] specify the default external and internal character encodings
|
237
|
+
HELP
|
238
|
+
|
239
|
+
allow(STDOUT).to receive(:puts).with(expected_help_message)
|
240
|
+
|
241
|
+
set_argv("--help")
|
242
|
+
OptionParser.new_with_yaml(@config_yaml_with_banner) do |opt|
|
243
|
+
opt.inherit_ruby_options("E")
|
244
|
+
opt.parse!
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'shellwords'
|
3
|
+
require 'optparse_plus'
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
def set_argv(command_line_str)
|
7
|
+
ARGV.replace Shellwords.split(command_line_str)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
RSpec.configure do |c|
|
12
|
+
c.include Helpers
|
13
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse_plus'
|
4
|
+
|
5
|
+
class OptparsePlusTest
|
6
|
+
attr_accessor :first_option_given, :second_option_value
|
7
|
+
|
8
|
+
OPTION_YAML_SOURCE = <<YAML
|
9
|
+
banner: #{File.basename($0)} [OPTION]
|
10
|
+
first_option:
|
11
|
+
short: -f
|
12
|
+
long: --first
|
13
|
+
description: First option for a test script
|
14
|
+
second_option:
|
15
|
+
short: -s [arg]
|
16
|
+
long: --second [=arg]
|
17
|
+
description: Second option for a test script
|
18
|
+
YAML
|
19
|
+
|
20
|
+
def parse_with_after_end
|
21
|
+
OptionParser.new_with_yaml do |opt|
|
22
|
+
opt.with(:first_option) {|status| @first_option_given = status }
|
23
|
+
opt.with(:second_option) {|arg| @second_option_value = arg }
|
24
|
+
opt.parse!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_with_string_as_source
|
29
|
+
OptionParser.new_with_yaml(OPTION_YAML_SOURCE) do |opt|
|
30
|
+
opt.with(:first_option) {|status| @first_option_given = status }
|
31
|
+
opt.with(:second_option) {|arg| @second_option_value = arg }
|
32
|
+
opt.parse!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def parse_using_on_with
|
37
|
+
OptionParser.new_with_yaml(OPTION_YAML_SOURCE) do |opt|
|
38
|
+
opt.on_with(:first_option) {|status| @first_option_given = status }
|
39
|
+
opt.on_with(:second_option) {|arg| @second_option_value = arg }
|
40
|
+
opt.parse!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_using_on
|
45
|
+
OptionParser.new_with_yaml(OPTION_YAML_SOURCE) do |opt|
|
46
|
+
opt.on('-f', '--first',
|
47
|
+
'First option for a test script') {|status| @first_option_given = status }
|
48
|
+
# opt.on(:first_option) {|status| @first_option_given = status }
|
49
|
+
|
50
|
+
# opt.on('-s [arg]', '--second [=arg]',
|
51
|
+
# 'Second option for a test script') {|arg| @second_option_value = arg }
|
52
|
+
opt.on(:second_option) {|arg| @second_option_value = arg }
|
53
|
+
opt.parse!
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
__END__
|
59
|
+
banner: test_program [OPTION]
|
60
|
+
first_option:
|
61
|
+
short: -f
|
62
|
+
long: --first
|
63
|
+
description: First option for a test script
|
64
|
+
second_option:
|
65
|
+
short: -s [arg]
|
66
|
+
long: --second [=arg]
|
67
|
+
description: Second option for a test script
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optparse_plus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- HASHIMOTO, Naoki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: "\"optparse_plus\" will let you define command line options more easily."
|
56
|
+
email:
|
57
|
+
- hashimoto.naoki@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/optparse_plus.rb
|
70
|
+
- lib/optparse_plus/version.rb
|
71
|
+
- optparse_plus.gemspec
|
72
|
+
- spec/optparse_plus_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/test_scripts/cli.rb
|
75
|
+
- spec/test_scripts/main.rb
|
76
|
+
homepage: https://github.com/nico-hn/optparse_plus
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: "\"optparse_plus\" adds some helper methods to OptionParser. In your script,
|
100
|
+
simply require 'optparse_plus' in stead of 'optparse', then the new methods of OptionParser
|
101
|
+
are available for you."
|
102
|
+
test_files:
|
103
|
+
- spec/optparse_plus_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- spec/test_scripts/cli.rb
|
106
|
+
- spec/test_scripts/main.rb
|