clone 1.0.0.alpha → 1.0.0.beta

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.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/Gemfile +1 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +22 -1
  6. data/VERSION +1 -1
  7. data/bin/clone +7 -0
  8. data/clone.gemspec +4 -0
  9. data/docs/readme +16 -0
  10. data/docs/readme.txt +5 -0
  11. data/lib/clone.rb +13 -2
  12. data/lib/clone/config/config.rb +19 -0
  13. data/lib/clone/config/config.yml +22 -0
  14. data/lib/clone/config/default_config.yml +20 -0
  15. data/lib/clone/config/require.rb +3 -0
  16. data/lib/clone/config/version.rb +10 -0
  17. data/lib/clone/config/yml.rb +22 -0
  18. data/lib/clone/config/z_defaults.rb +28 -0
  19. data/lib/clone/generator/engine.rb +581 -0
  20. data/lib/clone/generator/terminal.rb +104 -0
  21. data/lib/clone/helpers/helper_methods.rb +251 -0
  22. data/lib/clone/helpers/local_methods.rb +150 -0
  23. data/lib/clone/helpers/require.rb +37 -0
  24. data/module/Marshal.4.8 +0 -0
  25. data/module/Marshal.4.8.Z +0 -0
  26. data/module/gems/commander-4.1.3/.gitignore +6 -0
  27. data/module/gems/commander-4.1.3/.travis.yml +11 -0
  28. data/module/gems/commander-4.1.3/DEVELOPMENT +15 -0
  29. data/module/gems/commander-4.1.3/Gemfile +3 -0
  30. data/module/gems/commander-4.1.3/History.rdoc +345 -0
  31. data/module/gems/commander-4.1.3/Manifest +38 -0
  32. data/module/gems/commander-4.1.3/README.rdoc +375 -0
  33. data/module/gems/commander-4.1.3/Rakefile +10 -0
  34. data/module/gems/commander-4.1.3/bin/commander +55 -0
  35. data/module/gems/commander-4.1.3/commander.gemspec +26 -0
  36. data/module/gems/commander-4.1.3/lib/commander.rb +32 -0
  37. data/module/gems/commander-4.1.3/lib/commander/blank.rb +8 -0
  38. data/module/gems/commander-4.1.3/lib/commander/command.rb +213 -0
  39. data/module/gems/commander-4.1.3/lib/commander/core_ext.rb +3 -0
  40. data/module/gems/commander-4.1.3/lib/commander/core_ext/array.rb +26 -0
  41. data/module/gems/commander-4.1.3/lib/commander/core_ext/object.rb +11 -0
  42. data/module/gems/commander-4.1.3/lib/commander/delegates.rb +13 -0
  43. data/module/gems/commander-4.1.3/lib/commander/help_formatters.rb +8 -0
  44. data/module/gems/commander-4.1.3/lib/commander/help_formatters/base.rb +18 -0
  45. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal.rb +20 -0
  46. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal/command_help.erb +35 -0
  47. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal/help.erb +36 -0
  48. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact.rb +12 -0
  49. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact/command_help.erb +27 -0
  50. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact/help.erb +29 -0
  51. data/module/gems/commander-4.1.3/lib/commander/import.rb +10 -0
  52. data/module/gems/commander-4.1.3/lib/commander/platform.rb +8 -0
  53. data/module/gems/commander-4.1.3/lib/commander/runner.rb +411 -0
  54. data/module/gems/commander-4.1.3/lib/commander/user_interaction.rb +521 -0
  55. data/module/gems/commander-4.1.3/lib/commander/version.rb +3 -0
  56. data/module/gems/commander-4.1.3/spec/command_spec.rb +157 -0
  57. data/module/gems/commander-4.1.3/spec/core_ext/array_spec.rb +20 -0
  58. data/module/gems/commander-4.1.3/spec/core_ext/object_spec.rb +21 -0
  59. data/module/gems/commander-4.1.3/spec/help_formatters/terminal_spec.rb +67 -0
  60. data/module/gems/commander-4.1.3/spec/runner_spec.rb +526 -0
  61. data/module/gems/commander-4.1.3/spec/spec_helper.rb +59 -0
  62. data/module/gems/commander-4.1.3/spec/ui_spec.rb +30 -0
  63. data/module/gems/hello.rb +1 -0
  64. data/module/gems/highline-1.6.19/.gitignore +2 -0
  65. data/module/gems/highline-1.6.19/AUTHORS +3 -0
  66. data/module/gems/highline-1.6.19/CHANGELOG +346 -0
  67. data/module/gems/highline-1.6.19/COPYING +340 -0
  68. data/module/gems/highline-1.6.19/INSTALL +55 -0
  69. data/module/gems/highline-1.6.19/LICENSE +7 -0
  70. data/module/gems/highline-1.6.19/README.rdoc +63 -0
  71. data/module/gems/highline-1.6.19/Rakefile +50 -0
  72. data/module/gems/highline-1.6.19/TODO +6 -0
  73. data/module/gems/highline-1.6.19/examples/ansi_colors.rb +38 -0
  74. data/module/gems/highline-1.6.19/examples/asking_for_arrays.rb +18 -0
  75. data/module/gems/highline-1.6.19/examples/basic_usage.rb +75 -0
  76. data/module/gems/highline-1.6.19/examples/color_scheme.rb +32 -0
  77. data/module/gems/highline-1.6.19/examples/get_character.rb +12 -0
  78. data/module/gems/highline-1.6.19/examples/limit.rb +12 -0
  79. data/module/gems/highline-1.6.19/examples/menus.rb +65 -0
  80. data/module/gems/highline-1.6.19/examples/overwrite.rb +19 -0
  81. data/module/gems/highline-1.6.19/examples/page_and_wrap.rb +322 -0
  82. data/module/gems/highline-1.6.19/examples/password.rb +7 -0
  83. data/module/gems/highline-1.6.19/examples/repeat_entry.rb +21 -0
  84. data/module/gems/highline-1.6.19/examples/trapping_eof.rb +22 -0
  85. data/module/gems/highline-1.6.19/examples/using_readline.rb +17 -0
  86. data/module/gems/highline-1.6.19/highline.gemspec +37 -0
  87. data/module/gems/highline-1.6.19/lib/highline.rb +1012 -0
  88. data/module/gems/highline-1.6.19/lib/highline/color_scheme.rb +134 -0
  89. data/module/gems/highline-1.6.19/lib/highline/compatibility.rb +16 -0
  90. data/module/gems/highline-1.6.19/lib/highline/import.rb +41 -0
  91. data/module/gems/highline-1.6.19/lib/highline/menu.rb +398 -0
  92. data/module/gems/highline-1.6.19/lib/highline/question.rb +475 -0
  93. data/module/gems/highline-1.6.19/lib/highline/simulate.rb +48 -0
  94. data/module/gems/highline-1.6.19/lib/highline/string_extensions.rb +131 -0
  95. data/module/gems/highline-1.6.19/lib/highline/style.rb +181 -0
  96. data/module/gems/highline-1.6.19/lib/highline/system_extensions.rb +222 -0
  97. data/module/gems/highline-1.6.19/setup.rb +1360 -0
  98. data/module/gems/highline-1.6.19/site/.cvsignore +1 -0
  99. data/module/gems/highline-1.6.19/site/highline.css +65 -0
  100. data/module/gems/highline-1.6.19/site/images/logo.png +0 -0
  101. data/module/gems/highline-1.6.19/site/index.html +58 -0
  102. data/module/gems/highline-1.6.19/test/string_methods.rb +32 -0
  103. data/module/gems/highline-1.6.19/test/tc_color_scheme.rb +96 -0
  104. data/module/gems/highline-1.6.19/test/tc_highline.rb +1128 -0
  105. data/module/gems/highline-1.6.19/test/tc_import.rb +52 -0
  106. data/module/gems/highline-1.6.19/test/tc_menu.rb +439 -0
  107. data/module/gems/highline-1.6.19/test/tc_string_extension.rb +20 -0
  108. data/module/gems/highline-1.6.19/test/tc_string_highline.rb +38 -0
  109. data/module/gems/highline-1.6.19/test/tc_style.rb +567 -0
  110. data/module/gems/highline-1.6.19/test/ts_all.rb +16 -0
  111. data/module/latest_specs.4.8 +0 -0
  112. data/module/latest_specs.4.8.gz +0 -0
  113. data/module/prerelease_specs.4.8 +0 -0
  114. data/module/prerelease_specs.4.8.gz +0 -0
  115. data/module/specs.4.8 +0 -0
  116. data/module/specs.4.8.gz +0 -0
  117. data/samples/blather/restlike/Gemfile +4 -0
  118. data/samples/blather/restlike/cmd.yml +1 -0
  119. data/samples/blather/restlike/lib/blather.rb +9 -0
  120. data/samples/blather/restlike/lib/blather/dsl/api.rb +78 -0
  121. data/samples/blather/restlike/lib/blather/dsl/call.rb +13 -0
  122. data/samples/blather/restlike/lib/blather/dsl/client.rb +58 -0
  123. data/samples/blather/restlike/lib/blather/dsl/config.rb +11 -0
  124. data/samples/blather/restlike/lib/blather/dsl/extraDSL.rb +163 -0
  125. data/samples/blather/restlike/lib/blather/meta/require.rb +8 -0
  126. data/samples/blather/restlike/lib/blather/meta/xmpp.yml +5 -0
  127. data/samples/blather/restlike/lib/blather/vendors/xmpp_default.rb +27 -0
  128. data/samples/blather/restlike/readme +2 -0
  129. data/samples/grape/init/Gemfile +2 -0
  130. data/samples/grape/init/cmd.yml +3 -0
  131. data/samples/grape/init/config.ru +2 -0
  132. data/samples/grape/init/docs/grape/documentation.txt +939 -0
  133. data/samples/grape/init/docs/grape/generate_rest_routes.rb +37 -0
  134. data/samples/grape/init/docs/grape/ls_routes.rb +31 -0
  135. data/samples/grape/init/lib/grape.rb +4 -0
  136. data/samples/grape/init/lib/grape/meta/subclasses.rb +20 -0
  137. data/samples/grape/init/lib/grape/xpath/app.rb +30 -0
  138. data/samples/grape/init/lib/grape/xpath/ruotes.rb +6 -0
  139. data/samples/grape/init/readme +1 -0
  140. data/samples/grape/readme +29 -0
  141. data/samples/grape/vendor/lib/grape/vendors/v1/rest.rb +57 -0
  142. data/samples/mongoid/cmd.yml +1 -0
  143. data/samples/mongoid/init/Gemfile +3 -0
  144. data/samples/mongoid/init/cmd.yml +2 -0
  145. data/samples/mongoid/init/docs/mongoid/ModelsRelations.rb +11 -0
  146. data/samples/mongoid/init/docs/mongoid/documents.xls +0 -0
  147. data/samples/mongoid/init/docs/mongoid/generate_modelsToDocs.rb +25 -0
  148. data/samples/mongoid/init/docs/mongoid/modelsToDocs.rb +25 -0
  149. data/samples/mongoid/init/docs/mongoid/relations.txt +1354 -0
  150. data/samples/mongoid/init/lib/mongoid.rb +44 -0
  151. data/samples/mongoid/init/lib/mongoid/dsl/extraDSL_CRUD.rb +446 -0
  152. data/samples/mongoid/init/lib/mongoid/dsl/extraDSL_MP.rb +517 -0
  153. data/samples/mongoid/init/lib/mongoid/dsl/init.rb +37 -0
  154. data/samples/mongoid/init/lib/mongoid/dsl/params.rb +67 -0
  155. data/samples/mongoid/init/lib/mongoid/meta/banned.rb +147 -0
  156. data/samples/mongoid/init/lib/mongoid/meta/control.yml +13 -0
  157. data/samples/mongoid/init/lib/mongoid/meta/mongoid.yml +6 -0
  158. data/samples/mongoid/init/lib/mongoid/meta/mpatch.rb +14 -0
  159. data/samples/mongoid/model/lib/mongoid/models/model.rb +28 -0
  160. data/samples/mongoid/readme +33 -0
  161. data/samples/rack/init/Gemfile +10 -0
  162. data/samples/rack/init/cmd.yml +2 -0
  163. data/samples/rack/init/config.ru +1 -0
  164. data/samples/rack/init/docs/rack/rake introducing.txt +60 -0
  165. data/samples/rack/init/docs/rack/webservers/Thin +43 -0
  166. data/samples/rack/init/docs/rack/webservers/ebb +72 -0
  167. data/samples/rack/init/docs/rack/webservers/fcgi +103 -0
  168. data/samples/rack/init/docs/rack/webservers/mongrel +74 -0
  169. data/samples/rack/init/docs/rack/webservers/passenger +37 -0
  170. data/samples/rack/init/docs/rack/webservers/scgi +188 -0
  171. data/samples/rack/init/lib/rack.rb +1 -0
  172. data/samples/rack/init/lib/rack/meta/webserver/thin.rb +45 -0
  173. data/samples/rack/init/lib/rack/meta/webserver/thin.yml +6 -0
  174. data/samples/rack/init/server.rb +0 -0
  175. data/samples/rack/readme +13 -0
  176. data/samples/rest_client/init/Gemfile +5 -0
  177. data/samples/rest_client/init/boot.rb +2 -0
  178. data/samples/rest_client/init/cmd.yml +1 -0
  179. data/samples/rest_client/init/config/rest_client/defaults.rb +16 -0
  180. data/samples/rest_client/init/docs/rest_client/simple overlook +251 -0
  181. data/samples/rest_client/init/test/rest_client/rest_dsl.rb +5 -0
  182. data/samples/rest_client/readme +7 -0
  183. data/samples/scripts/lines_counter/lines_number.rb +32 -0
  184. data/samples/scripts/lines_counter/readme +5 -0
  185. data/samples/scripts/readme +1 -0
  186. metadata +197 -7
  187. data/lib/clone/cms.rb +0 -56
  188. data/lib/clone/ext.rb +0 -77
  189. data/sample/test.rb +0 -30
