rkh-bundler 1.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +42 -0
  3. data/CHANGELOG.md +1105 -0
  4. data/ISSUES.md +67 -0
  5. data/LICENSE +23 -0
  6. data/README.md +31 -0
  7. data/Rakefile +208 -0
  8. data/UPGRADING.md +103 -0
  9. data/bin/bundle +31 -0
  10. data/bundler.gemspec +31 -0
  11. data/lib/bundler.rb +353 -0
  12. data/lib/bundler/capistrano.rb +11 -0
  13. data/lib/bundler/cli.rb +693 -0
  14. data/lib/bundler/definition.rb +568 -0
  15. data/lib/bundler/dep_proxy.rb +43 -0
  16. data/lib/bundler/dependency.rb +134 -0
  17. data/lib/bundler/deployment.rb +58 -0
  18. data/lib/bundler/dsl.rb +256 -0
  19. data/lib/bundler/endpoint_specification.rb +78 -0
  20. data/lib/bundler/environment.rb +47 -0
  21. data/lib/bundler/fetcher.rb +225 -0
  22. data/lib/bundler/gem_helper.rb +162 -0
  23. data/lib/bundler/gem_helpers.rb +23 -0
  24. data/lib/bundler/gem_installer.rb +9 -0
  25. data/lib/bundler/gem_tasks.rb +2 -0
  26. data/lib/bundler/graph.rb +148 -0
  27. data/lib/bundler/index.rb +187 -0
  28. data/lib/bundler/installer.rb +190 -0
  29. data/lib/bundler/lazy_specification.rb +79 -0
  30. data/lib/bundler/lockfile_parser.rb +127 -0
  31. data/lib/bundler/match_platform.rb +13 -0
  32. data/lib/bundler/psyched_yaml.rb +15 -0
  33. data/lib/bundler/remote_specification.rb +57 -0
  34. data/lib/bundler/resolver.rb +486 -0
  35. data/lib/bundler/ruby_version.rb +94 -0
  36. data/lib/bundler/rubygems_ext.rb +153 -0
  37. data/lib/bundler/rubygems_integration.rb +394 -0
  38. data/lib/bundler/runtime.rb +233 -0
  39. data/lib/bundler/settings.rb +128 -0
  40. data/lib/bundler/setup.rb +23 -0
  41. data/lib/bundler/shared_helpers.rb +71 -0
  42. data/lib/bundler/source.rb +869 -0
  43. data/lib/bundler/spec_set.rb +137 -0
  44. data/lib/bundler/templates/Executable +16 -0
  45. data/lib/bundler/templates/Executable.standalone +12 -0
  46. data/lib/bundler/templates/Gemfile +4 -0
  47. data/lib/bundler/templates/newgem/Gemfile.tt +4 -0
  48. data/lib/bundler/templates/newgem/LICENSE.tt +22 -0
  49. data/lib/bundler/templates/newgem/README.md.tt +29 -0
  50. data/lib/bundler/templates/newgem/Rakefile.tt +2 -0
  51. data/lib/bundler/templates/newgem/bin/newgem.tt +3 -0
  52. data/lib/bundler/templates/newgem/gitignore.tt +17 -0
  53. data/lib/bundler/templates/newgem/lib/newgem.rb.tt +9 -0
  54. data/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +7 -0
  55. data/lib/bundler/templates/newgem/newgem.gemspec.tt +17 -0
  56. data/lib/bundler/ui.rb +88 -0
  57. data/lib/bundler/vendor/net/http/faster.rb +27 -0
  58. data/lib/bundler/vendor/net/http/persistent.rb +468 -0
  59. data/lib/bundler/vendor/thor.rb +358 -0
  60. data/lib/bundler/vendor/thor/actions.rb +314 -0
  61. data/lib/bundler/vendor/thor/actions/create_file.rb +105 -0
  62. data/lib/bundler/vendor/thor/actions/create_link.rb +57 -0
  63. data/lib/bundler/vendor/thor/actions/directory.rb +93 -0
  64. data/lib/bundler/vendor/thor/actions/empty_directory.rb +134 -0
  65. data/lib/bundler/vendor/thor/actions/file_manipulation.rb +270 -0
  66. data/lib/bundler/vendor/thor/actions/inject_into_file.rb +109 -0
  67. data/lib/bundler/vendor/thor/base.rb +576 -0
  68. data/lib/bundler/vendor/thor/core_ext/file_binary_read.rb +9 -0
  69. data/lib/bundler/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  70. data/lib/bundler/vendor/thor/core_ext/ordered_hash.rb +100 -0
  71. data/lib/bundler/vendor/thor/error.rb +30 -0
  72. data/lib/bundler/vendor/thor/group.rb +273 -0
  73. data/lib/bundler/vendor/thor/invocation.rb +168 -0
  74. data/lib/bundler/vendor/thor/parser.rb +4 -0
  75. data/lib/bundler/vendor/thor/parser/argument.rb +67 -0
  76. data/lib/bundler/vendor/thor/parser/arguments.rb +161 -0
  77. data/lib/bundler/vendor/thor/parser/option.rb +120 -0
  78. data/lib/bundler/vendor/thor/parser/options.rb +172 -0
  79. data/lib/bundler/vendor/thor/rake_compat.rb +66 -0
  80. data/lib/bundler/vendor/thor/runner.rb +309 -0
  81. data/lib/bundler/vendor/thor/shell.rb +88 -0
  82. data/lib/bundler/vendor/thor/shell/basic.rb +302 -0
  83. data/lib/bundler/vendor/thor/shell/color.rb +108 -0
  84. data/lib/bundler/vendor/thor/shell/html.rb +121 -0
  85. data/lib/bundler/vendor/thor/task.rb +113 -0
  86. data/lib/bundler/vendor/thor/util.rb +229 -0
  87. data/lib/bundler/vendor/thor/version.rb +3 -0
  88. data/lib/bundler/vendored_thor.rb +7 -0
  89. data/lib/bundler/version.rb +6 -0
  90. data/lib/bundler/vlad.rb +11 -0
  91. data/man/bundle-config.ronn +130 -0
  92. data/man/bundle-exec.ronn +111 -0
  93. data/man/bundle-install.ronn +335 -0
  94. data/man/bundle-package.ronn +59 -0
  95. data/man/bundle-update.ronn +176 -0
  96. data/man/bundle.ronn +83 -0
  97. data/man/gemfile.5.ronn +324 -0
  98. data/man/index.txt +6 -0
  99. data/spec/bundler/dsl_spec.rb +48 -0
  100. data/spec/bundler/gem_helper_spec.rb +174 -0
  101. data/spec/bundler/source_spec.rb +25 -0
  102. data/spec/cache/gems_spec.rb +239 -0
  103. data/spec/cache/git_spec.rb +124 -0
  104. data/spec/cache/path_spec.rb +103 -0
  105. data/spec/cache/platform_spec.rb +57 -0
  106. data/spec/install/deploy_spec.rb +211 -0
  107. data/spec/install/gems/c_ext_spec.rb +48 -0
  108. data/spec/install/gems/dependency_api_spec.rb +402 -0
  109. data/spec/install/gems/env_spec.rb +107 -0
  110. data/spec/install/gems/flex_spec.rb +313 -0
  111. data/spec/install/gems/groups_spec.rb +268 -0
  112. data/spec/install/gems/packed_spec.rb +84 -0
  113. data/spec/install/gems/platform_spec.rb +208 -0
  114. data/spec/install/gems/post_install_spec.rb +47 -0
  115. data/spec/install/gems/resolving_spec.rb +72 -0
  116. data/spec/install/gems/simple_case_spec.rb +814 -0
  117. data/spec/install/gems/standalone_spec.rb +260 -0
  118. data/spec/install/gems/sudo_spec.rb +74 -0
  119. data/spec/install/gems/win32_spec.rb +26 -0
  120. data/spec/install/gemspec_spec.rb +170 -0
  121. data/spec/install/git_spec.rb +796 -0
  122. data/spec/install/invalid_spec.rb +35 -0
  123. data/spec/install/path_spec.rb +405 -0
  124. data/spec/install/upgrade_spec.rb +26 -0
  125. data/spec/lock/git_spec.rb +35 -0
  126. data/spec/lock/lockfile_spec.rb +809 -0
  127. data/spec/other/check_spec.rb +265 -0
  128. data/spec/other/clean_spec.rb +492 -0
  129. data/spec/other/config_spec.rb +138 -0
  130. data/spec/other/console_spec.rb +54 -0
  131. data/spec/other/exec_spec.rb +229 -0
  132. data/spec/other/ext_spec.rb +37 -0
  133. data/spec/other/help_spec.rb +39 -0
  134. data/spec/other/init_spec.rb +40 -0
  135. data/spec/other/newgem_spec.rb +87 -0
  136. data/spec/other/open_spec.rb +35 -0
  137. data/spec/other/outdated_spec.rb +93 -0
  138. data/spec/other/platform_spec.rb +881 -0
  139. data/spec/other/show_spec.rb +88 -0
  140. data/spec/quality_spec.rb +62 -0
  141. data/spec/realworld/edgecases_spec.rb +177 -0
  142. data/spec/resolver/basic_spec.rb +20 -0
  143. data/spec/resolver/platform_spec.rb +82 -0
  144. data/spec/runtime/executable_spec.rb +120 -0
  145. data/spec/runtime/load_spec.rb +107 -0
  146. data/spec/runtime/platform_spec.rb +90 -0
  147. data/spec/runtime/require_spec.rb +261 -0
  148. data/spec/runtime/setup_spec.rb +755 -0
  149. data/spec/runtime/with_clean_env_spec.rb +80 -0
  150. data/spec/spec_helper.rb +98 -0
  151. data/spec/support/artifice/endopint_marshal_fail_basic_authentication.rb +13 -0
  152. data/spec/support/artifice/endpoint.rb +54 -0
  153. data/spec/support/artifice/endpoint_500.rb +37 -0
  154. data/spec/support/artifice/endpoint_api_missing.rb +16 -0
  155. data/spec/support/artifice/endpoint_basic_authentication.rb +13 -0
  156. data/spec/support/artifice/endpoint_extra.rb +27 -0
  157. data/spec/support/artifice/endpoint_extra_missing.rb +15 -0
  158. data/spec/support/artifice/endpoint_fallback.rb +18 -0
  159. data/spec/support/artifice/endpoint_marshal_fail.rb +11 -0
  160. data/spec/support/artifice/endpoint_redirect.rb +15 -0
  161. data/spec/support/builders.rb +604 -0
  162. data/spec/support/fakeweb/rack-1.0.0.marshal +2 -0
  163. data/spec/support/fakeweb/windows.rb +23 -0
  164. data/spec/support/helpers.rb +317 -0
  165. data/spec/support/indexes.rb +112 -0
  166. data/spec/support/matchers.rb +77 -0
  167. data/spec/support/path.rb +73 -0
  168. data/spec/support/platforms.rb +86 -0
  169. data/spec/support/ruby_ext.rb +20 -0
  170. data/spec/support/rubygems_ext.rb +37 -0
  171. data/spec/support/rubygems_hax/platform.rb +22 -0
  172. data/spec/support/sudo.rb +21 -0
  173. data/spec/update/gems_spec.rb +134 -0
  174. data/spec/update/git_spec.rb +196 -0
  175. data/spec/update/source_spec.rb +51 -0
  176. metadata +338 -0
