commander 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +275 -0
- data/Manifest +36 -0
- data/README.rdoc +357 -0
- data/Rakefile +16 -0
- data/bin/commander +55 -0
- data/commander.gemspec +35 -0
- data/lib/commander.rb +31 -0
- data/lib/commander/blank.rb +8 -0
- data/lib/commander/command.rb +210 -0
- data/lib/commander/core_ext.rb +3 -0
- data/lib/commander/core_ext/array.rb +25 -0
- data/lib/commander/core_ext/object.rb +11 -0
- data/lib/commander/delegates.rb +13 -0
- data/lib/commander/help_formatters.rb +8 -0
- data/lib/commander/help_formatters/base.rb +18 -0
- data/lib/commander/help_formatters/terminal.rb +20 -0
- data/lib/commander/help_formatters/terminal/command_help.erb +35 -0
- data/lib/commander/help_formatters/terminal/help.erb +36 -0
- data/lib/commander/help_formatters/terminal_compact.rb +12 -0
- data/lib/commander/help_formatters/terminal_compact/command_help.erb +27 -0
- data/lib/commander/help_formatters/terminal_compact/help.erb +29 -0
- data/lib/commander/import.rb +10 -0
- data/lib/commander/runner.rb +386 -0
- data/lib/commander/user_interaction.rb +422 -0
- data/lib/commander/version.rb +4 -0
- data/spec/command_spec.rb +125 -0
- data/spec/core_ext/array_spec.rb +14 -0
- data/spec/core_ext/object_spec.rb +20 -0
- data/spec/help_formatters/base_spec.rb +24 -0
- data/spec/help_formatters/terminal_spec.rb +66 -0
- data/spec/runner_spec.rb +326 -0
- data/spec/spec_helper.rb +53 -0
- data/spec/ui_spec.rb +11 -0
- data/tasks/docs.rake +18 -0
- data/tasks/gemspec.rake +3 -0
- data/tasks/spec.rake +25 -0
- metadata +127 -0
data/History.rdoc
ADDED
@@ -0,0 +1,275 @@
|
|
1
|
+
|
2
|
+
=== 3.3.0 / 2009-05-12
|
3
|
+
|
4
|
+
* Added #choose
|
5
|
+
* Added aliasing for help formatters [#24]
|
6
|
+
* Added UI.replace_tokens
|
7
|
+
* Added #converse
|
8
|
+
* Added #speak
|
9
|
+
* Added #applescript
|
10
|
+
* Added #reset_io
|
11
|
+
* Added #io
|
12
|
+
* Removed String#tokenize
|
13
|
+
* Removed #delete_switches
|
14
|
+
|
15
|
+
=== 3.2.9 / 2009-04-09
|
16
|
+
|
17
|
+
* Fixed bug in removal of global options
|
18
|
+
|
19
|
+
=== 3.2.8 / 2009-04-09
|
20
|
+
|
21
|
+
* Added auto-loading support of visionmedia-growl convenience methods when the gem is available
|
22
|
+
* Changed; growl related methods are now part of visionmedia-growl
|
23
|
+
* Fixed doc typos
|
24
|
+
|
25
|
+
=== 3.2.7 / 2009-04-08
|
26
|
+
|
27
|
+
* Added #notify, #notify_ok, #notify_warning, #notify_info and #notify_error
|
28
|
+
These methods all utilize the MacOS growlnotify binary, very cool stuff check it out!
|
29
|
+
|
30
|
+
=== 3.2.6 / 2009-04-08
|
31
|
+
|
32
|
+
* Added Commander::UI::AskForClass
|
33
|
+
* Added support to #remove_global_options for options with arguments
|
34
|
+
* Removed .re directory used for visionmedia-release
|
35
|
+
* Fixed bug preventing --trace from working
|
36
|
+
|
37
|
+
=== 3.2.5 / 2009-04-02
|
38
|
+
|
39
|
+
* Added #ask_editor
|
40
|
+
|
41
|
+
=== 3.2.4 / 2009-04-02
|
42
|
+
|
43
|
+
* Added #enable_paging [#9]
|
44
|
+
|
45
|
+
=== 3.2.3 / 2009-04-01
|
46
|
+
|
47
|
+
* Added new pager using less [#9]
|
48
|
+
|
49
|
+
=== 3.2.2 / 2009-04-01
|
50
|
+
|
51
|
+
* Github's gem builder timed out ? ...
|
52
|
+
|
53
|
+
=== 3.2.1 / 2009-04-01
|
54
|
+
|
55
|
+
* Added Commander::HelpFormatter::TerminalCompact
|
56
|
+
* Fix; updated gemspec / manifest removing test/* causing issue [#26]
|
57
|
+
|
58
|
+
=== 3.2.0 / 2009-03-26
|
59
|
+
|
60
|
+
* Added implied small switches so they appear in help (-h, -v, etc)
|
61
|
+
* Added #inspect back to Commander::Command::Options [#1]
|
62
|
+
* Added inheritance of global options for commands [#7]
|
63
|
+
* Added #require_valid_command
|
64
|
+
* Renamed #call_active_command to #run_active_command
|
65
|
+
* Changed; using same option format as command options for globals [#18]
|
66
|
+
* Changed; program :name is now optional, and is auto-defined when not specified [#21]
|
67
|
+
* Moved #switch_to_sym from Command to Commander::Runner
|
68
|
+
* Moved #separate_switches_from_description into Commander::Runner [#22]
|
69
|
+
* Removed program :name from commander init template since its not required anymore
|
70
|
+
|
71
|
+
=== 3.1.8 / 2009-03-25
|
72
|
+
|
73
|
+
* Utilizing abort and $stderr instead of using #say [#16]
|
74
|
+
* Fixed INT trapping issue (purely cosmetic) [#14]
|
75
|
+
* Removed todo, use lighthouse now for commander at:
|
76
|
+
http://visionmedia.lighthouseapp.com/projects/27643-commander/overview
|
77
|
+
|
78
|
+
=== 3.1.7 / 2009-03-24
|
79
|
+
|
80
|
+
* Added global --trace option
|
81
|
+
|
82
|
+
=== 3.1.6 / 2009-03-22
|
83
|
+
|
84
|
+
* Changed Options struct to always use symbols
|
85
|
+
|
86
|
+
=== 3.1.5 / 2009-03-22
|
87
|
+
|
88
|
+
* Added Options#__hash__ to access the internal hash table
|
89
|
+
* Refactored Options struct to function much like an OpenStruct
|
90
|
+
* Updated commander exec init help description
|
91
|
+
|
92
|
+
=== 3.1.4 / 2009-03-20
|
93
|
+
|
94
|
+
* Gemspec typo
|
95
|
+
|
96
|
+
=== 3.1.3 / 2009-03-20
|
97
|
+
|
98
|
+
* Added #remove_global_options
|
99
|
+
* Added core global option descriptions
|
100
|
+
* Added display of global options
|
101
|
+
* Removed small core switches (-v, -h) because they are implicitly set
|
102
|
+
* Fixed issue with option switchs failing when named as common methods like --send, or --password
|
103
|
+
* Fixed bug causing InvalidOption error when global options do not abort the process.
|
104
|
+
This was due to these options remaining in the arguments array, in turn being parsed
|
105
|
+
by the command's option parser, issuing the error. This is fixed by #remove_global_options.
|
106
|
+
|
107
|
+
=== 3.1.2 / 2009-03-16
|
108
|
+
|
109
|
+
* Added support for global options via #global_option
|
110
|
+
* Added #call_active_command and #say to clean things up internally
|
111
|
+
* Misc refactoring
|
112
|
+
|
113
|
+
=== 3.1.1 / 2009-03-13
|
114
|
+
|
115
|
+
* Fixed some terminal formatter spacing issues
|
116
|
+
|
117
|
+
=== 3.1.0 / 2009-03-13
|
118
|
+
|
119
|
+
* Added Command#inspect
|
120
|
+
* Added displaying of aliases in global help
|
121
|
+
* Added support for alias args
|
122
|
+
* Added #default_command
|
123
|
+
|
124
|
+
=== 3.0.3 / 2009-03-12
|
125
|
+
|
126
|
+
* Added #alias_command
|
127
|
+
|
128
|
+
=== 3.0.2 / 2009-03-12
|
129
|
+
|
130
|
+
* Added terminal formatter specs
|
131
|
+
* Fixed bug preventing command names passed to help from working
|
132
|
+
|
133
|
+
=== 3.0.1 / 2009-03-12
|
134
|
+
|
135
|
+
* Fixed bug in #command_name_from_args preventing the left-most match for a command name to be used
|
136
|
+
* Fixed bug in Command#example preventing the formatter from outputting them correctly
|
137
|
+
|
138
|
+
=== 3.0.0 / 2009-03-12
|
139
|
+
|
140
|
+
* Added sub command help support when using the --help switch
|
141
|
+
* #delete_switches is now mutative
|
142
|
+
* Added #valid_command_names_from
|
143
|
+
* #when_called now raises argument error when neither a block, object, or class is passed
|
144
|
+
* Added options#default method and specs
|
145
|
+
* Added support for multi-word list parsing in Array#parse
|
146
|
+
* Added summary to commander template
|
147
|
+
* Added optional block param for #new_command_runner spec helper
|
148
|
+
* Added .re (metadata for visionmedia-release gem)
|
149
|
+
* Moved delgation of Kernel methods to Object
|
150
|
+
* Refactored multi-word command name specs
|
151
|
+
* Refactored Object#method_missing; using call chain to implement
|
152
|
+
* Refactored all specs
|
153
|
+
* Change #color calls in formatters to $terminal#color for now
|
154
|
+
* Using highlines $terminal.output for IO stream with the progressbar
|
155
|
+
* Replaced #ensure_program_key_set with #require_program, taking an array of keys
|
156
|
+
* Renamed #args_without_command to #args_without_command_name
|
157
|
+
* Renamed #populate_options_to_when_called to #option_proc
|
158
|
+
* Renamed #sym_from_switch to #switch_to_sym
|
159
|
+
* Removed #get_command, use #command without supplying a block
|
160
|
+
* Removed Command#inspect
|
161
|
+
* Removed command description aliases of short_description, long_description. Use description / summary
|
162
|
+
* Removed VerboseFileUtils
|
163
|
+
* Removed String#camelcase as its not really needed (was only used in commander's executable for creating templates)
|
164
|
+
* Removed commander/import for now
|
165
|
+
* Removed LOG_FORMAT
|
166
|
+
* Removed Kernel ext which only had #color
|
167
|
+
|
168
|
+
=== 2.5.7 / 2009-03-11
|
169
|
+
|
170
|
+
* Fixed paren warnings in import spec
|
171
|
+
* Fixed Kernel#method_missing to preserve its original behavior (Raise errors for missing methods or variables)
|
172
|
+
|
173
|
+
=== 2.5.6 / 2009-03-06
|
174
|
+
|
175
|
+
* Replaced Commander::VERSION::STRING with Commander::VERSION (for release gem)
|
176
|
+
* Refactored progress bar
|
177
|
+
* Fixed String#tokenize
|
178
|
+
|
179
|
+
=== 2.4.5 / 2009-03-03
|
180
|
+
|
181
|
+
* Fixed bug which was not allowing switch arguments which are strings
|
182
|
+
such as --eval 'something here'
|
183
|
+
|
184
|
+
=== 2.4.4 / 2009-01-15
|
185
|
+
|
186
|
+
* Fixed bug which was not allowing multi-word commands to view help
|
187
|
+
|
188
|
+
=== 2.4.3 / 2009-01-15
|
189
|
+
|
190
|
+
* Fixed bug in command_name_from_args, preventing multi-word commands
|
191
|
+
|
192
|
+
=== 2.4.2 / 2009-01-12
|
193
|
+
|
194
|
+
* Github! build me!
|
195
|
+
|
196
|
+
=== 2.4.1 / 2009-01-11
|
197
|
+
|
198
|
+
* Added ask_for_CLASS, which may be any class name such as ask_for_date, ask_for_array, etc
|
199
|
+
* Changed doc to *.rdoc for pretty markup on github
|
200
|
+
|
201
|
+
=== 2.3.0 / 2008-12-16
|
202
|
+
|
203
|
+
* Removed input, output in favour of HighLines $terminal
|
204
|
+
* Autoloading terminal formatter since it may not always be used
|
205
|
+
* Added wrapping of text with 10 padding, defaults to 80
|
206
|
+
* Added friendly interruption message by default, overridable with trap or int_message
|
207
|
+
* Fixed description now showing as summary when no summary is available
|
208
|
+
|
209
|
+
=== 2.2.1 / 2008-12-09
|
210
|
+
|
211
|
+
* Fixed typo in String#tokenize, preventing progress bar from working
|
212
|
+
* Fixed some small formatting issues with help output
|
213
|
+
|
214
|
+
=== 2.2.0 / 2008-12-09
|
215
|
+
|
216
|
+
* Additional documentation
|
217
|
+
* Added support for arbitrary help 'blocks' using program :help, 'Your block', 'Your blocks contents'
|
218
|
+
* Added support for description, summary, short_description, and large_description, which are displaying conditionally within help docs
|
219
|
+
* Allowing uncaught exceptions to reach stderr, for debugging purposes
|
220
|
+
|
221
|
+
=== 2.1.1 / 2008-12-01
|
222
|
+
|
223
|
+
* Changed #password, now requires that the user must enter a non-empty? value
|
224
|
+
|
225
|
+
=== 2.1.0 / 2008-12-01
|
226
|
+
|
227
|
+
* Added progress bar
|
228
|
+
* Added auto-inclusion of FileUtils module
|
229
|
+
* Added stdout logging method
|
230
|
+
* Fixed issue with options placed before command
|
231
|
+
|
232
|
+
=== 2.0.1 / 2008-11-24
|
233
|
+
|
234
|
+
* Fixed new line after command options
|
235
|
+
* Fixed padding for command list
|
236
|
+
|
237
|
+
=== 2.0.0 / 2008-11-24
|
238
|
+
|
239
|
+
* Rewrite of entire gem
|
240
|
+
* Added auto-parsing and struct population of options
|
241
|
+
* Added better documentation
|
242
|
+
|
243
|
+
=== 1.2.2 / 2008-11-06
|
244
|
+
|
245
|
+
* Forgot to add array.rb
|
246
|
+
|
247
|
+
=== 1.2.0 / 2008-11-06
|
248
|
+
|
249
|
+
* Added paging ability (similar to 'less')
|
250
|
+
* Added coloring to default help generator
|
251
|
+
|
252
|
+
=== 1.1.0 / 2008-11-06
|
253
|
+
|
254
|
+
* Added dependency for Highline gem, which replaces Commander's user interaction lib
|
255
|
+
|
256
|
+
=== 1.0.4 / 2008-11-04
|
257
|
+
|
258
|
+
* Added support for --help and --version flags
|
259
|
+
|
260
|
+
=== 1.0.3 / 2008-11-01
|
261
|
+
|
262
|
+
* Typo causing the gem to fail build on github
|
263
|
+
|
264
|
+
=== 1.0.2 / 2008-11-01
|
265
|
+
|
266
|
+
* Added gemspec for github
|
267
|
+
|
268
|
+
=== 1.0.1 / 2008-10-31
|
269
|
+
|
270
|
+
* Added shebang line to commander init
|
271
|
+
* Added require 'rubygems'
|
272
|
+
|
273
|
+
=== 1.0.0 / 2008-10-31
|
274
|
+
|
275
|
+
* Initial release
|
data/Manifest
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
History.rdoc
|
2
|
+
Manifest
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
bin/commander
|
6
|
+
commander.gemspec
|
7
|
+
lib/commander.rb
|
8
|
+
lib/commander/blank.rb
|
9
|
+
lib/commander/command.rb
|
10
|
+
lib/commander/core_ext.rb
|
11
|
+
lib/commander/core_ext/array.rb
|
12
|
+
lib/commander/core_ext/object.rb
|
13
|
+
lib/commander/delegates.rb
|
14
|
+
lib/commander/help_formatters.rb
|
15
|
+
lib/commander/help_formatters/base.rb
|
16
|
+
lib/commander/help_formatters/terminal.rb
|
17
|
+
lib/commander/help_formatters/terminal/command_help.erb
|
18
|
+
lib/commander/help_formatters/terminal/help.erb
|
19
|
+
lib/commander/help_formatters/terminal_compact.rb
|
20
|
+
lib/commander/help_formatters/terminal_compact/command_help.erb
|
21
|
+
lib/commander/help_formatters/terminal_compact/help.erb
|
22
|
+
lib/commander/import.rb
|
23
|
+
lib/commander/runner.rb
|
24
|
+
lib/commander/user_interaction.rb
|
25
|
+
lib/commander/version.rb
|
26
|
+
spec/command_spec.rb
|
27
|
+
spec/core_ext/array_spec.rb
|
28
|
+
spec/core_ext/object_spec.rb
|
29
|
+
spec/help_formatters/base_spec.rb
|
30
|
+
spec/help_formatters/terminal_spec.rb
|
31
|
+
spec/runner_spec.rb
|
32
|
+
spec/spec_helper.rb
|
33
|
+
spec/ui_spec.rb
|
34
|
+
tasks/docs.rake
|
35
|
+
tasks/gemspec.rake
|
36
|
+
tasks/spec.rake
|
data/README.rdoc
ADDED
@@ -0,0 +1,357 @@
|
|
1
|
+
|
2
|
+
= Commander
|
3
|
+
|
4
|
+
The complete solution for Ruby command-line executables.
|
5
|
+
Commander bridges the gap between other terminal related libraries
|
6
|
+
you know and love (OptionParser, HighLine), while providing many new
|
7
|
+
features, and an elegant API.
|
8
|
+
|
9
|
+
== Features
|
10
|
+
|
11
|
+
* Easier than baking cookies
|
12
|
+
* Parses options using OptionParser
|
13
|
+
* Auto-populates struct with options ( no more { |v| options[:recursive] = v } )
|
14
|
+
* Auto-generates help documentation via pluggable help formatters
|
15
|
+
* Optional default command when none is present
|
16
|
+
* Global / Command level options
|
17
|
+
* Packaged with two help formatters (Terminal, TerminalCompact)
|
18
|
+
* Imports the highline gem for interacting with the terminal
|
19
|
+
* Adds additional user interaction functionality
|
20
|
+
* Highly customizable progress bar with intuative, simple usage
|
21
|
+
* Multi-word command name support such as 'drupal module install MOD', rather than 'drupal module_install MOD'
|
22
|
+
* Sexy paging for long bodies of text
|
23
|
+
* Support for MacOS text-to-speech
|
24
|
+
* Command aliasing (very powerful, as both switches and arguments can be used)
|
25
|
+
* Growl notification support for MacOS
|
26
|
+
* Use the 'commander' executable to initialize a commander driven program
|
27
|
+
|
28
|
+
== Installation
|
29
|
+
|
30
|
+
Due to issues with Github and supporting previous releases, until 4.0 you must:
|
31
|
+
sudo gem install visionmedia-commander --version 3.2.9
|
32
|
+
|
33
|
+
== Example
|
34
|
+
|
35
|
+
For more option examples view the Commander::Command#option method. Also
|
36
|
+
an important feature to note is that when_called may be a class to instantiate,
|
37
|
+
as well as an object, specifying a method to call, so view the RDoc for more information.
|
38
|
+
|
39
|
+
require 'rubygems'
|
40
|
+
require 'commander/import'
|
41
|
+
|
42
|
+
# :name is optional, otherwise uses the basename of this executable
|
43
|
+
program :name, 'Foo Bar'
|
44
|
+
program :version, '1.0.0'
|
45
|
+
program :description, 'Stupid command that prints foo or bar.'
|
46
|
+
|
47
|
+
command :foo do |c|
|
48
|
+
c.syntax = 'foobar foo'
|
49
|
+
c.description = 'Displays foo'
|
50
|
+
c.when_called do |args, options|
|
51
|
+
say 'foo'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
command :bar do |c|
|
56
|
+
c.syntax = 'foobar bar [options]'
|
57
|
+
c.description = 'Display bar with optional prefix and suffix'
|
58
|
+
c.option '--prefix STRING', String, 'Adds a prefix to bar'
|
59
|
+
c.option '--suffix STRING', String, 'Adds a suffix to bar'
|
60
|
+
c.when_called do |args, options|
|
61
|
+
options.default :prefix => '(', :suffix => ')'
|
62
|
+
say "#{options.prefix}bar#{options.suffix}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
$ foobar bar
|
67
|
+
# => (bar)
|
68
|
+
|
69
|
+
$ foobar bar --suffix '{' --prefix '}'
|
70
|
+
# => {bar}
|
71
|
+
|
72
|
+
== HighLine
|
73
|
+
|
74
|
+
As mentioned above the highline gem is imported into 'global scope', below
|
75
|
+
are some quick examples for how to utilize highline in your command(s):
|
76
|
+
|
77
|
+
# Ask for password masked with '*' character
|
78
|
+
ask("Password: ") { |q| q.echo = "*" }
|
79
|
+
|
80
|
+
# Ask for password
|
81
|
+
ask("Password: ") { |q| q.echo = false }
|
82
|
+
|
83
|
+
# Ask if the user agrees (yes or no)
|
84
|
+
agree("Do something?")
|
85
|
+
|
86
|
+
# Asks on a single line (note the space after ':')
|
87
|
+
ask("Name: ")
|
88
|
+
|
89
|
+
# Asks with new line after "Description:"
|
90
|
+
ask("Description:")
|
91
|
+
|
92
|
+
# Calls Date#parse to parse the date string passed
|
93
|
+
ask("Birthday? ", Date)
|
94
|
+
|
95
|
+
# Ensures Integer is within the range specified
|
96
|
+
ask("Age? ", Integer) { |q| q.in = 0..105 }
|
97
|
+
|
98
|
+
# Asks for a list of strings, converts to array
|
99
|
+
ask("Fav colors?", Array)
|
100
|
+
|
101
|
+
== HighLine & Interaction Additions
|
102
|
+
|
103
|
+
In addition to highline's fantastic choice of methods we will continue to
|
104
|
+
simplify common tasks using the following methods:
|
105
|
+
|
106
|
+
# Ask for password
|
107
|
+
password
|
108
|
+
|
109
|
+
# Ask for password with specific message and mask character
|
110
|
+
password "Enter your password please:", '-'
|
111
|
+
|
112
|
+
# Ask for CLASS, which may be any valid class responding to #parse. Date, Time, Array, etc
|
113
|
+
names = ask_for_array 'Names: '
|
114
|
+
bday = ask_for_date 'Birthday?: '
|
115
|
+
|
116
|
+
# Simple progress bar (Commander::UI::ProgressBar)
|
117
|
+
uris = %w[
|
118
|
+
http://vision-media.ca
|
119
|
+
http://google.com
|
120
|
+
http://yahoo.com
|
121
|
+
]
|
122
|
+
progress uris do |uri|
|
123
|
+
res = open uri
|
124
|
+
# Do something with response
|
125
|
+
end
|
126
|
+
|
127
|
+
# 'Log' action to stdout
|
128
|
+
log "create", "path/to/file.rb"
|
129
|
+
|
130
|
+
# Enable paging of output after this point
|
131
|
+
enable_paging
|
132
|
+
|
133
|
+
# Ask editor for input (EDITOR or TextMate by default)
|
134
|
+
ask_editor
|
135
|
+
|
136
|
+
# Ask editor, supplying initial text
|
137
|
+
ask_editor 'previous data to update'
|
138
|
+
|
139
|
+
# Display a generic Growl notification
|
140
|
+
notify 'Something happened'
|
141
|
+
|
142
|
+
# Display an 'info' status notification
|
143
|
+
notify_info 'You have #{emails.length} new email(s)'
|
144
|
+
|
145
|
+
# Display an 'ok' status notification
|
146
|
+
notify_ok 'Gems updated'
|
147
|
+
|
148
|
+
# Display a 'warning' status notification
|
149
|
+
notify_warning '1 gem failed installation'
|
150
|
+
|
151
|
+
# Display an 'error' status notification
|
152
|
+
notify_error "Gem #{name} failed"
|
153
|
+
|
154
|
+
# Choose from an array of elements
|
155
|
+
choice = choose("Favorite language?", :ruby, :perl, :js)
|
156
|
+
|
157
|
+
# Alter IO for the duration of the block
|
158
|
+
io new_input, new_output do
|
159
|
+
new_input_contents = $stdin.read
|
160
|
+
puts new_input_contents # outputs to new_output stream
|
161
|
+
end
|
162
|
+
# $stdin / $stdout reset back to original streams
|
163
|
+
|
164
|
+
# Speech synthesis
|
165
|
+
speak 'What is your favorite food? '
|
166
|
+
food = ask 'favorite food?: '
|
167
|
+
speak "wow, I like #{food} too. We have so much alike."
|
168
|
+
|
169
|
+
# Execute arbitrary applescript
|
170
|
+
applescript 'foo'
|
171
|
+
|
172
|
+
# Converse with speech recognition server
|
173
|
+
case converse 'What is the best food?', :cookies => 'Cookies', :unknown => 'Nothing'
|
174
|
+
when :cookies
|
175
|
+
speak 'o.m.g. you are awesome!'
|
176
|
+
else
|
177
|
+
case converse 'That is lame, shall I convince you cookies are the best?', :yes => 'Ok', :no => 'No', :maybe => 'Maybe another time'
|
178
|
+
when :yes
|
179
|
+
speak 'Well you see, cookies are just fantastic, they melt in your mouth.'
|
180
|
+
else
|
181
|
+
speak 'Ok then, bye.'
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
== Commander Goodies
|
186
|
+
|
187
|
+
=== Option Defaults
|
188
|
+
|
189
|
+
The options struct passed to #when_called provides a #default method, allowing you
|
190
|
+
to set defaults in a clean manor to options which have not been set.
|
191
|
+
|
192
|
+
command :foo do |c|
|
193
|
+
c.option '--interval SECONDS', Integer, 'Interval in seconds'
|
194
|
+
c.option '--timeout SECONDS', Integer, 'Timeout in seconds'
|
195
|
+
c.when_called do |args, options|
|
196
|
+
options.default \
|
197
|
+
:interval => 2,
|
198
|
+
:timeout => 60
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
=== Command Aliasing
|
203
|
+
|
204
|
+
Aliases can be created using the #alias_command method like below:
|
205
|
+
|
206
|
+
command :'install gem' do |c|
|
207
|
+
c.when_called { puts 'foo' }
|
208
|
+
end
|
209
|
+
alias_command :'gem install', :'install gem'
|
210
|
+
|
211
|
+
Or more complicated aliases can be made, passing any arguments
|
212
|
+
as if it was invoked via the command line:
|
213
|
+
|
214
|
+
command :'install gem' do |c|
|
215
|
+
c.syntax = 'install gem <name> [options]'
|
216
|
+
c.option '--dest DIR', String, 'Destination directory'
|
217
|
+
c.when_called { |args, options| puts "installing #{args.first} to #{options.dest}" }
|
218
|
+
end
|
219
|
+
alias_command :update, :'install gem', 'rubygems', '--dest', 'some_path'
|
220
|
+
|
221
|
+
$ foo update
|
222
|
+
# => installing rubygems to some_path
|
223
|
+
|
224
|
+
=== Command Defaults
|
225
|
+
|
226
|
+
Although working with a command executable framework provides many
|
227
|
+
benefits over a single command implementation, sometimes you still
|
228
|
+
want the ability to create a terse syntax for your command. With that
|
229
|
+
in mind we may use #default_command to help with this. Considering
|
230
|
+
our previous :'install gem' example:
|
231
|
+
|
232
|
+
default_command :update
|
233
|
+
|
234
|
+
$ foo
|
235
|
+
# => installing rubygems to some_path
|
236
|
+
|
237
|
+
Keeping in mind that commander searches for the longest possible match
|
238
|
+
when considering a command, so if you were to pass arguments to foo
|
239
|
+
like below, expecting them to be passed to :update, this would be incorrect,
|
240
|
+
and would end up calling :'install gem', so be careful that the users do
|
241
|
+
not need to use command names within the arguments.
|
242
|
+
|
243
|
+
$ foo install gem
|
244
|
+
# => installing to
|
245
|
+
|
246
|
+
=== Additional Global Help
|
247
|
+
|
248
|
+
Arbitrary help can be added using the following #program symbol:
|
249
|
+
|
250
|
+
program :help, 'Author', 'TJ Holowaychuk <tj@vision-media.ca>'
|
251
|
+
|
252
|
+
Which will output the rest of the help doc, along with:
|
253
|
+
|
254
|
+
AUTHOR:
|
255
|
+
|
256
|
+
TJ Holowaychuk <tj@vision-media.ca>
|
257
|
+
|
258
|
+
=== Global Options
|
259
|
+
|
260
|
+
Although most switches will be at the command level, several are available
|
261
|
+
by default at the global level, such as --version, and --help. Using #global_option
|
262
|
+
you can add additional global options:
|
263
|
+
|
264
|
+
global_option('-c', '--config FILE', 'Load config data for your commands to use') { |file| ... }
|
265
|
+
|
266
|
+
This method accepts the same syntax as Commander::Command#option so check it out for documentation.
|
267
|
+
|
268
|
+
All global options regardless of providing a block are accessable at the command level. This
|
269
|
+
means that instead of the following:
|
270
|
+
|
271
|
+
global_option('--verbose') { $verbose = true }
|
272
|
+
...
|
273
|
+
c.when_called do |args, options|
|
274
|
+
say 'foo' if $verbose
|
275
|
+
...
|
276
|
+
|
277
|
+
You may:
|
278
|
+
|
279
|
+
global_option '--verbose'
|
280
|
+
...
|
281
|
+
c.when_called do |args, options|
|
282
|
+
say 'foo' if options.verbose
|
283
|
+
...
|
284
|
+
|
285
|
+
=== Formatters
|
286
|
+
|
287
|
+
Two core formatters are currently available, the default Terminal formatter as well
|
288
|
+
as TerminalCompact. To utilize a different formatter simply use :help_formatter like below:
|
289
|
+
|
290
|
+
program :help_formatter, Commander::HelpFormatter::TerminalCompact
|
291
|
+
|
292
|
+
Or utilize the help formatter aliases:
|
293
|
+
|
294
|
+
program :help_formatter, :compact
|
295
|
+
|
296
|
+
This abstraction could be utilized to generate HTML documentation for your executable.
|
297
|
+
|
298
|
+
== Tips
|
299
|
+
|
300
|
+
When adding a global or command option, OptionParser implicitly adds a small
|
301
|
+
switch even when not explicitly created, for example -c will be the same as --config
|
302
|
+
in both examples, however '-c' will only appear in the documentation when explicitly
|
303
|
+
assigning it.
|
304
|
+
|
305
|
+
global_option '-c', '--config FILE'
|
306
|
+
global_option '--config FILE'
|
307
|
+
|
308
|
+
== ASCII Tables
|
309
|
+
|
310
|
+
For feature rich ASCII tables for your terminal app check out visionmedia's terminal-table gem at
|
311
|
+
http://github.com/visionmedia/terminal-table
|
312
|
+
|
313
|
+
+----------+-------+----+--------+-----------------------+
|
314
|
+
| Terminal | Table | Is | Wicked | Awesome |
|
315
|
+
+----------+-------+----+--------+-----------------------+
|
316
|
+
| | | | | get it while its hot! |
|
317
|
+
+----------+-------+----+--------+-----------------------+
|
318
|
+
|
319
|
+
== Growl Notifications
|
320
|
+
|
321
|
+
To utilize the #notify and #notify_STATUS methods you need to install
|
322
|
+
http://github.com/visionmedia/growl which utilizes the 'growlnotify' executable.
|
323
|
+
Note that growl is auto-imported by Commander when available, no need to require.
|
324
|
+
|
325
|
+
== Contrib
|
326
|
+
|
327
|
+
Feel free to fork and request a pull, or submit a ticket
|
328
|
+
http://visionmedia.lighthouseapp.com/projects/27643-commander
|
329
|
+
|
330
|
+
== Known Issues
|
331
|
+
|
332
|
+
* ask_editor has been tested with TextMate only
|
333
|
+
|
334
|
+
== License
|
335
|
+
|
336
|
+
(The MIT License)
|
337
|
+
|
338
|
+
Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
|
339
|
+
|
340
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
341
|
+
a copy of this software and associated documentation files (the
|
342
|
+
'Software'), to deal in the Software without restriction, including
|
343
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
344
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
345
|
+
permit persons to whom the Software is furnished to do so, subject to
|
346
|
+
the following conditions:
|
347
|
+
|
348
|
+
The above copyright notice and this permission notice shall be
|
349
|
+
included in all copies or substantial portions of the Software.
|
350
|
+
|
351
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
352
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
353
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
354
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
355
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
356
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
357
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|