@@ -0,0 +1,27 @@
1
+
2
+ <%= @name %>
3
+ <% if @syntax -%>
4
+
5
+ Usage: <%= @syntax %>
6
+ <% end -%>
7
+ <% if @description || @summary -%>
8
+
9
+ <%= @description || @summary %>
10
+ <% end -%>
11
+ <% unless @examples.empty? -%>
12
+
13
+ Examples:
14
+ <% for description, command in @examples -%>
15
+
16
+ # <%= description %>
17
+ <%= command %>
18
+ <% end -%>
19
+ <% end -%>
20
+ <% unless @options.empty? -%>
21
+
22
+ Options:
23
+ <% for option in @options -%>
24
+ <%= "%-20s %s" % [option[:switches].join(', '), option[:description]] %>
25
+ <% end -%>
26
+ <% end -%>
27
+
@@ -0,0 +1,29 @@
1
+ <%= program :name %>
2
+
3
+ <%= program :description %>
4
+
5
+ Commands:
6
+ <% for name, command in @commands.sort -%>
7
+ <% unless alias? name -%>
8
+ <%= "%-20s %s" % [command.name, command.summary || command.description] %>
9
+ <% end -%>
10
+ <% end -%>
11
+ <% unless @aliases.empty? %>
12
+ Aliases:
13
+ <% for alias_name, args in @aliases.sort -%>
14
+ <%= "%-20s %s %s" % [alias_name, command(alias_name).name, args.join(' ')] %>
15
+ <% end -%>
16
+ <% end %>
17
+ <% unless @options.empty? -%>
18
+ Global Options:
19
+ <% for option in @options -%>
20
+ <%= "%-20s %s" % [option[:switches].join(', '), option[:description]] -%>
21
+ <% end -%>
22
+ <% end -%>
23
+ <% if program :help -%>
24
+ <% for title, body in program(:help) %>
25
+ <%= title %>:
26
+ <%= body %>
27
+ <% end %>
28
+ <% end -%>
29
+
@@ -0,0 +1,10 @@
1
+
2
+ require 'commander'
3
+ require 'commander/delegates'
4
+
5
+ include Commander::UI
6
+ include Commander::UI::AskForClass
7
+ include Commander::Delegates
8
+
9
+ $terminal.wrap_at = HighLine::SystemExtensions.terminal_size.first - 5 rescue 80 if $stdin.tty?
10
+ at_exit { run! }
@@ -0,0 +1,8 @@
1
+
2
+ module Commander
3
+ module Platform
4
+ def self.jruby?
5
+ defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'jruby')
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,411 @@
1
+
2
+ require 'optparse'
3
+
4
+ module Commander
5
+ class Runner
6
+
7
+ #--
8
+ # Exceptions
9
+ #++
10
+
11
+ class CommandError < StandardError; end
12
+ class InvalidCommandError < CommandError; end
13
+
14
+ ##
15
+ # Array of commands.
16
+
17
+ attr_reader :commands
18
+
19
+ ##
20
+ # Global options.
21
+
22
+ attr_reader :options
23
+
24
+ ##
25
+ # Hash of help formatter aliases.
26
+
27
+ attr_reader :help_formatter_aliases
28
+
29
+ ##
30
+ # Initialize a new command runner. Optionally
31
+ # supplying _args_ for mocking, or arbitrary usage.
32
+
33
+ def initialize args = ARGV
34
+ @args, @commands, @aliases, @options = args, {}, {}, []
35
+ @help_formatter_aliases = help_formatter_alias_defaults
36
+ @program = program_defaults
37
+ create_default_commands
38
+ end
39
+
40
+ ##
41
+ # Return singleton Runner instance.
42
+
43
+ def self.instance
44
+ @singleton ||= new
45
+ end
46
+
47
+ ##
48
+ # Run command parsing and execution process.
49
+
50
+ def run!
51
+ trace = false
52
+ require_program :version, :description
53
+ trap('INT') { abort program(:int_message) } if program(:int_message)
54
+ trap('INT') { program(:int_block).call } if program(:int_block)
55
+ global_option('-h', '--help', 'Display help documentation') do
56
+ args = @args - %w[-h --help]
57
+ command(:help).run(*args)
58
+ return
59
+ end
60
+ global_option('-v', '--version', 'Display version information') { say version; return }
61
+ global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true }
62
+ parse_global_options
63
+ remove_global_options options, @args
64
+ unless trace
65
+ begin
66
+ run_active_command
67
+ rescue InvalidCommandError => e
68
+ abort "#{e}. Use --help for more information"
69
+ rescue \
70
+ OptionParser::InvalidOption,
71
+ OptionParser::InvalidArgument,
72
+ OptionParser::MissingArgument => e
73
+ abort e.to_s
74
+ rescue => e
75
+ abort "error: #{e}. Use --trace to view backtrace"
76
+ end
77
+ else
78
+ run_active_command
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Return program version.
84
+
85
+ def version
86
+ '%s %s' % [program(:name), program(:version)]
87
+ end
88
+
89
+ ##
90
+ # Assign program information.
91
+ #
92
+ # === Examples
93
+ #
94
+ # # Set data
95
+ # program :name, 'Commander'
96
+ # program :version, Commander::VERSION
97
+ # program :description, 'Commander utility program.'
98
+ # program :help, 'Copyright', '2008 TJ Holowaychuk'
99
+ # program :help, 'Anything', 'You want'
100
+ # program :int_message 'Bye bye!'
101
+ # program :help_formatter, :compact
102
+ # program :help_formatter, Commander::HelpFormatter::TerminalCompact
103
+ #
104
+ # # Get data
105
+ # program :name # => 'Commander'
106
+ #
107
+ # === Keys
108
+ #
109
+ # :version (required) Program version triple, ex: '0.0.1'
110
+ # :description (required) Program description
111
+ # :name Program name, defaults to basename of executable
112
+ # :help_formatter Defaults to Commander::HelpFormatter::Terminal
113
+ # :help Allows addition of arbitrary global help blocks
114
+ # :int_message Message to display when interrupted (CTRL + C)
115
+ #
116
+
117
+ def program key, *args, &block
118
+ if key == :help and !args.empty?
119
+ @program[:help] ||= {}
120
+ @program[:help][args.first] = args.at(1)
121
+ elsif key == :help_formatter && !args.empty?
122
+ @program[key] = (@help_formatter_aliases[args.first] || args.first)
123
+ elsif block
124
+ @program[key] = block
125
+ else
126
+ unless args.empty?
127
+ @program[key] = (args.count == 1 && args[0]) || args
128
+ end
129
+ @program[key]
130
+ end
131
+ end
132
+
133
+ ##
134
+ # Creates and yields a command instance when a block is passed.
135
+ # Otherwise attempts to return the command, raising InvalidCommandError when
136
+ # it does not exist.
137
+ #
138
+ # === Examples
139
+ #
140
+ # command :my_command do |c|
141
+ # c.when_called do |args|
142
+ # # Code
143
+ # end
144
+ # end
145
+ #
146
+
147
+ def command name, &block
148
+ yield add_command(Commander::Command.new(name)) if block
149
+ @commands[name.to_s]
150
+ end
151
+
152
+ ##
153
+ # Add a global option; follows the same syntax as Command#option
154
+ # This would be used for switches such as --version, --trace, etc.
155
+
156
+ def global_option *args, &block
157
+ switches, description = Runner.separate_switches_from_description *args
158
+ @options << {
159
+ :args => args,
160
+ :proc => block,
161
+ :switches => switches,
162
+ :description => description,
163
+ }
164
+ end
165
+
166
+ ##
167
+ # Alias command _name_ with _alias_name_. Optionally _args_ may be passed
168
+ # as if they were being passed straight to the original command via the command-line.
169
+
170
+ def alias_command alias_name, name, *args
171
+ @commands[alias_name.to_s] = command name
172
+ @aliases[alias_name.to_s] = args
173
+ end
174
+
175
+ ##
176
+ # Default command _name_ to be used when no other
177
+ # command is found in the arguments.
178
+
179
+ def default_command name
180
+ @default_command = name
181
+ end
182
+
183
+ ##
184
+ # Add a command object to this runner.
185
+
186
+ def add_command command
187
+ @commands[command.name] = command
188
+ end
189
+
190
+ ##
191
+ # Check if command _name_ is an alias.
192
+
193
+ def alias? name
194
+ @aliases.include? name.to_s
195
+ end
196
+
197
+ ##
198
+ # Check if a command _name_ exists.
199
+
200
+ def command_exists? name
201
+ @commands[name.to_s]
202
+ end
203
+
204
+ #:stopdoc:
205
+
206
+ ##
207
+ # Get active command within arguments passed to this runner.
208
+
209
+ def active_command
210
+ @__active_command ||= command(command_name_from_args)
211
+ end
212
+
213
+ ##
214
+ # Attempts to locate a command name from within the arguments.
215
+ # Supports multi-word commands, using the largest possible match.
216
+
217
+ def command_name_from_args
218
+ @__command_name_from_args ||= (valid_command_names_from(*@args.dup).sort.last || @default_command)
219
+ end
220
+
221
+ ##
222
+ # Returns array of valid command names found within _args_.
223
+
224
+ def valid_command_names_from *args
225
+ arg_string = args.delete_if { |value| value =~ /^-/ }.join ' '
226
+ commands.keys.find_all { |name| name if /^#{name}\b/.match arg_string }
227
+ end
228
+
229
+ ##
230
+ # Help formatter instance.
231
+
232
+ def help_formatter
233
+ @__help_formatter ||= program(:help_formatter).new self
234
+ end
235
+
236
+ ##
237
+ # Return arguments without the command name.
238
+
239
+ def args_without_command_name
240
+ removed = []
241
+ parts = command_name_from_args.split rescue []
242
+ @args.dup.delete_if do |arg|
243
+ removed << arg if parts.include?(arg) and not removed.include?(arg)
244
+ end
245
+ end
246
+
247
+ ##
248
+ # Returns hash of help formatter alias defaults.
249
+
250
+ def help_formatter_alias_defaults
251
+ return :compact => HelpFormatter::TerminalCompact
252
+ end
253
+
254
+ ##
255
+ # Returns hash of program defaults.
256
+
257
+ def program_defaults
258
+ return :help_formatter => HelpFormatter::Terminal,
259
+ :name => File.basename($0)
260
+ end
261
+
262
+ ##
263
+ # Creates default commands such as 'help' which is
264
+ # essentially the same as using the --help switch.
265
+
266
+ def create_default_commands
267
+ command :help do |c|
268
+ c.syntax = 'commander help [command]'
269
+ c.description = 'Display global or [command] help documentation.'
270
+ c.example 'Display global help', 'command help'
271
+ c.example "Display help for 'foo'", 'command help foo'
272
+ c.when_called do |args, options|
273
+ enable_paging
274
+ if args.empty?
275
+ say help_formatter.render
276
+ else
277
+ command = command args.join(' ')
278
+ begin
279
+ require_valid_command command
280
+ rescue InvalidCommandError => e
281
+ abort "#{e}. Use --help for more information"
282
+ end
283
+ say help_formatter.render_command(command)
284
+ end
285
+ end
286
+ end
287
+ end
288
+
289
+ ##
290
+ # Raises InvalidCommandError when a _command_ is not found.
291
+
292
+ def require_valid_command command = active_command
293
+ raise InvalidCommandError, 'invalid command', caller if command.nil?
294
+ end
295
+
296
+ ##
297
+ # Removes global _options_ from _args_. This prevents an invalid
298
+ # option error from occurring when options are parsed
299
+ # again for the command.
300
+
301
+ def remove_global_options options, args
302
+ # TODO: refactor with flipflop, please TJ ! have time to refactor me !
303
+ options.each do |option|
304
+ switches = option[:switches].dup
305
+ next if switches.empty?
306
+
307
+ if switchHasArg = switches.any? { |s| s =~ /[ =]/ }
308
+ switches.map! { |s| s[0, s.index('=') || s.index(' ') || s.length] }
309
+ end
310
+
311
+ past_switch, arg_removed = false, false
312
+ args.delete_if do |arg|
313
+ if switches.any? { |s| arg[0, s.length] == s }
314
+ arg_removed = !switchHasArg
315
+ past_switch = true
316
+ elsif past_switch && !arg_removed && arg !~ /^-/
317
+ arg_removed = true
318
+ else
319
+ arg_removed = true
320
+ false
321
+ end
322
+ end
323
+ end
324
+ end
325
+
326
+ ##
327
+ # Parse global command options.
328
+
329
+ def parse_global_options
330
+
331
+ parser = options.inject(OptionParser.new) do |options, option|
332
+ options.on *option[:args], &global_option_proc(option[:switches], &option[:proc])
333
+ end
334
+
335
+ options = @args.dup
336
+ begin
337
+ parser.parse!(options)
338
+ rescue OptionParser::InvalidOption => e
339
+ # Remove the offending args and retry.
340
+ options = options.reject { |o| e.args.include?(o) }
341
+ retry
342
+ end
343
+ end
344
+
345
+ ##
346
+ # Returns a proc allowing for commands to inherit global options.
347
+ # This functionality works whether a block is present for the global
348
+ # option or not, so simple switches such as --verbose can be used
349
+ # without a block, and used throughout all commands.
350
+
351
+ def global_option_proc switches, &block
352
+ lambda do |value|
353
+ unless active_command.nil?
354
+ active_command.proxy_options << [Runner.switch_to_sym(switches.last), value]
355
+ end
356
+ yield value if block and !value.nil?
357
+ end
358
+ end
359
+
360
+ ##
361
+ # Raises a CommandError when the program any of the _keys_ are not present, or empty.
362
+
363
+ def require_program *keys
364
+ keys.each do |key|
365
+ raise CommandError, "program #{key} required" if program(key).nil? or program(key).empty?
366
+ end
367
+ end
368
+
369
+ ##
370
+ # Return switches and description separated from the _args_ passed.
371
+
372
+ def self.separate_switches_from_description *args
373
+ switches = args.find_all { |arg| arg.to_s =~ /^-/ }
374
+ description = args.last unless !args.last.is_a? String or args.last.match(/^-/)
375
+ return switches, description
376
+ end
377
+
378
+ ##
379
+ # Attempts to generate a method name symbol from +switch+.
380
+ # For example:
381
+ #
382
+ # -h # => :h
383
+ # --trace # => :trace
384
+ # --some-switch # => :some_switch
385
+ # --[no-]feature # => :feature
386
+ # --file FILE # => :file
387
+ # --list of,things # => :list
388
+ #
389
+
390
+ def self.switch_to_sym switch
391
+ switch.scan(/[\-\]](\w+)/).join('_').to_sym rescue nil
392
+ end
393
+
394
+ ##
395
+ # Run the active command.
396
+
397
+ def run_active_command
398
+ require_valid_command
399
+ if alias? command_name_from_args
400
+ active_command.run *(@aliases[command_name_from_args.to_s] + args_without_command_name)
401
+ else
402
+ active_command.run *args_without_command_name
403
+ end
404
+ end
405
+
406
+ def say *args #:nodoc:
407
+ $terminal.say *args
408
+ end
409
+
410
+ end
411
+ end