@@ -0,0 +1,168 @@
1
+ class Thor
2
+ module Invocation
3
+ def self.included(base) #:nodoc:
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ # This method is responsible for receiving a name and find the proper
9
+ # class and task for it. The key is an optional parameter which is
10
+ # available only in class methods invocations (i.e. in Thor::Group).
11
+ def prepare_for_invocation(key, name) #:nodoc:
12
+ case name
13
+ when Symbol, String
14
+ Thor::Util.find_class_and_task_by_namespace(name.to_s, !key)
15
+ else
16
+ name
17
+ end
18
+ end
19
+ end
20
+
21
+ # Make initializer aware of invocations and the initialization args.
22
+ def initialize(args=[], options={}, config={}, &block) #:nodoc:
23
+ @_invocations = config[:invocations] || Hash.new { |h,k| h[k] = [] }
24
+ @_initializer = [ args, options, config ]
25
+ super
26
+ end
27
+
28
+ # Receives a name and invokes it. The name can be a string (either "task" or
29
+ # "namespace:task"), a Thor::Task, a Class or a Thor instance. If the task
30
+ # cannot be guessed by name, it can also be supplied as second argument.
31
+ #
32
+ # You can also supply the arguments, options and configuration values for
33
+ # the task to be invoked, if none is given, the same values used to
34
+ # initialize the invoker are used to initialize the invoked.
35
+ #
36
+ # When no name is given, it will invoke the default task of the current class.
37
+ #
38
+ # ==== Examples
39
+ #
40
+ # class A < Thor
41
+ # def foo
42
+ # invoke :bar
43
+ # invoke "b:hello", ["José"]
44
+ # end
45
+ #
46
+ # def bar
47
+ # invoke "b:hello", ["José"]
48
+ # end
49
+ # end
50
+ #
51
+ # class B < Thor
52
+ # def hello(name)
53
+ # puts "hello #{name}"
54
+ # end
55
+ # end
56
+ #
57
+ # You can notice that the method "foo" above invokes two tasks: "bar",
58
+ # which belongs to the same class and "hello" which belongs to the class B.
59
+ #
60
+ # By using an invocation system you ensure that a task is invoked only once.
61
+ # In the example above, invoking "foo" will invoke "b:hello" just once, even
62
+ # if it's invoked later by "bar" method.
63
+ #
64
+ # When class A invokes class B, all arguments used on A initialization are
65
+ # supplied to B. This allows lazy parse of options. Let's suppose you have
66
+ # some rspec tasks:
67
+ #
68
+ # class Rspec < Thor::Group
69
+ # class_option :mock_framework, :type => :string, :default => :rr
70
+ #
71
+ # def invoke_mock_framework
72
+ # invoke "rspec:#{options[:mock_framework]}"
73
+ # end
74
+ # end
75
+ #
76
+ # As you noticed, it invokes the given mock framework, which might have its
77
+ # own options:
78
+ #
79
+ # class Rspec::RR < Thor::Group
80
+ # class_option :style, :type => :string, :default => :mock
81
+ # end
82
+ #
83
+ # Since it's not rspec concern to parse mock framework options, when RR
84
+ # is invoked all options are parsed again, so RR can extract only the options
85
+ # that it's going to use.
86
+ #
87
+ # If you want Rspec::RR to be initialized with its own set of options, you
88
+ # have to do that explicitely:
89
+ #
90
+ # invoke "rspec:rr", [], :style => :foo
91
+ #
92
+ # Besides giving an instance, you can also give a class to invoke:
93
+ #
94
+ # invoke Rspec::RR, [], :style => :foo
95
+ #
96
+ def invoke(name=nil, *args)
97
+ if name.nil?
98
+ warn "[Thor] Calling invoke() without argument is deprecated. Please use invoke_all instead.\n#{caller.join("\n")}"
99
+ return invoke_all
100
+ end
101
+
102
+ args.unshift(nil) if Array === args.first || NilClass === args.first
103
+ task, args, opts, config = args
104
+
105
+ klass, task = _retrieve_class_and_task(name, task)
106
+ raise "Expected Thor class, got #{klass}" unless klass <= Thor::Base
107
+
108
+ args, opts, config = _parse_initialization_options(args, opts, config)
109
+ klass.send(:dispatch, task, args, opts, config)
110
+ end
111
+
112
+ # Invoke the given task if the given args.
113
+ def invoke_task(task, *args) #:nodoc:
114
+ current = @_invocations[self.class]
115
+
116
+ unless current.include?(task.name)
117
+ current << task.name
118
+ task.run(self, *args)
119
+ end
120
+ end
121
+
122
+ # Invoke all tasks for the current instance.
123
+ def invoke_all #:nodoc:
124
+ self.class.all_tasks.map { |_, task| invoke_task(task) }
125
+ end
126
+
127
+ # Invokes using shell padding.
128
+ def invoke_with_padding(*args)
129
+ with_padding { invoke(*args) }
130
+ end
131
+
132
+ protected
133
+
134
+ # Configuration values that are shared between invocations.
135
+ def _shared_configuration #:nodoc:
136
+ { :invocations => @_invocations }
137
+ end
138
+
139
+ # This method simply retrieves the class and task to be invoked.
140
+ # If the name is nil or the given name is a task in the current class,
141
+ # use the given name and return self as class. Otherwise, call
142
+ # prepare_for_invocation in the current class.
143
+ def _retrieve_class_and_task(name, sent_task=nil) #:nodoc:
144
+ case
145
+ when name.nil?
146
+ [self.class, nil]
147
+ when self.class.all_tasks[name.to_s]
148
+ [self.class, name.to_s]
149
+ else
150
+ klass, task = self.class.prepare_for_invocation(nil, name)
151
+ [klass, task || sent_task]
152
+ end
153
+ end
154
+
155
+ # Initialize klass using values stored in the @_initializer.
156
+ def _parse_initialization_options(args, opts, config) #:nodoc:
157
+ stored_args, stored_opts, stored_config = @_initializer
158
+
159
+ args ||= stored_args.dup
160
+ opts ||= stored_opts.dup
161
+
162
+ config ||= {}
163
+ config = stored_config.merge(_shared_configuration).merge!(config)
164
+
165
+ [ args, opts, config ]
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,4 @@
1
+ require 'thor/parser/argument'
2
+ require 'thor/parser/arguments'
3
+ require 'thor/parser/option'
4
+ require 'thor/parser/options'
@@ -0,0 +1,67 @@
1
+ class Thor
2
+ class Argument #:nodoc:
3
+ VALID_TYPES = [ :numeric, :hash, :array, :string ]
4
+
5
+ attr_reader :name, :description, :required, :type, :default, :banner
6
+ alias :human_name :name
7
+
8
+ def initialize(name, description=nil, required=true, type=:string, default=nil, banner=nil)
9
+ class_name = self.class.name.split("::").last
10
+
11
+ raise ArgumentError, "#{class_name} name can't be nil." if name.nil?
12
+ raise ArgumentError, "Type :#{type} is not valid for #{class_name.downcase}s." if type && !valid_type?(type)
13
+
14
+ @name = name.to_s
15
+ @description = description
16
+ @required = required || false
17
+ @type = (type || :string).to_sym
18
+ @default = default
19
+ @banner = banner || default_banner
20
+
21
+ validate! # Trigger specific validations
22
+ end
23
+
24
+ def usage
25
+ required? ? banner : "[#{banner}]"
26
+ end
27
+
28
+ def required?
29
+ required
30
+ end
31
+
32
+ def show_default?
33
+ case default
34
+ when Array, String, Hash
35
+ !default.empty?
36
+ else
37
+ default
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def validate!
44
+ raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
45
+ end
46
+
47
+ def valid_type?(type)
48
+ self.class::VALID_TYPES.include?(type.to_sym)
49
+ end
50
+
51
+ def default_banner
52
+ case type
53
+ when :boolean
54
+ nil
55
+ when :string, :default
56
+ human_name.upcase
57
+ when :numeric
58
+ "N"
59
+ when :hash
60
+ "key:value"
61
+ when :array
62
+ "one two three"
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,161 @@
1
+ class Thor
2
+ class Arguments #:nodoc:
3
+ NUMERIC = /(\d*\.\d+|\d+)/
4
+
5
+ # Receives an array of args and returns two arrays, one with arguments
6
+ # and one with switches.
7
+ #
8
+ def self.split(args)
9
+ arguments = []
10
+
11
+ args.each do |item|
12
+ break if item =~ /^-/
13
+ arguments << item
14
+ end
15
+
16
+ return arguments, args[Range.new(arguments.size, -1)]
17
+ end
18
+
19
+ def self.parse(*args)
20
+ to_parse = args.pop
21
+ new(*args).parse(to_parse)
22
+ end
23
+
24
+ # Takes an array of Thor::Argument objects.
25
+ #
26
+ def initialize(arguments=[])
27
+ @assigns, @non_assigned_required = {}, []
28
+ @switches = arguments
29
+
30
+ arguments.each do |argument|
31
+ if argument.default != nil
32
+ @assigns[argument.human_name] = argument.default
33
+ elsif argument.required?
34
+ @non_assigned_required << argument
35
+ end
36
+ end
37
+ end
38
+
39
+ def parse(args)
40
+ @pile = args.dup
41
+
42
+ @switches.each do |argument|
43
+ break unless peek
44
+ @non_assigned_required.delete(argument)
45
+ @assigns[argument.human_name] = send(:"parse_#{argument.type}", argument.human_name)
46
+ end
47
+
48
+ check_requirement!
49
+ @assigns
50
+ end
51
+
52
+ private
53
+
54
+ def no_or_skip?(arg)
55
+ arg =~ /^--(no|skip)-([-\w]+)$/
56
+ $2
57
+ end
58
+
59
+ def last?
60
+ @pile.empty?
61
+ end
62
+
63
+ def peek
64
+ @pile.first
65
+ end
66
+
67
+ def shift
68
+ @pile.shift
69
+ end
70
+
71
+ def unshift(arg)
72
+ unless arg.kind_of?(Array)
73
+ @pile.unshift(arg)
74
+ else
75
+ @pile = arg + @pile
76
+ end
77
+ end
78
+
79
+ def current_is_value?
80
+ peek && peek.to_s !~ /^-/
81
+ end
82
+
83
+ # Runs through the argument array getting strings that contains ":" and
84
+ # mark it as a hash:
85
+ #
86
+ # [ "name:string", "age:integer" ]
87
+ #
88
+ # Becomes:
89
+ #
90
+ # { "name" => "string", "age" => "integer" }
91
+ #
92
+ def parse_hash(name)
93
+ return shift if peek.is_a?(Hash)
94
+ hash = {}
95
+
96
+ while current_is_value? && peek.include?(?:)
97
+ key, value = shift.split(':',2)
98
+ hash[key] = value
99
+ end
100
+ hash
101
+ end
102
+
103
+ # Runs through the argument array getting all strings until no string is
104
+ # found or a switch is found.
105
+ #
106
+ # ["a", "b", "c"]
107
+ #
108
+ # And returns it as an array:
109
+ #
110
+ # ["a", "b", "c"]
111
+ #
112
+ def parse_array(name)
113
+ return shift if peek.is_a?(Array)
114
+ array = []
115
+
116
+ while current_is_value?
117
+ array << shift
118
+ end
119
+ array
120
+ end
121
+
122
+ # Check if the peek is numeric format and return a Float or Integer.
123
+ # Otherwise raises an error.
124
+ #
125
+ def parse_numeric(name)
126
+ return shift if peek.is_a?(Numeric)
127
+
128
+ unless peek =~ NUMERIC && $& == peek
129
+ raise MalformattedArgumentError, "Expected numeric value for '#{name}'; got #{peek.inspect}"
130
+ end
131
+
132
+ $&.index('.') ? shift.to_f : shift.to_i
133
+ end
134
+
135
+ # Parse string:
136
+ # for --string-arg, just return the current value in the pile
137
+ # for --no-string-arg, nil
138
+ #
139
+ def parse_string(name)
140
+ if no_or_skip?(name)
141
+ nil
142
+ else
143
+ shift
144
+ end
145
+ end
146
+
147
+ # Raises an error if @non_assigned_required array is not empty.
148
+ #
149
+ def check_requirement!
150
+ unless @non_assigned_required.empty?
151
+ names = @non_assigned_required.map do |o|
152
+ o.respond_to?(:switch_name) ? o.switch_name : o.human_name
153
+ end.join("', '")
154
+
155
+ class_name = self.class.name.split('::').last.downcase
156
+ raise RequiredArgumentMissingError, "No value provided for required #{class_name} '#{names}'"
157
+ end
158
+ end
159
+
160
+ end
161
+ end
@@ -0,0 +1,120 @@
1
+ class Thor
2
+ class Option < Argument #:nodoc:
3
+ attr_reader :aliases, :group, :lazy_default
4
+
5
+ VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
6
+
7
+ def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, lazy_default=nil, group=nil, aliases=nil)
8
+ super(name, description, required, type, default, banner)
9
+ @lazy_default = lazy_default
10
+ @group = group.to_s.capitalize if group
11
+ @aliases = [*aliases].compact
12
+ end
13
+
14
+ # This parse quick options given as method_options. It makes several
15
+ # assumptions, but you can be more specific using the option method.
16
+ #
17
+ # parse :foo => "bar"
18
+ # #=> Option foo with default value bar
19
+ #
20
+ # parse [:foo, :baz] => "bar"
21
+ # #=> Option foo with default value bar and alias :baz
22
+ #
23
+ # parse :foo => :required
24
+ # #=> Required option foo without default value
25
+ #
26
+ # parse :foo => 2
27
+ # #=> Option foo with default value 2 and type numeric
28
+ #
29
+ # parse :foo => :numeric
30
+ # #=> Option foo without default value and type numeric
31
+ #
32
+ # parse :foo => true
33
+ # #=> Option foo with default value true and type boolean
34
+ #
35
+ # The valid types are :boolean, :numeric, :hash, :array and :string. If none
36
+ # is given a default type is assumed. This default type accepts arguments as
37
+ # string (--foo=value) or booleans (just --foo).
38
+ #
39
+ # By default all options are optional, unless :required is given.
40
+ #
41
+ def self.parse(key, value)
42
+ if key.is_a?(Array)
43
+ name, *aliases = key
44
+ else
45
+ name, aliases = key, []
46
+ end
47
+
48
+ name = name.to_s
49
+ default = value
50
+
51
+ type = case value
52
+ when Symbol
53
+ default = nil
54
+ if VALID_TYPES.include?(value)
55
+ value
56
+ elsif required = (value == :required)
57
+ :string
58
+ end
59
+ when TrueClass, FalseClass
60
+ :boolean
61
+ when Numeric
62
+ :numeric
63
+ when Hash, Array, String
64
+ value.class.name.downcase.to_sym
65
+ end
66
+
67
+ self.new(name.to_s, nil, required, type, default, nil, nil, nil, aliases)
68
+ end
69
+
70
+ def switch_name
71
+ @switch_name ||= dasherized? ? name : dasherize(name)
72
+ end
73
+
74
+ def human_name
75
+ @human_name ||= dasherized? ? undasherize(name) : name
76
+ end
77
+
78
+ def usage(padding=0)
79
+ sample = if banner && !banner.to_s.empty?
80
+ "#{switch_name}=#{banner}"
81
+ else
82
+ switch_name
83
+ end
84
+
85
+ sample = "[#{sample}]" unless required?
86
+
87
+ if aliases.empty?
88
+ (" " * padding) << sample
89
+ else
90
+ "#{aliases.join(', ')}, #{sample}"
91
+ end
92
+ end
93
+
94
+ VALID_TYPES.each do |type|
95
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
96
+ def #{type}?
97
+ self.type == #{type.inspect}
98
+ end
99
+ RUBY
100
+ end
101
+
102
+ protected
103
+
104
+ def validate!
105
+ raise ArgumentError, "An option cannot be boolean and required." if boolean? && required?
106
+ end
107
+
108
+ def dasherized?
109
+ name.index('-') == 0
110
+ end
111
+
112
+ def undasherize(str)
113
+ str.sub(/^-{1,2}/, '')
114
+ end
115
+
116
+ def dasherize(str)
117
+ (str.length > 1 ? "--" : "-") + str.gsub('_', '-')
118
+ end
119
+ end
120
+ end