commander-openflighthpc 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -176
- data/lib/commander.rb +1 -4
- data/lib/commander/cli.rb +180 -0
- data/lib/commander/import.rb +2 -2
- data/lib/commander/runner.rb +49 -249
- data/lib/commander/version.rb +1 -1
- metadata +15 -7
- data/lib/commander/configure.rb +0 -14
- data/lib/commander/delegates.rb +0 -27
- data/lib/commander/methods.rb +0 -11
- data/lib/commander/user_interaction.rb +0 -551
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f44f3e008d2afe4767d93545e1f96bee55eb972e1b8558f6ed214bf18290079
|
4
|
+
data.tar.gz: fc44c6aa1a350fe8be8d8ea1a79cd3841dc82b59fb5fab14040f4eaac2e199cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b9a7db7d0cc5f8b08c8e2d894ace171c8a7d8527142c8117eb1705fc563d9504d92ddbe6abb56abc94526a9d2a3fb11f226191aa1f629a9a72a42bd95bc8c98
|
7
|
+
data.tar.gz: 55004b7e9ff3f42cbdc5a5d99281d24f9eb88f441bfbc8465fe2ea8792fc817ec8c06ada2a2f0dd66f6820264507fda8ff575941b265faacefa7ab2be32b96e0
|
data/README.md
CHANGED
@@ -115,171 +115,22 @@ require 'rubygems'
|
|
115
115
|
require 'commander'
|
116
116
|
|
117
117
|
class MyApplication
|
118
|
-
|
119
|
-
|
120
|
-
def run
|
121
|
-
program :name, 'Foo Bar'
|
122
|
-
program :version, '1.0.0'
|
123
|
-
program :description, 'Stupid command that prints foo or bar.'
|
124
|
-
|
125
|
-
command :foo do |c|
|
126
|
-
c.syntax = 'foobar foo'
|
127
|
-
c.description = 'Displays foo'
|
128
|
-
c.action do |args, options|
|
129
|
-
say 'foo'
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
run!
|
134
|
-
end
|
135
|
-
end
|
118
|
+
extend Commander::CLI
|
136
119
|
|
137
|
-
MyApplication.new.run if $0 == __FILE__
|
138
|
-
```
|
139
|
-
|
140
|
-
### Block style
|
141
|
-
|
142
|
-
```ruby
|
143
|
-
require 'rubygems'
|
144
|
-
require 'commander'
|
145
|
-
|
146
|
-
Commander.configure do
|
147
120
|
program :name, 'Foo Bar'
|
148
121
|
program :version, '1.0.0'
|
149
122
|
program :description, 'Stupid command that prints foo or bar.'
|
150
123
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
As mentioned above, the highline gem is imported into the global scope. Here
|
158
|
-
are some quick examples for how to utilize highline in your commands:
|
159
|
-
|
160
|
-
```ruby
|
161
|
-
# Ask for password masked with '*' character
|
162
|
-
ask("Password: ") { |q| q.echo = "*" }
|
163
|
-
|
164
|
-
# Ask for password
|
165
|
-
ask("Password: ") { |q| q.echo = false }
|
166
|
-
|
167
|
-
# Ask if the user agrees (yes or no)
|
168
|
-
agree("Do something?")
|
169
|
-
|
170
|
-
# Asks on a single line (note the space after ':')
|
171
|
-
ask("Name: ")
|
172
|
-
|
173
|
-
# Asks with new line after "Description:"
|
174
|
-
ask("Description:")
|
175
|
-
|
176
|
-
# Calls Date#parse to parse the date string passed
|
177
|
-
ask("Birthday? ", Date)
|
178
|
-
|
179
|
-
# Ensures Integer is within the range specified
|
180
|
-
ask("Age? ", Integer) { |q| q.in = 0..105 }
|
181
|
-
|
182
|
-
# Asks for a list of strings, converts to array
|
183
|
-
ask("Fav colors?", Array)
|
184
|
-
```
|
185
|
-
|
186
|
-
## HighLine & Interaction Additions
|
187
|
-
|
188
|
-
In addition to highline's fantastic choice of methods, commander adds the
|
189
|
-
following methods to simplify common tasks:
|
190
|
-
|
191
|
-
```ruby
|
192
|
-
# Ask for password
|
193
|
-
password
|
194
|
-
|
195
|
-
# Ask for password with specific message and mask character
|
196
|
-
password "Enter your password please:", '-'
|
197
|
-
|
198
|
-
# Ask for CLASS, which may be any valid class responding to #parse. Date, Time, Array, etc
|
199
|
-
names = ask_for_array 'Names: '
|
200
|
-
bday = ask_for_date 'Birthday?: '
|
201
|
-
|
202
|
-
# Simple progress bar (Commander::UI::ProgressBar)
|
203
|
-
uris = %w[
|
204
|
-
http://vision-media.ca
|
205
|
-
http://google.com
|
206
|
-
http://yahoo.com
|
207
|
-
]
|
208
|
-
progress uris do |uri|
|
209
|
-
res = open uri
|
210
|
-
# Do something with response
|
211
|
-
end
|
212
|
-
|
213
|
-
# 'Log' action to stdout
|
214
|
-
log "create", "path/to/file.rb"
|
215
|
-
|
216
|
-
# Enable paging of output after this point
|
217
|
-
enable_paging
|
218
|
-
|
219
|
-
# Ask editor for input (EDITOR environment variable or whichever is available: TextMate, vim, vi, emacs, nano, pico)
|
220
|
-
ask_editor
|
221
|
-
|
222
|
-
# Ask editor, supplying initial text
|
223
|
-
ask_editor 'previous data to update'
|
224
|
-
|
225
|
-
# Ask editor, preferring a specific editor
|
226
|
-
ask_editor 'previous data', 'vim'
|
227
|
-
|
228
|
-
# Choose from an array of elements
|
229
|
-
choice = choose("Favorite language?", :ruby, :perl, :js)
|
230
|
-
|
231
|
-
# Alter IO for the duration of the block
|
232
|
-
io new_input, new_output do
|
233
|
-
new_input_contents = $stdin.read
|
234
|
-
puts new_input_contents # outputs to new_output stream
|
235
|
-
end
|
236
|
-
# $stdin / $stdout reset back to original streams
|
237
|
-
|
238
|
-
# Speech synthesis
|
239
|
-
speak 'What is your favorite food? '
|
240
|
-
food = ask 'favorite food?: '
|
241
|
-
speak "Wow, I like #{food} too. We have so much in common."
|
242
|
-
speak "I like #{food} as well!", "Victoria", 190
|
243
|
-
|
244
|
-
# Execute arbitrary applescript
|
245
|
-
applescript 'foo'
|
246
|
-
|
247
|
-
# Converse with speech recognition server
|
248
|
-
case converse 'What is the best food?', :cookies => 'Cookies', :unknown => 'Nothing'
|
249
|
-
when :cookies
|
250
|
-
speak 'o.m.g. you are awesome!'
|
251
|
-
else
|
252
|
-
case converse 'That is lame, shall I convince you cookies are the best?', :yes => 'Ok', :no => 'No', :maybe => 'Maybe another time'
|
253
|
-
when :yes
|
254
|
-
speak 'Well you see, cookies are just fantastic, they melt in your mouth.'
|
255
|
-
else
|
256
|
-
speak 'Ok then, bye.'
|
124
|
+
command :foo do |c|
|
125
|
+
c.syntax = 'foobar foo'
|
126
|
+
c.description = 'Displays foo'
|
127
|
+
c.action do |args, options|
|
128
|
+
say 'foo'
|
129
|
+
end
|
257
130
|
end
|
258
131
|
end
|
259
|
-
```
|
260
|
-
|
261
|
-
## Growl Notifications
|
262
|
-
|
263
|
-
Commander provides methods for displaying Growl notifications. To use these
|
264
|
-
methods you need to install http://github.com/tj/growl which utilizes
|
265
|
-
the [growlnotify](http://growl.info/extras.php#growlnotify) executable. Note that
|
266
|
-
growl is auto-imported by Commander when available, no need to require.
|
267
|
-
|
268
|
-
```ruby
|
269
|
-
# Display a generic Growl notification
|
270
|
-
notify 'Something happened'
|
271
|
-
|
272
|
-
# Display an 'info' status notification
|
273
|
-
notify_info 'You have #{emails.length} new email(s)'
|
274
132
|
|
275
|
-
|
276
|
-
notify_ok 'Gems updated'
|
277
|
-
|
278
|
-
# Display a 'warning' status notification
|
279
|
-
notify_warning '1 gem failed installation'
|
280
|
-
|
281
|
-
# Display an 'error' status notification
|
282
|
-
notify_error "Gem #{name} failed"
|
133
|
+
MyApplication.run!(ARGV) if $0 == __FILE__
|
283
134
|
```
|
284
135
|
|
285
136
|
## Commander Goodies
|
@@ -439,25 +290,7 @@ This abstraction could be utilized to generate HTML documentation for your execu
|
|
439
290
|
|
440
291
|
### Tracing
|
441
292
|
|
442
|
-
|
443
|
-
|
444
|
-
You can disable these options:
|
445
|
-
|
446
|
-
```ruby
|
447
|
-
never_trace!
|
448
|
-
```
|
449
|
-
|
450
|
-
Or make it always on:
|
451
|
-
|
452
|
-
```ruby
|
453
|
-
always_trace!
|
454
|
-
```
|
455
|
-
|
456
|
-
Include the `--trace` global option but turn off the error message prompt:
|
457
|
-
|
458
|
-
```ruby
|
459
|
-
silent_trace!
|
460
|
-
```
|
293
|
+
WIP: Update OpenFlight --trace behaviour
|
461
294
|
|
462
295
|
## Tips
|
463
296
|
|
data/lib/commander.rb
CHANGED
@@ -25,12 +25,9 @@ require 'highline'
|
|
25
25
|
$terminal = HighLine.new
|
26
26
|
require 'commander/version'
|
27
27
|
require 'commander/blank'
|
28
|
-
require 'commander/user_interaction'
|
29
28
|
require 'commander/core_ext'
|
30
29
|
require 'commander/runner'
|
31
30
|
require 'commander/command'
|
32
31
|
require 'commander/help_formatters'
|
33
32
|
require 'commander/platform'
|
34
|
-
require 'commander/
|
35
|
-
require 'commander/methods'
|
36
|
-
require 'commander/configure'
|
33
|
+
require 'commander/cli'
|
@@ -0,0 +1,180 @@
|
|
1
|
+
module Commander
|
2
|
+
module CLI
|
3
|
+
##
|
4
|
+
# Wrapper run command with error handling
|
5
|
+
def run!(*args)
|
6
|
+
instance = Runner.new(
|
7
|
+
@program, commands, default_command,
|
8
|
+
global_options, aliases, args
|
9
|
+
)
|
10
|
+
instance.run
|
11
|
+
rescue StandardError, Interrupt => e
|
12
|
+
$stderr.puts e.backtrace.reverse if args.include?('--trace')
|
13
|
+
error_handler(instance, e)
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Assign program information.
|
18
|
+
#
|
19
|
+
# === Examples
|
20
|
+
#
|
21
|
+
# # Set data
|
22
|
+
# program :name, 'Commander'
|
23
|
+
# program :version, Commander::VERSION
|
24
|
+
# program :description, 'Commander utility program.'
|
25
|
+
# program :help, 'Copyright', '2008 TJ Holowaychuk'
|
26
|
+
# program :help, 'Anything', 'You want'
|
27
|
+
# program :help_formatter, :compact
|
28
|
+
# program :help_formatter, Commander::HelpFormatter::TerminalCompact
|
29
|
+
#
|
30
|
+
# # Get data
|
31
|
+
# program :name # => 'Commander'
|
32
|
+
#
|
33
|
+
# === Keys
|
34
|
+
#
|
35
|
+
# :version (required) Program version triple, ex: '0.0.1'
|
36
|
+
# :description (required) Program description
|
37
|
+
# :name Program name, defaults to basename of executable
|
38
|
+
# :help_formatter Defaults to Commander::HelpFormatter::Terminal
|
39
|
+
# :help Allows addition of arbitrary global help blocks
|
40
|
+
# :help_paging Flag for toggling help paging
|
41
|
+
#
|
42
|
+
|
43
|
+
def program(key, *args, &block)
|
44
|
+
@program ||= {
|
45
|
+
help_formatter: HelpFormatter::Terminal,
|
46
|
+
name: File.basename($PROGRAM_NAME),
|
47
|
+
help_paging: true,
|
48
|
+
}
|
49
|
+
|
50
|
+
if key == :help && !args.empty?
|
51
|
+
@program[:help] ||= {}
|
52
|
+
@program[:help][args.first] = args.at(1)
|
53
|
+
elsif key == :help_formatter && !args.empty?
|
54
|
+
@program[key] = (@help_formatter_aliases[args.first] || args.first)
|
55
|
+
elsif block
|
56
|
+
@program[key] = block
|
57
|
+
else
|
58
|
+
unless args.empty?
|
59
|
+
@program[key] = args.count == 1 ? args[0] : args
|
60
|
+
end
|
61
|
+
@program[key]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Default command _name_ to be used when no other
|
67
|
+
# command is found in the arguments.
|
68
|
+
|
69
|
+
def default_command(name = nil)
|
70
|
+
@default_command = name unless name.nil?
|
71
|
+
@default_command
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
##
|
76
|
+
# Hash of Command objects
|
77
|
+
def commands
|
78
|
+
@commands ||= {}
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# Hash of Global Options
|
83
|
+
def global_options
|
84
|
+
@global_options ||= begin
|
85
|
+
@global_options = [] # Allows Recursive - Refactor
|
86
|
+
global_option('-h', '--help', 'Display help documentation') do
|
87
|
+
args = @args - %w(-h --help)
|
88
|
+
command(:help).run(*args)
|
89
|
+
exit 0
|
90
|
+
end
|
91
|
+
global_option('--version', 'Display version information') do
|
92
|
+
say version
|
93
|
+
exit 0
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
##
|
99
|
+
# Add a global option; follows the same syntax as Command#option
|
100
|
+
# This would be used for switches such as --version
|
101
|
+
# NOTE: --trace is special and does not appear in the help
|
102
|
+
# It is intended for debugging purposes
|
103
|
+
|
104
|
+
def global_option(*args, &block)
|
105
|
+
switches, description = Runner.separate_switches_from_description(*args)
|
106
|
+
global_options << {
|
107
|
+
args: args,
|
108
|
+
proc: block,
|
109
|
+
switches: switches,
|
110
|
+
description: description,
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# Define and get a command by name
|
116
|
+
#
|
117
|
+
def command(name)
|
118
|
+
name = name.to_s
|
119
|
+
(commands[name] ||= Command.new(name)).tap do |cmd|
|
120
|
+
yield cmd if block_given?
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# A hash of known aliases
|
125
|
+
def aliases
|
126
|
+
@aliases ||= {}
|
127
|
+
end
|
128
|
+
|
129
|
+
##
|
130
|
+
# Alias command _name_ with _alias_name_. Optionally _args_ may be passed
|
131
|
+
# as if they were being passed straight to the original command via the command-line.
|
132
|
+
|
133
|
+
def alias_command(alias_name, name, *args)
|
134
|
+
commands[alias_name.to_s] = command name
|
135
|
+
aliases[alias_name.to_s] = args
|
136
|
+
end
|
137
|
+
|
138
|
+
def error_handler(runner, e)
|
139
|
+
error_msg = "#{Paint[runner.program(:name), '#2794d8']}: #{Paint[e.to_s, :red, :bright]}"
|
140
|
+
exit_code = e.respond_to?(:exit_code) ? e.exit_code.to_i : 1
|
141
|
+
case e
|
142
|
+
when OptionParser::InvalidOption,
|
143
|
+
Commander::Runner::InvalidCommandError,
|
144
|
+
Commander::Patches::CommandUsageError
|
145
|
+
# Display the error message for a specific command. Most likely due to
|
146
|
+
# invalid command syntax
|
147
|
+
if cmd = runner.active_command
|
148
|
+
$stderr.puts error_msg
|
149
|
+
$stderr.puts "\nUsage:\n\n"
|
150
|
+
runner.command('help').run(cmd.name)
|
151
|
+
# Display the main app help text when called without `--help`
|
152
|
+
elsif runner.args_without_command_name.empty?
|
153
|
+
$stderr.puts "Usage:\n\n"
|
154
|
+
runner.command('help').run(:error)
|
155
|
+
# Display the main app help text when called with arguments. Mostly
|
156
|
+
# likely an invalid syntax error
|
157
|
+
else
|
158
|
+
$stderr.puts error_msg
|
159
|
+
$stderr.puts "\nUsage:\n\n"
|
160
|
+
runner.command('help').run(:error)
|
161
|
+
end
|
162
|
+
exit_code = 254
|
163
|
+
# Display the help text for sub command groups when called without `--help`
|
164
|
+
when SubCommandGroupError
|
165
|
+
if cmd = runner.active_command
|
166
|
+
$stderr.puts "Usage:\n\n"
|
167
|
+
runner.command('help').run(cmd.name)
|
168
|
+
end
|
169
|
+
exit_code = 254
|
170
|
+
when Interrupt
|
171
|
+
$stderr.puts 'Received Interrupt!'
|
172
|
+
exit_code = 255
|
173
|
+
# Catch all error message for all other issues
|
174
|
+
else
|
175
|
+
$stderr.puts error_msg
|
176
|
+
end
|
177
|
+
exit(exit_code)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
data/lib/commander/import.rb
CHANGED
data/lib/commander/runner.rb
CHANGED
@@ -2,42 +2,6 @@ require 'paint'
|
|
2
2
|
|
3
3
|
module Commander
|
4
4
|
class Runner
|
5
|
-
DEFAULT_ERROR_HANDLER = lambda do |runner, e|
|
6
|
-
error_msg = "#{Paint[runner.program(:name), '#2794d8']}: #{Paint[e.to_s, :red, :bright]}"
|
7
|
-
case e
|
8
|
-
when OptionParser::InvalidOption,
|
9
|
-
Commander::Runner::InvalidCommandError,
|
10
|
-
Commander::Patches::CommandUsageError
|
11
|
-
# Display the error message for a specific command. Most likely due to
|
12
|
-
# invalid command syntax
|
13
|
-
if cmd = runner.active_command
|
14
|
-
$stderr.puts error_msg
|
15
|
-
$stderr.puts "\nUsage:\n\n"
|
16
|
-
runner.command('help').run(cmd.name)
|
17
|
-
# Display the main app help text when called without `--help`
|
18
|
-
elsif runner.args_without_command_name.empty?
|
19
|
-
$stderr.puts "Usage:\n\n"
|
20
|
-
runner.command('help').run(:error)
|
21
|
-
# Display the main app help text when called with arguments. Mostly
|
22
|
-
# likely an invalid syntax error
|
23
|
-
else
|
24
|
-
$stderr.puts error_msg
|
25
|
-
$stderr.puts "\nUsage:\n\n"
|
26
|
-
runner.command('help').run(:error)
|
27
|
-
end
|
28
|
-
# Display the help text for sub command groups when called without `--help`
|
29
|
-
when SubCommandGroupError
|
30
|
-
if cmd = runner.active_command
|
31
|
-
$stderr.puts "Usage:\n\n"
|
32
|
-
runner.command('help').run(cmd.name)
|
33
|
-
end
|
34
|
-
# Catch all error message for all other issues
|
35
|
-
else
|
36
|
-
$stderr.puts error_msg
|
37
|
-
end
|
38
|
-
exit(1)
|
39
|
-
end
|
40
|
-
|
41
5
|
#--
|
42
6
|
# Exceptions
|
43
7
|
#++
|
@@ -63,67 +27,37 @@ module Commander
|
|
63
27
|
# Initialize a new command runner. Optionally
|
64
28
|
# supplying _args_ for mocking, or arbitrary usage.
|
65
29
|
|
66
|
-
def initialize(args = ARGV)
|
67
|
-
@args, @commands, @aliases, @options = args, {}, {}, []
|
68
|
-
@help_formatter_aliases = help_formatter_alias_defaults
|
69
|
-
@program = program_defaults
|
70
|
-
@always_trace = false
|
71
|
-
@never_trace = false
|
72
|
-
@silent_trace = false
|
73
|
-
@error_handler = DEFAULT_ERROR_HANDLER
|
74
|
-
create_default_commands
|
75
|
-
end
|
76
|
-
|
77
30
|
##
|
78
|
-
#
|
31
|
+
# Display the backtrace in the event of an error
|
32
|
+
attr_accessor :trace
|
79
33
|
|
80
|
-
def
|
81
|
-
@
|
34
|
+
def initialize(*inputs)
|
35
|
+
@program, @commands, @default_command, \
|
36
|
+
@options, @aliases, @args = inputs.map(&:dup)
|
37
|
+
@args.reject! { |a| a == '--trace' }
|
38
|
+
@commands['help'] ||= Command.new('help').tap do |c|
|
39
|
+
c.syntax = "#{program(:name)} help [command]"
|
40
|
+
c.description = 'Display global or [command] help documentation'
|
41
|
+
c.example 'Display global help', "#{program(:name)} help"
|
42
|
+
c.example "Display help for 'foo'", "#{program(:name)} help foo"
|
43
|
+
c.when_called do |help_args, _|
|
44
|
+
self.run_help_command(help_args)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
@help_formatter_aliases = help_formatter_alias_defaults
|
82
48
|
end
|
83
49
|
|
84
50
|
##
|
85
|
-
# Run command parsing and execution process
|
51
|
+
# Run command parsing and execution process
|
52
|
+
# NOTE: This method does not have error handling, see: run!
|
86
53
|
|
87
|
-
def run
|
88
|
-
trace = @always_trace || false
|
54
|
+
def run
|
89
55
|
require_program :version, :description
|
90
|
-
|
91
|
-
trap('INT') { program(:int_block).call } if program(:int_block)
|
92
|
-
global_option('-h', '--help', 'Display help documentation') do
|
93
|
-
args = @args - %w(-h --help)
|
94
|
-
command(:help).run(*args)
|
95
|
-
return
|
96
|
-
end
|
97
|
-
global_option('--version', 'Display version information') do
|
98
|
-
say version
|
99
|
-
return
|
100
|
-
end
|
101
|
-
global_option('--trace', 'Display backtrace when an error occurs') { trace = true } unless @never_trace || @always_trace
|
56
|
+
|
102
57
|
parse_global_options
|
103
58
|
remove_global_options options, @args
|
104
|
-
|
105
|
-
|
106
|
-
else
|
107
|
-
begin
|
108
|
-
run_active_command
|
109
|
-
rescue InvalidCommandError => e
|
110
|
-
error_handler&.call(self, e) ||
|
111
|
-
abort("#{e}. Use --help for more information")
|
112
|
-
rescue \
|
113
|
-
OptionParser::InvalidOption,
|
114
|
-
OptionParser::InvalidArgument,
|
115
|
-
OptionParser::MissingArgument => e
|
116
|
-
error_handler&.call(self, e) ||
|
117
|
-
abort(e.to_s)
|
118
|
-
rescue => e
|
119
|
-
error_handler&.call(self, e) ||
|
120
|
-
if @never_trace || @silent_trace
|
121
|
-
abort("error: #{e}.")
|
122
|
-
else
|
123
|
-
abort("error: #{e}. Use --trace to view backtrace")
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
59
|
+
|
60
|
+
run_active_command
|
127
61
|
end
|
128
62
|
|
129
63
|
##
|
@@ -134,83 +68,11 @@ module Commander
|
|
134
68
|
end
|
135
69
|
|
136
70
|
##
|
137
|
-
#
|
138
|
-
|
139
|
-
def always_trace!
|
140
|
-
@always_trace = true
|
141
|
-
@never_trace = false
|
142
|
-
@silent_trace = false
|
143
|
-
end
|
144
|
-
|
145
|
-
##
|
146
|
-
# Hide the trace option from the help menus and don't add it as a global option
|
147
|
-
|
148
|
-
def never_trace!
|
149
|
-
@always_trace = false
|
150
|
-
@never_trace = true
|
151
|
-
@silent_trace = false
|
152
|
-
end
|
153
|
-
|
154
|
-
##
|
155
|
-
# Includes the trace option in the help but not in the error message
|
156
|
-
|
157
|
-
def silent_trace!
|
158
|
-
@always_trace = false
|
159
|
-
@never_trace = false
|
160
|
-
@silent_trace = true
|
161
|
-
end
|
162
|
-
|
163
|
-
##
|
164
|
-
# Set a handler to be used for advanced exception handling
|
165
|
-
|
166
|
-
def error_handler(&block)
|
167
|
-
@error_handler = block if block
|
168
|
-
@error_handler
|
169
|
-
end
|
170
|
-
|
171
|
-
##
|
172
|
-
# Assign program information.
|
173
|
-
#
|
174
|
-
# === Examples
|
175
|
-
#
|
176
|
-
# # Set data
|
177
|
-
# program :name, 'Commander'
|
178
|
-
# program :version, Commander::VERSION
|
179
|
-
# program :description, 'Commander utility program.'
|
180
|
-
# program :help, 'Copyright', '2008 TJ Holowaychuk'
|
181
|
-
# program :help, 'Anything', 'You want'
|
182
|
-
# program :int_message 'Bye bye!'
|
183
|
-
# program :help_formatter, :compact
|
184
|
-
# program :help_formatter, Commander::HelpFormatter::TerminalCompact
|
185
|
-
#
|
186
|
-
# # Get data
|
187
|
-
# program :name # => 'Commander'
|
71
|
+
# The hash of program variables
|
188
72
|
#
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
# :description (required) Program description
|
193
|
-
# :name Program name, defaults to basename of executable
|
194
|
-
# :help_formatter Defaults to Commander::HelpFormatter::Terminal
|
195
|
-
# :help Allows addition of arbitrary global help blocks
|
196
|
-
# :help_paging Flag for toggling help paging
|
197
|
-
# :int_message Message to display when interrupted (CTRL + C)
|
198
|
-
#
|
199
|
-
|
200
|
-
def program(key, *args, &block)
|
201
|
-
if key == :help && !args.empty?
|
202
|
-
@program[:help] ||= {}
|
203
|
-
@program[:help][args.first] = args.at(1)
|
204
|
-
elsif key == :help_formatter && !args.empty?
|
205
|
-
@program[key] = (@help_formatter_aliases[args.first] || args.first)
|
206
|
-
elsif block
|
207
|
-
@program[key] = block
|
208
|
-
else
|
209
|
-
unless args.empty?
|
210
|
-
@program[key] = args.count == 1 ? args[0] : args
|
211
|
-
end
|
212
|
-
@program[key]
|
213
|
-
end
|
73
|
+
def program(key, default = nil)
|
74
|
+
@program[key] ||= default if default
|
75
|
+
@program[key]
|
214
76
|
end
|
215
77
|
|
216
78
|
##
|
@@ -227,49 +89,10 @@ module Commander
|
|
227
89
|
# end
|
228
90
|
#
|
229
91
|
|
230
|
-
def command(name
|
231
|
-
yield add_command(Commander::Command.new(name)) if block
|
92
|
+
def command(name)
|
232
93
|
@commands[name.to_s]
|
233
94
|
end
|
234
95
|
|
235
|
-
##
|
236
|
-
# Add a global option; follows the same syntax as Command#option
|
237
|
-
# This would be used for switches such as --version, --trace, etc.
|
238
|
-
|
239
|
-
def global_option(*args, &block)
|
240
|
-
switches, description = Runner.separate_switches_from_description(*args)
|
241
|
-
@options << {
|
242
|
-
args: args,
|
243
|
-
proc: block,
|
244
|
-
switches: switches,
|
245
|
-
description: description,
|
246
|
-
}
|
247
|
-
end
|
248
|
-
|
249
|
-
##
|
250
|
-
# Alias command _name_ with _alias_name_. Optionally _args_ may be passed
|
251
|
-
# as if they were being passed straight to the original command via the command-line.
|
252
|
-
|
253
|
-
def alias_command(alias_name, name, *args)
|
254
|
-
@commands[alias_name.to_s] = command name
|
255
|
-
@aliases[alias_name.to_s] = args
|
256
|
-
end
|
257
|
-
|
258
|
-
##
|
259
|
-
# Default command _name_ to be used when no other
|
260
|
-
# command is found in the arguments.
|
261
|
-
|
262
|
-
def default_command(name)
|
263
|
-
@default_command = name
|
264
|
-
end
|
265
|
-
|
266
|
-
##
|
267
|
-
# Add a command object to this runner.
|
268
|
-
|
269
|
-
def add_command(command)
|
270
|
-
@commands[command.name] = command
|
271
|
-
end
|
272
|
-
|
273
96
|
##
|
274
97
|
# Check if command _name_ is an alias.
|
275
98
|
|
@@ -336,17 +159,6 @@ module Commander
|
|
336
159
|
}
|
337
160
|
end
|
338
161
|
|
339
|
-
##
|
340
|
-
# Returns hash of program defaults.
|
341
|
-
|
342
|
-
def program_defaults
|
343
|
-
{
|
344
|
-
help_formatter: HelpFormatter::Terminal,
|
345
|
-
name: File.basename($PROGRAM_NAME),
|
346
|
-
help_paging: true,
|
347
|
-
}
|
348
|
-
end
|
349
|
-
|
350
162
|
##
|
351
163
|
# Limit commands to those which are subcommands of the one that is active
|
352
164
|
def limit_commands_to_subcommands(command)
|
@@ -368,39 +180,25 @@ module Commander
|
|
368
180
|
##
|
369
181
|
# Creates default commands such as 'help' which is
|
370
182
|
# essentially the same as using the --help switch.
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
command = command args.join(' ')
|
391
|
-
begin
|
392
|
-
require_valid_command command
|
393
|
-
rescue InvalidCommandError => e
|
394
|
-
error_handler&.call(self, e) ||
|
395
|
-
abort("#{e}. Use --help for more information")
|
396
|
-
end
|
397
|
-
if command.sub_command_group?
|
398
|
-
limit_commands_to_subcommands(command)
|
399
|
-
say help_formatter.render_subcommand(command)
|
400
|
-
else
|
401
|
-
say help_formatter.render_command(command)
|
402
|
-
end
|
403
|
-
end
|
183
|
+
def run_help_command(args)
|
184
|
+
UI.enable_paging if program(:help_paging)
|
185
|
+
@help_commands = @commands.dup
|
186
|
+
if args.empty? || args[0] == :error
|
187
|
+
@help_options = @options
|
188
|
+
@help_commands.reject! { |k, v| !!v.hidden }
|
189
|
+
old_wrap = $terminal.wrap_at
|
190
|
+
$terminal.wrap_at = nil
|
191
|
+
program(:nobanner, true) if args[0] == :error
|
192
|
+
say help_formatter.render
|
193
|
+
$terminal.wrap_at = old_wrap
|
194
|
+
else
|
195
|
+
command = command args.join(' ')
|
196
|
+
require_valid_command command
|
197
|
+
if command.sub_command_group?
|
198
|
+
limit_commands_to_subcommands(command)
|
199
|
+
say help_formatter.render_subcommand(command)
|
200
|
+
else
|
201
|
+
say help_formatter.render_command(command)
|
404
202
|
end
|
405
203
|
end
|
406
204
|
end
|
@@ -487,7 +285,9 @@ module Commander
|
|
487
285
|
unless active_command.nil?
|
488
286
|
active_command.proxy_options << [Runner.switch_to_sym(switches.last), value]
|
489
287
|
end
|
490
|
-
|
288
|
+
if block && !value.nil?
|
289
|
+
instance_exec(value, &block)
|
290
|
+
end
|
491
291
|
end
|
492
292
|
end
|
493
293
|
|