acclaim 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  require 'acclaim/option'
2
2
  require 'acclaim/option/parser'
3
- require 'acclaim/options'
3
+ require 'acclaim/option/values'
4
4
 
5
5
  module Acclaim
6
6
 
@@ -61,9 +61,8 @@ module Acclaim
61
61
  end
62
62
 
63
63
  # Adds an option to this command.
64
- def option(key, args = {})
65
- args.merge!(key: key)
66
- options << Option.new(args)
64
+ def option(*args)
65
+ options << Option.new(*args)
67
66
  end
68
67
 
69
68
  alias :opt :option
@@ -84,7 +83,7 @@ module Acclaim
84
83
 
85
84
  # Invokes this command with a fresh set of options.
86
85
  def run(*args)
87
- invoke Options.new, args
86
+ invoke Option::Values.new, args
88
87
  rescue Option::Parser::Error => e
89
88
  puts e.message
90
89
  end
@@ -101,7 +100,7 @@ module Acclaim
101
100
  index = args.index subcommand.line
102
101
  # If we have the subcommand AND the separator, then we have it if the
103
102
  # subcommand is before the separator.
104
- index and not separator_index or index < separator_index
103
+ index and (not separator_index or index < separator_index)
105
104
  end.tap do |subcommand|
106
105
  if subcommand
107
106
  args.delete subcommand.line
@@ -1,16 +1,24 @@
1
+ require 'acclaim/option/arity'
2
+ require 'acclaim/option/parser'
3
+
1
4
  module Acclaim
2
5
 
3
6
  # Represents a command-line option.
4
7
  class Option
5
8
 
6
- attributes = %w(key names description arity default).map!(&:to_sym).freeze
9
+ attributes = %w().map!(&:to_sym).freeze
7
10
 
8
- attr_accessor *attributes
11
+ attr_accessor :key, :names, :description, :type, :default
9
12
 
10
- def initialize(args = {})
11
- args.each do |attribute, value|
12
- instance_variable_set :"@#{attribute}", value
13
- end
13
+ def initialize(key, *args)
14
+ options = args.last.is_a?(Hash) ? args.pop : {}
15
+ self.key = key
16
+ self.names = args.find_all { |arg| arg =~ Parser::SWITCH }
17
+ self.description = args.find { |arg| arg !~ Parser::SWITCH }
18
+ self.type = args.find { |arg| arg.is_a? Class }
19
+ self.arity = options[:arity]
20
+ self.default = options[:default]
21
+ self.required = options[:required]
14
22
  yield self if block_given?
15
23
  end
16
24
 
@@ -18,6 +26,18 @@ module Acclaim
18
26
  names.include? str.strip
19
27
  end
20
28
 
29
+ def arity
30
+ @arity ||= Arity.new
31
+ end
32
+
33
+ def arity=(arity_or_array)
34
+ @arity = if arity.nil? or arity_or_array.is_a? Arity
35
+ arity_or_array
36
+ else
37
+ Arity.new *arity_or_array
38
+ end
39
+ end
40
+
21
41
  def required?
22
42
  @required
23
43
  end
@@ -31,7 +51,7 @@ module Acclaim
31
51
  end
32
52
 
33
53
  def flag?
34
- not arity or arity.empty? or arity == [0, 0]
54
+ not arity or arity == [0, 0]
35
55
  end
36
56
 
37
57
  alias :bool? :flag?
