Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
.gitignore | Thu Aug 27 18:01:57 -0700 2009 | |
| |
README.rdoc | Mon Oct 19 17:17:57 -0700 2009 | |
| |
Rakefile | Sat Jul 18 15:11:25 -0700 2009 | |
| |
bin/ | Sun Jul 12 14:12:14 -0700 2009 | |
| |
gli.gemspec | Sun Oct 18 11:26:02 -0700 2009 | |
| |
gli.rdoc | Sun Jul 12 14:59:51 -0700 2009 | |
| |
lib/ | Thu Aug 27 18:01:14 -0700 2009 | |
| |
test/ | Thu Aug 27 17:59:39 -0700 2009 |
Git-Like Interface Command Line Parser
| Author: | Dave Copeland (davetron5000 at g mail dot com) |
| Copyright: | Copyright © 2009 by Dave Copeland |
| License: | Distributes under the Apache License, see LICENSE.txt in the source distro |
This is a DSL you can use to create a command line interface like git, gem or svn, in that the first argument is a command, and there are global and command specific flags.
Use
Note that the latest version of this gem is hosted at [www.gemcutter.org], so if you haven’t set it up yet, do so:
# Only needed if you haven't used/setup Gemcutter before
sudo gem install gemcutter
sudo gem tumble
Install if you need to:
sudo gem install gli
The simplest way to get started is to create a scaffold project
gli init my_proj command_name other_command_name
This will create a basic scaffold project in ./my_proj with:
- executable in ./my_proj/bin/my_proj. This file demonstrates most of what you need to describe your command line interface.
- an empty test in ./my_proj/test/tc_nothing.rb that can bootstrap your tests
- a gemspec shell
- a README shell
- Rakefile that can generate RDoc, package your Gem and run tests
Example
This example demonstrates most of the features of GLI.
This sets you up to use the DSL that GLI defines:
#!/usr/bin/ruby
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'gli'
include GLI
This sets a description of your program. This can be as long as you want.
program_description 'Support program for bootstrapping GLI-based programs'
This describes a command line switch "-n" that is global to all commands and specified before the command name on the command line.
desc 'Dry run; don\'t change the disk'
switch :n
The following describes a command line flag that is global and has a default value of ‘.’. It also specifies a short description of its argument. This is used to print command line help. Note that we have specified two different aliases for this flag. -r (because it is listed first) is the default one and —root (note two-dash syntax) is also supported. This means that -r some_dir and —root=some_dir mean the same thing to the application.
desc 'Root dir in which to create project'
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'
default_value '.'
arg_name 'root_dir'
flag [:r,:root]
Here we specify a command. Inside the block we can use the same sorts of things as we did above to define flags and switches specific to the command. These must come after the command name. Also note that we use arg_name here to describe the arguments this command accepts.
desc 'Create a new GLI-based project'
arg_name 'project_name [command[ command]*]'
command [:init,:scaffold] do |c|
c.desc 'Create an ext dir'
c.switch [:e,:ext]
c.desc 'Overwrite/ignore existing files and directories'
c.switch [:force]
Here we specify the actual actions to take when the command is executed. We define a block that will be given the global options (as a Hash), the command-specific options (as a hash) and the command line arguments
c.action do |global_options,options,args|
if args.length < 1
raise 'You must specify the name of your project'
end
Scaffold.create_scaffold(g[:r],!o[:notest],o[:e],args[0],args[1..-1],o[:force],g[:n])
end
end
You can also specify some global code to run before, after and on errors:
pre do |global_options,command,options,args|
puts "After parsing, but before #{command.name} is run"
return true
# return false if we want to skip command execution for some reason
end
post do |global_options,command,options,args|
puts "After successful execution of #{command.name}"
end
on_error do |ex|
puts "We go an error"
return true # does the standard error handling code
# return false # this would skip standard error handling code
end
Now, we run the program using the arguments the user provided on the command line
run(ARGV)
Note that by using gli init you can create a shell with all of this already there for you.
What this gives you:
- A reasonably useful help system. your_program help will list all the global options and commands (along with command aliases) and your_program help command_name will list help for that given command.
- Error handling when flags do not receive arguments or unknown flags or switches are given
- Error handling when an unknown command is specified
- Default values for flags if they are not specified by the user (switches all default to false)
What this doesn’t give you:
- A way to indicate required flags
- A way to indicate a required argument or required number of arguments
- A way to do default switches to ‘true’ and therefore accept things like —no-force
Reference
Interface Generated
The command line interface that is created with the GLI DSL is:
executable global options and flags command command specific options and flags `arguments`
Switches
Switches can be specified one at a time in either a long or short format:
git add -i
git add --interactive
Switches can also be combined in their short form:
ls -l -a
ls -la
Flags
Flags can be specified in long or short form, and with or without an equals:
git merge -s resolve
git merge --strategy=resolve
Stop Switch
A — at any time stops processing and sends the rest of the argument to the command as arguments, even if they start with a "—"
:include:gli.rdoc
Links
- [davetron5000.github.com/gli] - RubyDoc
- [www.github.com/davetron5000/gli] - Source on GitHub







