acclaim 0.0.5 → 0.0.6
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/Gemfile +8 -0
- data/README.markdown +3 -3
- data/acclaim.gemspec +2 -0
- data/lib/acclaim/command.rb +18 -20
- data/lib/acclaim/command/parser.rb +51 -0
- data/lib/acclaim/option.rb +2 -2
- data/lib/acclaim/option/parser.rb +38 -34
- data/lib/acclaim/option/parser/regexp.rb +7 -5
- data/lib/acclaim/option/type.rb +1 -0
- data/lib/acclaim/option/type/string.rb +0 -1
- data/lib/acclaim/option/type/symbol.rb +21 -0
- data/lib/acclaim/version.rb +1 -1
- data/spec/acclaim/option_spec.rb +279 -4
- metadata +25 -7
- data/lib/acclaim/option/values.rb +0 -49
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -82,10 +82,10 @@ from an existing command:
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
$ app do x
|
86
|
-
Doing something with x
|
85
|
+
$ app do x y, z
|
86
|
+
Doing something with x y, z
|
87
87
|
|
88
|
-
$ app do --what x
|
88
|
+
$ app do --what x y, z
|
89
89
|
Doing x with y, z
|
90
90
|
|
91
91
|
Options may also take an Hash as the last parameter. Among the things that can
|
data/acclaim.gemspec
CHANGED
data/lib/acclaim/command.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'acclaim/command/help'
|
2
|
+
require 'acclaim/command/parser'
|
2
3
|
require 'acclaim/command/version'
|
3
4
|
require 'acclaim/option'
|
4
5
|
require 'acclaim/option/parser'
|
5
6
|
require 'acclaim/option/parser/regexp'
|
6
|
-
require '
|
7
|
+
require 'ribbon'
|
7
8
|
|
8
9
|
module Acclaim
|
9
10
|
|
@@ -71,7 +72,7 @@ module Acclaim
|
|
71
72
|
alias :opt :option
|
72
73
|
|
73
74
|
# The block which is executed when this command is called. It is given 2
|
74
|
-
# parameters; the first is an
|
75
|
+
# parameters; the first is an Ribbon::Object instance which can be queried
|
75
76
|
# for settings information; the second is the remaining command line.
|
76
77
|
def action(&block)
|
77
78
|
@action = block
|
@@ -94,9 +95,14 @@ module Acclaim
|
|
94
95
|
Option::Parser.new(args, options).parse!
|
95
96
|
end
|
96
97
|
|
98
|
+
# Looks for this command's subcommands in the argument array.
|
99
|
+
def parse_subcommands!(args)
|
100
|
+
Command::Parser.new(args, subcommands).parse!
|
101
|
+
end
|
102
|
+
|
97
103
|
# Invokes this command with a fresh set of option values.
|
98
104
|
def run(*args)
|
99
|
-
invoke
|
105
|
+
invoke Ribbon::Object.new, args
|
100
106
|
rescue Option::Parser::Error => e
|
101
107
|
puts e.message
|
102
108
|
end
|
@@ -106,14 +112,16 @@ module Acclaim
|
|
106
112
|
# will be executed. A subcommand may be anywhere in the array as long as
|
107
113
|
# it is before an argument separator, which is tipically a double dash
|
108
114
|
# (<tt>--<\tt>) and may be omitted.
|
115
|
+
#
|
116
|
+
# All argument separators will be deleted from the argument array before a
|
117
|
+
# command is executed.
|
109
118
|
def invoke(opts, args = [])
|
110
|
-
|
119
|
+
Ribbon::Object.merge! opts, parse_options!(args)
|
111
120
|
handle_special_options! opts, args
|
112
|
-
if subcommand =
|
113
|
-
args.delete subcommand.line
|
121
|
+
if subcommand = parse_subcommands!(args)
|
114
122
|
subcommand.invoke(opts, args)
|
115
123
|
else
|
116
|
-
args
|
124
|
+
delete_argument_separators_in! args
|
117
125
|
execute(opts, args)
|
118
126
|
end
|
119
127
|
end
|
@@ -146,22 +154,12 @@ module Acclaim
|
|
146
154
|
const_get(:Version).execute opts, args if opts.acclaim_version?
|
147
155
|
end
|
148
156
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
def find_subcommand_in(args)
|
153
|
-
subcommands.find do |subcommand|
|
154
|
-
args.include? subcommand.line
|
157
|
+
def delete_argument_separators_in!(args)
|
158
|
+
args.delete_if do |arg|
|
159
|
+
arg =~ Option::Parser::Regexp::ARGUMENT_SEPARATOR
|
155
160
|
end
|
156
161
|
end
|
157
162
|
|
158
|
-
# Finds the argument separator and returns an array containing all the
|
159
|
-
# elements before it. If a separator is not present, the original array
|
160
|
-
# is returned.
|
161
|
-
def separated(args)
|
162
|
-
args.take_while { |arg| arg !~ Option::Parser::Regexp::ARGUMENT_SEPARATOR }
|
163
|
-
end
|
164
|
-
|
165
163
|
end
|
166
164
|
|
167
165
|
# Add the class methods to the subclass and add it to this command's list of
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'acclaim/option/parser/regexp'
|
2
|
+
|
3
|
+
module Acclaim
|
4
|
+
class Command
|
5
|
+
|
6
|
+
# Given an argument array and a list of commands, searches for them among
|
7
|
+
# the elements of the array.
|
8
|
+
class Parser
|
9
|
+
|
10
|
+
attr_accessor :argv, :commands
|
11
|
+
|
12
|
+
# Initializes a new command parser, with the given argument array and set
|
13
|
+
# of commands to search for.
|
14
|
+
def initialize(argv, commands)
|
15
|
+
self.argv = argv
|
16
|
+
self.commands = commands
|
17
|
+
end
|
18
|
+
|
19
|
+
# Parses the argument array and returns one of the given commands, if one
|
20
|
+
# is found, or +nil+ otherwise.
|
21
|
+
def parse!
|
22
|
+
find_command
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Discards all elements in the argument array after and including the
|
28
|
+
# argument separator, if one exists.
|
29
|
+
#
|
30
|
+
# Does not modify +argv+; returns a new array.
|
31
|
+
def arguments_up_to_separator
|
32
|
+
argv.take_while do |arg|
|
33
|
+
arg !~ Option::Parser::Regexp::ARGUMENT_SEPARATOR
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Searches for one of the given commands in the argument array, and
|
38
|
+
# returns it. Removes the string that matched the command name from
|
39
|
+
# +argv+. Returns +nil if no command was found.
|
40
|
+
def find_command
|
41
|
+
commands.find do |command|
|
42
|
+
arguments_up_to_separator.include? command.line
|
43
|
+
end.tap do |command|
|
44
|
+
argv.delete command.line if command
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/acclaim/option.rb
CHANGED
@@ -32,7 +32,7 @@ module Acclaim
|
|
32
32
|
# line.
|
33
33
|
#
|
34
34
|
# Additionally, if a block is given, it will be called when the option is
|
35
|
-
# parsed with a
|
35
|
+
# parsed with a ribbon instance and the parameters given to the option. The
|
36
36
|
# parameters will already be converted to this option's specified type; if
|
37
37
|
# this is not desirable consider not specifying a class to the option or
|
38
38
|
# registering a custom type handler.
|
@@ -41,7 +41,7 @@ module Acclaim
|
|
41
41
|
matches = args.select { |arg| arg.is_a? String }.group_by do |arg|
|
42
42
|
arg =~ Parser::Regexp::SWITCH ? true : false
|
43
43
|
end
|
44
|
-
klass = args.find { |arg| arg.is_a?
|
44
|
+
klass = args.find { |arg| arg.is_a? Module }
|
45
45
|
self.key = key
|
46
46
|
self.names = matches.fetch true, []
|
47
47
|
self.description = matches.fetch(false, []).first
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'acclaim/option/parser/regexp'
|
2
|
-
require '
|
2
|
+
require 'ribbon'
|
3
3
|
|
4
4
|
module Acclaim
|
5
5
|
class Option
|
@@ -51,7 +51,7 @@ module Acclaim
|
|
51
51
|
# options << Option.new(:verbose, '--verbose')
|
52
52
|
#
|
53
53
|
# Option::Parser.new(args, options).parse!
|
54
|
-
# =>
|
54
|
+
# => { Ribbon file:log.txt, verbose:true }
|
55
55
|
#
|
56
56
|
# args
|
57
57
|
# => ["arg1", "arg2"]
|
@@ -98,28 +98,28 @@ module Acclaim
|
|
98
98
|
end
|
99
99
|
|
100
100
|
# Parses the options and their arguments, associating that information
|
101
|
-
# with a
|
101
|
+
# with a Ribbon::Object instance.
|
102
102
|
def parse_values!
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
else
|
113
|
-
switches.each do |switch|
|
114
|
-
params = extract_parameters_of! option, switch
|
115
|
-
set_option_value option, values, params
|
116
|
-
end
|
117
|
-
end
|
103
|
+
ribbon = Ribbon::Object.new
|
104
|
+
options.each do |option|
|
105
|
+
key = option.key
|
106
|
+
ribbon[key] = option.default unless ribbon[key]
|
107
|
+
switches = argv.find_all { |switch| option =~ switch }
|
108
|
+
if switches.any?
|
109
|
+
if option.flag?
|
110
|
+
found_boolean option, ribbon
|
111
|
+
argv.delete *switches
|
118
112
|
else
|
119
|
-
|
113
|
+
switches.each do |switch|
|
114
|
+
params = extract_parameters_of! option, switch
|
115
|
+
found_params_for option, ribbon, params
|
116
|
+
end
|
120
117
|
end
|
118
|
+
else
|
119
|
+
Error.raise_missing_arg(option.names.join ' | ') if option.required?
|
121
120
|
end
|
122
121
|
end
|
122
|
+
ribbon
|
123
123
|
end
|
124
124
|
|
125
125
|
# Finds the +switch+ in #argv and scans the next +option.arity.total+
|
@@ -154,26 +154,30 @@ module Acclaim
|
|
154
154
|
values.each { |value| argv.delete value }
|
155
155
|
end
|
156
156
|
|
157
|
-
# If the option has an custom handler associated,
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
# +params+ if it
|
162
|
-
|
157
|
+
# If the option has an custom handler associated, it will be called with
|
158
|
+
# the option values as the first argument and the array of parameters
|
159
|
+
# found as the second argument. Otherwise, the value will be set to
|
160
|
+
# <tt>params.first</tt>, if the option takes only one argument, or to
|
161
|
+
# +params+ if it takes more.
|
162
|
+
#
|
163
|
+
# The parameters will be converted according to the option's type.
|
164
|
+
def found_params_for(option, values, params = [])
|
163
165
|
params = option.convert_parameters *params
|
164
|
-
if handler = option.handler
|
165
|
-
if option.flag? then handler.call values
|
166
|
-
else handler.call values, params end
|
166
|
+
if handler = option.handler then handler.call values, params
|
167
167
|
else
|
168
|
-
|
169
|
-
|
170
|
-
else
|
171
|
-
value = option.arity.total == 1 ? params.first : params
|
172
|
-
values[key] = value unless params.empty?
|
173
|
-
end
|
168
|
+
value = option.arity.total == 1 ? params.first : params
|
169
|
+
values[option.key.to_sym] = value unless params.empty?
|
174
170
|
end
|
175
171
|
end
|
176
172
|
|
173
|
+
# If the option has an custom handler associated, it will be called with
|
174
|
+
# only the option values as the first argument. Otherwise, the value will
|
175
|
+
# be set to <tt>true</tt>.
|
176
|
+
def found_boolean(option, values)
|
177
|
+
if handler = option.handler then handler.call values
|
178
|
+
else values[option.key.to_sym] = true end
|
179
|
+
end
|
180
|
+
|
177
181
|
end
|
178
182
|
end
|
179
183
|
end
|
@@ -8,12 +8,14 @@ module Acclaim
|
|
8
8
|
# Regular expression for a short option switch.
|
9
9
|
#
|
10
10
|
# Matches strings that begin with a single dash and contains only one
|
11
|
-
# word character
|
11
|
+
# word character before the end of the string.
|
12
12
|
#
|
13
|
-
# Examples: <tt>-s; -
|
13
|
+
# Examples: <tt>-s; -_</tt>
|
14
14
|
#
|
15
|
-
# <tt>'-mult'</tt>
|
16
|
-
|
15
|
+
# <tt>'-mult'</tt> should match MULTIPLE_SHORT_SWITCHES, and will be
|
16
|
+
# split into <tt>%w(-m -u -l -t)</tt>, which in turn should match this
|
17
|
+
# regular expression.
|
18
|
+
SHORT_SWITCH = /\A-\w\Z/
|
17
19
|
|
18
20
|
# Regular expression for a long option switch.
|
19
21
|
#
|
@@ -58,7 +60,7 @@ module Acclaim
|
|
58
60
|
# <tt>'--weird=,PARAM2'</tt> will become
|
59
61
|
# <tt>['--weird', '', 'PARAM2']</tt> when it is split up. What to make
|
60
62
|
# of those isn't a decision for a preprocessor.
|
61
|
-
SWITCH_PARAM_EQUALS = /\A--[\w\d]+(
|
63
|
+
SWITCH_PARAM_EQUALS = /\A--[\w\d]+(-[\w\d]+)*=(,*[\w\d]*)*\Z/
|
62
64
|
|
63
65
|
# Regular expression for any kind of option switch.
|
64
66
|
#
|
data/lib/acclaim/option/type.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'acclaim/option/type'
|
2
|
+
|
3
|
+
module Acclaim
|
4
|
+
class Option
|
5
|
+
module Type
|
6
|
+
|
7
|
+
# Handles symbols given as arguments in the command line.
|
8
|
+
module Symbol
|
9
|
+
|
10
|
+
# Simply returns +str.to_sym+.
|
11
|
+
def self.handle(str)
|
12
|
+
str.to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
self.accept ::Symbol, &Symbol.method(:handle)
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/acclaim/version.rb
CHANGED
data/spec/acclaim/option_spec.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
1
|
require 'acclaim/option'
|
2
|
+
require 'date'
|
3
|
+
require 'time'
|
4
|
+
require 'uri'
|
2
5
|
|
3
6
|
describe Acclaim::Option do
|
4
7
|
|
8
|
+
let(:key) { :key }
|
9
|
+
let(:args) { [] }
|
10
|
+
let(:block) { nil }
|
11
|
+
subject { Acclaim::Option.new key, *args, &block }
|
12
|
+
|
5
13
|
describe '#initialize' do
|
14
|
+
it 'should use the given key' do
|
15
|
+
subject.key.should == key
|
16
|
+
end
|
6
17
|
|
7
18
|
context 'when given multiple strings' do
|
8
|
-
|
9
19
|
let(:switches) { %w(-s --switch) }
|
10
20
|
let(:description) { 'Description' }
|
11
|
-
|
21
|
+
let(:args) { [switches, description].flatten! }
|
12
22
|
|
13
23
|
it 'should find the switches' do
|
14
24
|
subject.names.should == switches
|
@@ -17,20 +27,285 @@ describe Acclaim::Option do
|
|
17
27
|
it 'should find the description' do
|
18
28
|
subject.description.should == description
|
19
29
|
end
|
30
|
+
end
|
20
31
|
|
32
|
+
context 'when not given a class' do
|
33
|
+
it "should use String as the option's type" do
|
34
|
+
subject.type.should == String
|
35
|
+
end
|
21
36
|
end
|
22
37
|
|
23
38
|
context 'when given a class' do
|
24
|
-
|
25
39
|
let(:type) { Integer }
|
26
|
-
|
40
|
+
let(:args) { [type] }
|
27
41
|
|
28
42
|
it "should use it as the option's type" do
|
29
43
|
subject.type.should == type
|
30
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when given an additional parameter hash' do
|
48
|
+
let(:hash) { {} }
|
49
|
+
let(:args) { [hash] }
|
50
|
+
|
51
|
+
context 'that does not specify an arity' do
|
52
|
+
it 'should be a flag' do
|
53
|
+
subject.should be_flag
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'that specifies an arity' do
|
58
|
+
let(:hash) { { arity: arity } }
|
59
|
+
|
60
|
+
context 'which requires one argument' do
|
61
|
+
let(:arity) { Acclaim::Option::Arity.new 1 }
|
62
|
+
|
63
|
+
it 'should not be a flag' do
|
64
|
+
subject.should_not be_flag
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'which allows one optional argument' do
|
69
|
+
let(:arity) { Acclaim::Option::Arity.new 0, 1 }
|
70
|
+
|
71
|
+
it 'should not be a flag' do
|
72
|
+
subject.should_not be_flag
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'that does not specify a default value' do
|
78
|
+
it 'should have a default value of nil' do
|
79
|
+
subject.default.should be_nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'that specifies a default value' do
|
84
|
+
let(:default) { 10 }
|
85
|
+
let(:hash) { { default: default } }
|
86
|
+
|
87
|
+
it 'should use the default value supplied' do
|
88
|
+
subject.default.should == default
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'that does not specify whether the option is required' do
|
93
|
+
it 'should not be required' do
|
94
|
+
subject.should_not be_required
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'that specifies if the option is required' do
|
99
|
+
let(:hash) { { required: required } }
|
100
|
+
|
101
|
+
context 'as false' do
|
102
|
+
let(:required) { false }
|
103
|
+
|
104
|
+
it 'should not be required' do
|
105
|
+
subject.should_not be_required
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'as true' do
|
110
|
+
let(:required) { true }
|
111
|
+
|
112
|
+
it 'should be required' do
|
113
|
+
subject.should be_required
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when not given a block' do
|
120
|
+
it 'should not have a custom handler' do
|
121
|
+
subject.handler.should be_nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'when given a block' do
|
126
|
+
let(:block) { proc { |*args| p args } }
|
127
|
+
|
128
|
+
it 'should have a custom handler' do
|
129
|
+
subject.handler.should be_a(Proc)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe '#convert_parameters' do
|
135
|
+
let(:args) { [type].compact }
|
136
|
+
let!(:converted) { subject.convert_parameters *params }
|
137
|
+
|
138
|
+
context 'when the option was not explicitly initialized with a type' do
|
139
|
+
let(:type) { nil }
|
140
|
+
let!(:params) { %w(a b c d) }
|
141
|
+
|
142
|
+
it 'should convert the parameters to strings' do
|
143
|
+
converted.should == params.map(&:to_s)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'when the option was initialized with String as its type' do
|
148
|
+
let(:type) { String }
|
149
|
+
let(:params) { %w(a b c d) }
|
150
|
+
|
151
|
+
it 'should convert the parameters to strings' do
|
152
|
+
converted.should == params.map(&:to_s)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'when the option was initialized with Symbol as its type' do
|
157
|
+
let(:type) { Symbol }
|
158
|
+
let(:params) { %w(a b c d) }
|
159
|
+
|
160
|
+
it 'should convert the parameters to strings' do
|
161
|
+
converted.should == params.map(&:to_sym)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'when the option was initialized with Date as its type' do
|
166
|
+
let(:type) { Date }
|
167
|
+
let(:date) { Date.today }
|
168
|
+
let(:params) { [date.to_s] }
|
169
|
+
|
170
|
+
it 'should convert the parameters to dates' do
|
171
|
+
converted.should == [date]
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
context 'when the option was initialized with DateTime as its type' do
|
176
|
+
let(:type) { DateTime }
|
177
|
+
let(:date_time) { DateTime.now }
|
178
|
+
let(:params) { [date_time.to_s] }
|
179
|
+
|
180
|
+
it 'should convert the parameters to dates/times' do
|
181
|
+
converted.should == [date_time]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'when the option was initialized with Time as its type' do
|
186
|
+
let(:type) { Time }
|
187
|
+
let(:time) { Time.now }
|
188
|
+
let(:params) { [time.to_s] }
|
189
|
+
|
190
|
+
it 'should convert the parameters to times' do
|
191
|
+
converted.should == [time]
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'when the option was initialized with URI as its type' do
|
196
|
+
let(:type) { URI }
|
197
|
+
let(:uri) { URI.parse 'https://github.com/matheusmoreira/acclaim' }
|
198
|
+
let(:params) { [uri.to_s] }
|
199
|
+
|
200
|
+
it 'should convert the parameters to URIs' do
|
201
|
+
converted.should == [uri]
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '#=~' do
|
207
|
+
let(:names) { %w(-o -a --option --alternative) }
|
208
|
+
let(:args) { names }
|
209
|
+
|
210
|
+
context "when passed the option's short switch" do
|
211
|
+
let(:str) { '-o' }
|
212
|
+
|
213
|
+
it 'should match' do
|
214
|
+
(subject =~ str).should be_true
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "when passed the option's alternative short switch" do
|
219
|
+
let(:str) { '-a' }
|
220
|
+
|
221
|
+
it 'should match' do
|
222
|
+
(subject =~ str).should be_true
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
context "when passed a different short switch" do
|
227
|
+
let(:str) { '-d' }
|
228
|
+
|
229
|
+
it 'should not match' do
|
230
|
+
(subject =~ str).should be_false
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context "when passed the option's long switch" do
|
235
|
+
let(:str) { '--option' }
|
236
|
+
|
237
|
+
it 'should match' do
|
238
|
+
(subject =~ str).should be_true
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
context "when passed the option's alternative long switch" do
|
243
|
+
let(:str) { '--alternative' }
|
244
|
+
|
245
|
+
it 'should match' do
|
246
|
+
(subject =~ str).should be_true
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context "when passed a different long switch" do
|
251
|
+
let(:str) { '--different' }
|
252
|
+
|
253
|
+
it 'should not match' do
|
254
|
+
(subject =~ str).should be_false
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
31
258
|
|
259
|
+
describe '#arity' do
|
260
|
+
context 'when the option was not explicitly initialized with an arity' do
|
261
|
+
it 'should not return nil' do
|
262
|
+
subject.arity.should_not be_nil
|
263
|
+
end
|
32
264
|
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe '#arity=' do
|
268
|
+
before(:each) { begin subject.arity = arg; rescue; end }
|
269
|
+
|
270
|
+
context 'when passed nil' do
|
271
|
+
let(:arg) { nil }
|
272
|
+
|
273
|
+
it "the option's arity should not be set to nil" do
|
274
|
+
subject.arity.should_not be_nil
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
context 'when passed an arity' do
|
279
|
+
let(:arg) { Acclaim::Option::Arity.new 5, -1 }
|
33
280
|
|
281
|
+
it "should set the option's arity" do
|
282
|
+
subject.arity.should == arg
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'when passed an array' do
|
287
|
+
let(:arg) { [ 5, -1 ] }
|
288
|
+
|
289
|
+
it "should use the array to create the option's arity" do
|
290
|
+
subject.arity.should == Acclaim::Option::Arity.new(5, -1)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
|
294
|
+
context 'when passed an invalid array' do
|
295
|
+
let(:arg) { [ 5, -1, 99 ] }
|
296
|
+
|
297
|
+
it 'should raise an argument error' do
|
298
|
+
expect { subject.arity = arg }.to raise_error ArgumentError
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
describe '#require' do
|
304
|
+
before(:each) { subject.require }
|
305
|
+
|
306
|
+
it 'should make the option mandatory' do
|
307
|
+
subject.should be_required
|
308
|
+
end
|
34
309
|
end
|
35
310
|
|
36
311
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acclaim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ribbon
|
16
|
+
requirement: &19367260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19367260
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: rspec
|
16
|
-
requirement: &
|
27
|
+
requirement: &19366760 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ! '>='
|
@@ -21,10 +32,10 @@ dependencies:
|
|
21
32
|
version: '0'
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *19366760
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rookie
|
27
|
-
requirement: &
|
38
|
+
requirement: &19366220 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,7 +43,7 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *19366220
|
36
47
|
description: Command-line option parser and command interface.
|
37
48
|
email: matheus.a.m.moreira@gmail.com
|
38
49
|
executables: []
|
@@ -51,6 +62,7 @@ files:
|
|
51
62
|
- lib/acclaim/command/help.rb
|
52
63
|
- lib/acclaim/command/help/template.rb
|
53
64
|
- lib/acclaim/command/help/template/command.erb
|
65
|
+
- lib/acclaim/command/parser.rb
|
54
66
|
- lib/acclaim/command/version.rb
|
55
67
|
- lib/acclaim/option.rb
|
56
68
|
- lib/acclaim/option/arity.rb
|
@@ -60,9 +72,9 @@ files:
|
|
60
72
|
- lib/acclaim/option/type/date.rb
|
61
73
|
- lib/acclaim/option/type/date_time.rb
|
62
74
|
- lib/acclaim/option/type/string.rb
|
75
|
+
- lib/acclaim/option/type/symbol.rb
|
63
76
|
- lib/acclaim/option/type/time.rb
|
64
77
|
- lib/acclaim/option/type/uri.rb
|
65
|
-
- lib/acclaim/option/values.rb
|
66
78
|
- lib/acclaim/version.rb
|
67
79
|
- spec/acclaim/option/arity_spec.rb
|
68
80
|
- spec/acclaim/option/parser_spec.rb
|
@@ -79,12 +91,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
91
|
- - ! '>='
|
80
92
|
- !ruby/object:Gem::Version
|
81
93
|
version: '0'
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
hash: 407623794010834174
|
82
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
98
|
none: false
|
84
99
|
requirements:
|
85
100
|
- - ! '>='
|
86
101
|
- !ruby/object:Gem::Version
|
87
102
|
version: '0'
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
hash: 407623794010834174
|
88
106
|
requirements: []
|
89
107
|
rubyforge_project:
|
90
108
|
rubygems_version: 1.8.10
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module Acclaim
|
2
|
-
class Option
|
3
|
-
|
4
|
-
# Represents a set of option values.
|
5
|
-
class Values
|
6
|
-
|
7
|
-
# Gets a value by key.
|
8
|
-
def [](key)
|
9
|
-
data[key]
|
10
|
-
end
|
11
|
-
|
12
|
-
# Sets a value by key.
|
13
|
-
def []=(key, value)
|
14
|
-
data[key] = value
|
15
|
-
end
|
16
|
-
|
17
|
-
# Merge these values with the others.
|
18
|
-
def merge!(other, &block)
|
19
|
-
data.merge! other.data, &block
|
20
|
-
end
|
21
|
-
|
22
|
-
# Handles the following cases:
|
23
|
-
#
|
24
|
-
# options.method = value => options[method] = value
|
25
|
-
# options.method? => options[method] ? true : false
|
26
|
-
# options.method => options[method]
|
27
|
-
def method_missing(method, *args, &block)
|
28
|
-
m = method.to_s.chop!.to_sym
|
29
|
-
case method
|
30
|
-
when /=$/
|
31
|
-
self[m] = args.first
|
32
|
-
when /\?$/
|
33
|
-
self[m] ? true : false
|
34
|
-
else
|
35
|
-
self[method]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
protected
|
40
|
-
|
41
|
-
# The option values
|
42
|
-
def data
|
43
|
-
@options ||= {}
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|