@@ -0,0 +1,65 @@
1
+ module Acclaim
2
+ class Option
3
+
4
+ # Represents the the number of arguments an option can take, both mandatory
5
+ # and optional.
6
+ class Arity
7
+
8
+ attr_accessor :minimum, :optional
9
+ alias :required :minimum
10
+
11
+ def initialize(minimum = 0, optional = 0)
12
+ @minimum, @optional = minimum, optional
13
+ end
14
+
15
+ # Returns +true+ if the option takes +n+ and only +n+ parameters.
16
+ def only?(n)
17
+ optional.zero? and minimum == n
18
+ end
19
+
20
+ # Returns +true+ if the option can take an infinite number of arguments.
21
+ def unlimited?
22
+ optional < 0
23
+ end
24
+
25
+ # Returns +true+ if the option must take a finite number of arguments.
26
+ def bound?
27
+ not unlimited?
28
+ end
29
+
30
+ # Returns the total number of parameters that the option may take, which
31
+ # is the number of mandatory parameters plus the number of optional
32
+ # parameters. Returns +nil+ if the option may take an infinite number of
33
+ # parameters.
34
+ def total
35
+ bound? ? minimum + optional : nil
36
+ end
37
+
38
+ def to_a
39
+ [ minimum, optional ]
40
+ end
41
+
42
+ alias :to_ary :to_a
43
+ alias :to_array :to_a
44
+
45
+ def hash
46
+ to_a.hash
47
+ end
48
+
49
+ def ==(arity)
50
+ to_a == arity.to_a
51
+ end
52
+
53
+ alias :eql? :==
54
+ alias :=== :==
55
+
56
+ def to_s
57
+ "Arity: #{minimum} +#{unlimited? ? 'infinite' : optional}"
58
+ end
59
+
60
+ alias :inspect :to_s
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -1,4 +1,4 @@
1
- require 'acclaim/options'
1
+ require 'acclaim/option/values'
2
2
 
3
3
  module Acclaim
4
4
  class Option
@@ -85,7 +85,7 @@ module Acclaim
85
85
  end
86
86
 
87
87
  def build_options_instance!
88
- Options.new.tap do |options_instance|
88
+ Values.new.tap do |options_instance|
89
89
  options.each do |option|
90
90
  key = option.key.to_sym
91
91
  options_instance[key] = option.default
@@ -94,11 +94,11 @@ module Acclaim
94
94
  if option.flag?
95
95
  options_instance[key] = true
96
96
  else
97
- minimum, optional = option.arity
97
+ arity = option.arity
98
98
  args.each do |arg|
99
99
  arg_index = argv.index arg
100
- len = if optional >= 0
101
- arg_index + minimum + optional
100
+ len = if arity.bound?
101
+ arg_index + arity.total
102
102
  else
103
103
  argv.length - 1
104
104
  end
@@ -108,13 +108,13 @@ module Acclaim
108
108
  case param
109
109
  when nil, SWITCH, ARGUMENT_SEPARATOR then break
110
110
  else
111
- break if optional >= 0 and values.count >= minimum + optional
111
+ break if arity.bound? and values.count >= arity.total
112
112
  values << param
113
113
  end
114
114
  end
115
115
  count = values.count
116
- Error.raise_wrong_arg_number count, *option.arity if count < minimum
117
- options_instance[key] = if minimum == 1 and optional.zero?
116
+ Error.raise_wrong_arg_number count, *option.arity if count < arity.required
117
+ options_instance[key] = if arity.only? 1
118
118
  values.first
119
119
  else
120
120
  values
@@ -0,0 +1,46 @@
1
+ module Acclaim
2
+ class Option
3
+
4
+ # Represents a set of option values.
5
+ class Values
6
+
7
+ def [](key)
8
+ data[key]
9
+ end
10
+
11
+ def []=(key, value)
12
+ data[key] = value
13
+ end
14
+
15
+ def merge!(other, &block)
16
+ data.merge! other.data, &block
17
+ end
18
+
19
+ # Handles the following cases:
20
+ #
21
+ # options.method = value
22
+ # options.method?
23
+ # options.method
24
+ def method_missing(method, *args, &block)
25
+ m = method.to_s
26
+ case m
27
+ when /=$/
28
+ self[:"#{m.chop!}"] = args.first
29
+ when /\?$/
30
+ self[:"#{m.chop!}"] ? true : false
31
+ else
32
+ self[method]
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ # The option values
39
+ def data
40
+ @options ||= {}
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -3,7 +3,7 @@ module Acclaim
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 0
6
- PATCH = 1
6
+ PATCH = 2
7
7
  BUILD = nil
