Skip to content

Instantly share code, notes, and snippets.

@cldwalker
Forked from jeroenvandijk/run_example.rb
Created December 28, 2009 22:37
Show Gist options
  • Save cldwalker/265007 to your computer and use it in GitHub Desktop.
Save cldwalker/265007 to your computer and use it in GitHub Desktop.
Example of using Boson to call subcommands in one file
# In example.rb
module Example
extend self
# This method does something
def command1(arg1, arg2, options = {})
end
# This method does something else
def command2(arg1, arg2, options = {})
end
end
# In run_example.rb
require 'rubygems'
require 'boson'
Boson::Manager.load Boson::Commands::Core
Boson::Manager.load Example,
:commands=>{
'command1'=>{:options=>{:option1=>:string, :option2=>:string }, :args=>3},
'command2'=>{:options=>{:option1=>:string, :option2=>:string }, :args=>3}
}
if (cmd = ARGV.shift) && Boson.can_invoke?(cmd)
begin
Boson.full_invoke cmd, ARGV
rescue ArgumentError
puts "#{$!} for command '#{cmd}'"
end
else
puts "Command '#{cmd}' is invalid."
end
# The following would be possible from the command line
# ./run_example.rb command1 arg1 arg2 --option1=foo --option2=bar
# ./run_example.rb command1 -h
# ./run_example.rb command2 arg1 arg2 --option1=foo --option2=bar
# Handles edge cases
# no commands: ./run_example.rb
# invalid command: ./run_example.rb blah
# command with wrong number of arguments: ./run_example.rb command1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment