gli 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +5 -258
- data/lib/gli/command_line_token.rb +1 -1
- data/lib/gli_version.rb +1 -1
- data/lib/support/help.rb +1 -1
- metadata +3 -6
data/CHANGELOG.rdoc
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
== Changelog
|
2
|
+
=== 1.2.2 - 12/11/2010
|
3
|
+
|
4
|
+
* Changed uses of <code>map(&:symbol)</code> to older form, since this doesn't work on 100% of Ruby 1.8.7s.
|
5
|
+
|
2
6
|
=== 1.2.1 - 11/26/2010
|
3
7
|
|
4
8
|
* Changed default data structure of options *back* to a <tt>Hash</tt>. If you want to use the <tt>OpenStruct</tt> subclass <tt>Options</tt>, simply put <tt>use_openstruct true</tt> in your command line definition.
|
data/README.rdoc
CHANGED
@@ -35,269 +35,16 @@ Known to work on
|
|
35
35
|
* 1.8.7
|
36
36
|
* 1.9.2
|
37
37
|
|
38
|
-
Though likely works on various other versions
|
38
|
+
Though likely works on various other versions.
|
39
39
|
|
40
|
-
|
40
|
+
== Documentation
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
The following sets you up to use the DSL that GLI defines:
|
45
|
-
|
46
|
-
#!/usr/bin/ruby
|
47
|
-
$: << File.expand_path(File.dirname(File.realpath(__FILE__)) + '/../lib')
|
48
|
-
|
49
|
-
require 'gli'
|
50
|
-
require 'gli_version'
|
51
|
-
|
52
|
-
include GLI
|
53
|
-
|
54
|
-
The following sets the version of your application for exposure in the default <tt>help</tt> subcommand (the VERSION constant was created by the scaffolding):
|
55
|
-
|
56
|
-
version GLI::VERSION
|
57
|
-
|
58
|
-
The following sets a description of your program. This can be as long as you want.
|
59
|
-
|
60
|
-
program_description 'Support program for bootstrapping GLI-based programs'
|
61
|
-
|
62
|
-
The following sets a config file for your program. The config file can be used to store default values for command
|
63
|
-
line options and command-specific options on a per-user (or per-site) basis. The format is YAML-based.
|
64
|
-
Using an absolute path will result in the configuraiton file being located there. Without an absolute path,
|
65
|
-
the file will be located relative to the current user's home directory (which is what is being done here).
|
66
|
-
|
67
|
-
config_file '.glirc'
|
68
|
-
|
69
|
-
The following describes a command line switch "-n" that is global to all commands and specified before
|
70
|
-
the command name on the command line.
|
71
|
-
|
72
|
-
desc 'Dry run; don\'t change the disk'
|
73
|
-
switch :n
|
74
|
-
|
75
|
-
The following describes a command line flag that is global and has a default value of '<tt>.</tt>' (in GLI parlance,
|
76
|
-
a "flag" is a command line switch that takes an option). It also
|
77
|
-
specifies a short and long description of its argument. This is used to print command line help and to generate
|
78
|
-
rdoc documentation. Note that we
|
79
|
-
have specified two different aliases for this flag. <tt>-r</tt> (because it is listed first) is the default
|
80
|
-
one and <tt>--root</tt> (note two-dash syntax) is also supported. This means that <tt>-r some_dir</tt> and <tt>--root=some_dir</tt> mean
|
81
|
-
the same thing to the application, but that your code should look for <tt>:r</tt>.
|
82
|
-
|
83
|
-
desc 'Root dir in which to create project'
|
84
|
-
long_desc 'This is the location where your project ill be created. A subdirectory named for your project will be created here, and THAT directory will contain the generated files'
|
85
|
-
default_value '.'
|
86
|
-
arg_name 'root_dir'
|
87
|
-
flag [:r,:root]
|
88
|
-
|
89
|
-
Next, we specify a command. Inside the block we can use the same sorts of things as we did above to define flags
|
90
|
-
and switches specific to the command. These must come after the command name. Also note that we use <tt>arg_name</tt>
|
91
|
-
here to describe the arguments this command accepts.
|
92
|
-
|
93
|
-
desc 'Create a new GLI-based project'
|
94
|
-
arg_name 'project_name [command[ command]*]'
|
95
|
-
command [:init,:scaffold] do |c|
|
96
|
-
|
97
|
-
c.desc 'Create an ext dir'
|
98
|
-
c.switch [:e,:ext]
|
99
|
-
|
100
|
-
c.desc 'Overwrite/ignore existing files and directories'
|
101
|
-
c.switch [:force]
|
102
|
-
|
103
|
-
Next, while we are still inside the <tt>command</tt> block,
|
104
|
-
we specify the actual code to execute when the command is chosen by the user. We define a block that
|
105
|
-
will be given the global options (as a Hash), the command-specific options (as a Hash) and the command
|
106
|
-
line arguments. The hashes keys are symbols based upon the switches and flags. For a switch or
|
107
|
-
flag named "-r", we would use <tt>:r</tt>. Note that if you gave multiple values for one flag, e.g. <tt>flag [:f,:force]</tt>
|
108
|
-
then both <tt>:f</tt> and <tt>:force</tt> would be set to the same value; this lets you use more readable
|
109
|
-
keynames in your code when parsing options, but still provide terse flags to the user.
|
110
|
-
|
111
|
-
c.action do |global_options,options,args|
|
112
|
-
if args.length < 1
|
113
|
-
raise 'You must specify the name of your project'
|
114
|
-
end
|
115
|
-
Scaffold.create_scaffold(global_options[:r],
|
116
|
-
!options[:notest],
|
117
|
-
options[:e],
|
118
|
-
args[0],
|
119
|
-
args[1..-1],
|
120
|
-
options[:force],
|
121
|
-
global_options[:n])
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
You can also specify some global code to run before, after and on errors:
|
126
|
-
|
127
|
-
pre do |global_options,command,options,args|
|
128
|
-
puts "After parsing, but before #{command.name} is run"
|
129
|
-
return true
|
130
|
-
# return false if we want to skip command execution for some reason,
|
131
|
-
# such as some global precondition not having been met
|
132
|
-
end
|
133
|
-
|
134
|
-
post do |global_options,command,options,args|
|
135
|
-
puts "After successful execution of #{command.name}"
|
136
|
-
end
|
137
|
-
|
138
|
-
on_error do |ex|
|
139
|
-
puts "We got an error"
|
140
|
-
return true # does the standard error handling code
|
141
|
-
# return false # this would skip standard error handling code
|
142
|
-
end
|
143
|
-
|
144
|
-
Now, we run the program using the arguments the user provided on the command line
|
145
|
-
|
146
|
-
run(ARGV)
|
147
|
-
|
148
|
-
Note that by using <tt>gli init</tt> you can create a shell with all of this already there for you.
|
149
|
-
|
150
|
-
What this gives you:
|
151
|
-
|
152
|
-
* A reasonably useful help system. <tt>your_program help</tt> will list all the global options and commands (along with command aliases) and <tt>your_program help command_name</tt> will list help for that given command.
|
153
|
-
* Error handling when flags do not receive arguments or unknown flags or switches are given
|
154
|
-
* Error handling when an unknown command is specified
|
155
|
-
* Default values for flags if they are not specified by the user (switches all default to false)
|
156
|
-
* An easy way to allow user or site-specific defaults for options via a config file for your app
|
157
|
-
* Nice RDoc describing how to use your application (you can see an example in the {rdoc version of this file}[http://davetron5000.github.com/gli] for the <tt>gli</tt> command)
|
158
|
-
|
159
|
-
What this doesn't give you:
|
160
|
-
|
161
|
-
* A way to indicate required flags
|
162
|
-
* A way to indicate a required argument or required number of arguments
|
163
|
-
* A way to do default switches to 'true' and therefore accept things like <tt>--no-force</tt>
|
164
|
-
* A way to have repeated flags turn into an array or other type-transforming things
|
165
|
-
|
166
|
-
== Configuration File
|
167
|
-
|
168
|
-
The configuration file format is a very simple means of customizing the execution of your command on a per-user
|
169
|
-
or per-site basis. The idea is that commonly used values that aren't the commands' default can be stored in the configuration
|
170
|
-
file so that users do not need to specify them on the command line. The search order for the value of a particular
|
171
|
-
flag then becomes:
|
172
|
-
|
173
|
-
1. Command line invocation
|
174
|
-
2. Configuration File value
|
175
|
-
3. Default value in the application
|
176
|
-
|
177
|
-
Note that since there is no way to switch _off_ switches, setting them to default to true in the configuration file
|
178
|
-
cannot be "undone" on the command line. A future version may allow this.
|
179
|
-
|
180
|
-
The configuration file format is YAML based and can be bootstrapped via the +initconfig+ command to your application.
|
181
|
-
This command is automatically created and added to your application's commands when you declare that there is a
|
182
|
-
config file. When invoked, all global options set on the command line are configured
|
183
|
-
inside the configuration file. Further, a blank area for each
|
184
|
-
command of your application is created, to allow the user edit the config file ith command-specific default values.
|
185
|
-
---
|
186
|
-
# Global options are here
|
187
|
-
:f: foo
|
188
|
-
:g: blah
|
189
|
-
# Command-specific options are under 'commands'
|
190
|
-
commands:
|
191
|
-
# defaults for the "doit" command
|
192
|
-
:doit:
|
193
|
-
:g: bar
|
194
|
-
:s: true
|
195
|
-
# defaults for the "gonow" command
|
196
|
-
:gonow:
|
197
|
-
:g: foobar
|
198
|
-
:f: barfoo
|
199
|
-
|
200
|
-
This allows you to design your application to have it's behavior _entirely_ affected by command line options, with sensible
|
201
|
-
defaults stored in a configuration file.
|
202
|
-
|
203
|
-
== Generating RDoc
|
204
|
-
|
205
|
-
All gli-based applications include a "hidden" command named <tt>rdoc</tt>. When you execute this command, a file called <tt>yourapp.rdoc</tt>
|
206
|
-
is created in the current directory. This contains a rdoc-formatted helpfile for your command line application. This can be useful
|
207
|
-
in packaging your application to share with others. This is also the only place in which the <tt>long_desc</tt> values are currently
|
208
|
-
used.
|
209
|
-
|
210
|
-
If your application has a <tt>README.rdoc</tt> already, you can simply add <tt>:include:yourapp.rdoc</tt> to the bottom and it will
|
211
|
-
be included when you generate and publish your rdoc (note that it will *not* show up on github).
|
212
|
-
|
213
|
-
== Bash Completion
|
214
|
-
|
215
|
-
The +help+ command takes an optional switch, +-c+, that will list all the commands (including aliases) in sorted order suitable for use in a bash completion script. When +-c+ is specified, the argument can be a partial command, and +help+ will only list the commands matching. Put this in your +.bashrc+:
|
216
|
-
|
217
|
-
complete -F get_myapp_targets myapp
|
218
|
-
function get_myapp_targets()
|
219
|
-
{
|
220
|
-
if [ -z $2 ] ; then
|
221
|
-
COMPREPLY=(`myapp help -c`)
|
222
|
-
else
|
223
|
-
COMPREPLY=(`myapp help -c $2`)
|
224
|
-
fi
|
225
|
-
}
|
226
|
-
|
227
|
-
Now, suppose your app takes the commands +list+, +ls+, +rm+, +add+, and +init+:
|
228
|
-
|
229
|
-
> myapp <TAB>
|
230
|
-
add init list ls rm
|
231
|
-
> myapp l<TAB>
|
232
|
-
list ls
|
233
|
-
|
234
|
-
== Reference
|
235
|
-
|
236
|
-
|
237
|
-
[+action+] Specify the action to take when a command is executed from the command line. This is only usable in a command block on the command object (e.g. <tt>c.action</tt>). This takes a block that yields three parameters: a hash of global options specified on the commandline, a hash of command-specific options specified on the command line, and an array of arguments parsed after the options were set on the command line. So, a command like <tt>git --git-dir=/tmp commit -a -m 'Foo bar' foo.c bar.c</tt> would result in the global hash containing <tt>:'git-dir' => '/tmp'</tt>, the options hash containing <tt>:a => true, :m => 'Foo bar'</tt> and the arguments array being <tt>['foo.c', 'bar.c']</tt>
|
238
|
-
[+arg_name+] Describe the name of the argument to the next flag or command. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.arg_name</tt>)
|
239
|
-
[+config_file+] Name the configuration file for your applicaiton. This can either be an absolute path to where the applicaiton will find the configuration file, or a relative path, that will be interpretted as relative to the user's home directory. Default is +nil+, which means no configuration file will be used. Declaring this creates a special +initconfig+ command that can bootstrap this configuration file for your users.
|
240
|
-
[+command+] Declare a command. This takes a symbol, String or array of symbols/Strings and a block. The block yields one argument, the command itself.
|
241
|
-
[+default_value+] Indicate the default value of the next flag. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.default_value</tt>)
|
242
|
-
[+desc+] Describe the next flag, switch, or command you will declare. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.desc</tt>)
|
243
|
-
[+flag+] Declare a flag, which is a command line switch that takes an argument. This takes either a symbol, String, or an array of symbols/Strings. The first symbol decared is used in your program to determine the flag's value at runtime. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.flag</tt>)
|
244
|
-
[+long_desc+] Provide a more lengthy description of the next flag, switch, or command you will declare. This will appear in command line output for commands when you get help for a command. For flags and switches, this will only appear in the generated rdoc and *not* on the command line. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.long_desc</tt>)
|
245
|
-
[+on_error+] Declare an error handling routine that will be called if any command (or other GLI processing) encouters an exception. This is a block that will receive the exception that was caught. All exceptions are routed through this block. If the block evaluates to true, the built-in error handling will be called after, otherwise, nothing will happen.
|
246
|
-
[+post+] Declare code to run after every command that didn't experience an error. This is not available inside a command block. This takes a block that will receive four arguments: the global argument hash (as in <tt>action</tt>), the command (instance of Command), the command-specific options (as in <tt>action</tt>, and the parsed command line arguments (as in <tt>action</tt>).
|
247
|
-
[+pre+] Declare code to run before every command. This is not available inside a command block. This takes a block that will receive four arguments: the global argument hash (as in <tt>action</tt>), the command (instance of Command), the command-specific options (as in <tt>action</tt>, and the parsed command line arguments (as in <tt>action</tt>). If this block evaluates to false, the command will not be executed and the program will stop.
|
248
|
-
[+switch+] Declare a switch, which is a command-line switch taking no argument that indicates a boolean "true" when specified on the command line. This takes either a symbol, String or array of symbols/Strings. The first symbol declared is used in your program to determine if the switch was set. This can be used at the global level or inside a command block on the command object (e.g. <tt>c.switch</tt>)
|
249
|
-
[+version+] Indicate the verison of your application/library. This is used by the default <tt>help</tt> command to allow users to see the version of your application.
|
250
|
-
|
251
|
-
== Interface Generated
|
252
|
-
|
253
|
-
The command line interface that is created with the GLI DSL is:
|
254
|
-
|
255
|
-
*executable* <i>global options and flags</i> *command* <i>command specific options and flags</i> `arguments`
|
256
|
-
|
257
|
-
[switch] a command line control string that takes no argument. The <tt>-l</tt> in <tt>ls -l</tt>
|
258
|
-
[flag] a switch that takes an argument. The <tt>-d' '</tt> in <tt>cut -d' ' file</tt>
|
259
|
-
[command] the command to execute. The <tt>rebase</tt> in <tt>git rebase</tt>
|
260
|
-
[arguments] Anything that's not a switch, flag, or command. The <tt>main.c</tt> in <tt>git add main.c</tt>
|
261
|
-
|
262
|
-
=== Switches
|
263
|
-
|
264
|
-
Switches can be specified one at a time in either a long or short format:
|
265
|
-
|
266
|
-
git add -i
|
267
|
-
git add --interactive
|
268
|
-
|
269
|
-
Switches can also be combined in their short form:
|
270
|
-
|
271
|
-
ls -l -a
|
272
|
-
ls -la
|
273
|
-
|
274
|
-
=== Flags
|
275
|
-
|
276
|
-
Flags can be specified in long or short form, and with or without an equals:
|
277
|
-
|
278
|
-
git merge -s resolve
|
279
|
-
git merge --strategy=resolve
|
280
|
-
|
281
|
-
=== Stop Switch
|
282
|
-
|
283
|
-
A <tt>--</tt> at any time stops processing and sends the rest of the argument to the command as arguments, even if
|
284
|
-
they start with a "--"
|
285
|
-
|
286
|
-
:include:gli.rdoc
|
287
|
-
|
288
|
-
:include:CHANGELOG.rdoc
|
289
|
-
|
290
|
-
== Developing
|
291
|
-
|
292
|
-
gem install bundler
|
293
|
-
bundle install
|
294
|
-
rake test
|
295
|
-
rake rcov
|
296
|
-
|
297
|
-
GLI currently has 100% test coverage, and I'd like to keep it that way. If you submit patches, please have a test, that makes it easier for me to know if anything's broken.
|
42
|
+
Extensive documentation is {available at the wiki}[https://github.com/davetron5000/gli/wiki]
|
298
43
|
|
299
44
|
== Links
|
300
45
|
|
301
46
|
* [http://davetron5000.github.com/gli] - RubyDoc
|
302
47
|
* [http://www.github.com/davetron5000/gli] - Source on GitHub
|
48
|
+
* [http://www.github.com/davetron5000/gli/wiki] - Documentation Wiki
|
303
49
|
|
50
|
+
:include:gli.rdoc
|
@@ -35,7 +35,7 @@ module GLI
|
|
35
35
|
# it into the primary name and aliases list
|
36
36
|
def parse_names(names)
|
37
37
|
# Allow strings; convert to symbols
|
38
|
-
names = [names].flatten.map
|
38
|
+
names = [names].flatten.map { |name| name.to_sym }
|
39
39
|
names_hash = Hash.new
|
40
40
|
names.each do |n|
|
41
41
|
raise ArgumentError.new("#{n} has spaces; they are not allowed") if n.to_s =~ /\s/
|
data/lib/gli_version.rb
CHANGED
data/lib/support/help.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 29
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 1
|
8
7
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
8
|
+
- 2
|
9
|
+
version: 1.2.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- David Copeland
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-11
|
17
|
+
date: 2010-12-11 00:00:00 -05:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -64,7 +63,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
63
|
requirements:
|
65
64
|
- - ">="
|
66
65
|
- !ruby/object:Gem::Version
|
67
|
-
hash: 3
|
68
66
|
segments:
|
69
67
|
- 0
|
70
68
|
version: "0"
|
@@ -73,7 +71,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
71
|
requirements:
|
74
72
|
- - ">="
|
75
73
|
- !ruby/object:Gem::Version
|
76
|
-
hash: 3
|
77
74
|
segments:
|
78
75
|
- 0
|
79
76
|
version: "0"
|