8
8
 
9
9
  STRING = [ MAJOR, MINOR, PATCH, BUILD ].compact.join '.'
@@ -32,14 +32,13 @@ describe Acclaim::Option::Parser do
32
32
  let(:options) do
33
33
  [].tap do |opts|
34
34
  ('a'..'f').each do |c|
35
- hash = { key: c, names: ["-#{c}"] }
35
+ hash = {}
36
36
  hash[:arity] = [1, 0] if c == 'b' or c == 'f'
37
37
  hash[:required] = true if c == 'd'
38
- opts << Acclaim::Option.new(hash)
38
+ opts << Acclaim::Option.new(c.to_sym, "-#{c}", hash)
39
39
  end
40
- opts << Acclaim::Option.new(key: 'long', names: ['--long'])
41
- opts << Acclaim::Option.new(key: 'params', names: ['--parameters'],
42
- arity: [1, 1], default: [])
40
+ opts << Acclaim::Option.new(:long, '--long')
41
+ opts << Acclaim::Option.new(:params, '--parameters', arity: [1, 1], default: [])
43
42
  end
44
43
  end
45
44
 
@@ -0,0 +1,36 @@
1
+ require 'acclaim/option'
2
+
3
+ describe Acclaim::Option do
4
+
5
+ describe '#initialize' do
6
+
7
+ context 'when given multiple strings' do
8
+
9
+ let(:switches) { %w(-s --switch) }
10
+ let(:description) { 'Description' }
11
+ subject { Acclaim::Option.new :key, *[switches, description].flatten }
12
+
13
+ it 'should find the switches' do
14
+ subject.names.should == switches
15
+ end
16
+
17
+ it 'should find the description' do
18
+ subject.description.should == description
19
+ end
20
+
21
+ end
22
+
23
+ context 'when given a class' do
24
+
25
+ let(:type) { Integer }
26
+ subject { Acclaim::Option.new :key, type }
27
+
28
+ it "should use it as the option's type" do
29
+ subject.type.should == type
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ 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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &11364480 !ruby/object:Gem::Requirement
16
+ requirement: &7597280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *11364480
24
+ version_requirements: *7597280
25
25
  description: Command-line option parser and command interface.
26
26
  email: matheus.a.m.moreira@gmail.com
27
27
  executables: []
@@ -38,10 +38,12 @@ files:
38
38
  - lib/acclaim.rb
39
39
  - lib/acclaim/command.rb
40
40
  - lib/acclaim/option.rb
41
+ - lib/acclaim/option/arity.rb
41
42
  - lib/acclaim/option/parser.rb
42
- - lib/acclaim/options.rb
43
+ - lib/acclaim/option/values.rb
43
44
  - lib/acclaim/version.rb
44
45
  - spec/acclaim/option/parser_spec.rb
46
+ - spec/acclaim/option_spec.rb
45
47
  homepage: https://github.com/matheusmoreira/acclaim
46
48
  licenses: []
47
49
  post_install_message:
@@ -1,45 +0,0 @@
1
- module Acclaim
2
-
3
- # Represents a set of options.
4
- class Options
5
-
6
- def [](key)
7
- data[key]
8
- end
9
-
10
- def []=(key, value)
11
- data[key] = value
12
- end
13
-
14
- def merge!(other, &block)
15
- data.merge! other.data, &block
16
- end
17
-
18
- # Handles the following cases:
19
- #
20
- # options.method = value
21
- # options.method?
22
- # options.method
23
- def method_missing(method, *args, &block)
24
- m = method.to_s
25
- case m
26
- when /=$/
27
- self[:"#{m.chop!}"] = args.first
28
- when /\?$/
29
- self[:"#{m.chop!}"] ? true : false
30
- else
31
- self[method]
32
- end
33
- end
34
-
35
- protected
36
-
37
- # The option values
38
- def data
39
- @options ||= {}
40
- end
41
-
42
-
43
- end
44
-
45
- end