-
-
Save cldwalker/265007 to your computer and use it in GitHub Desktop.
Example of using Boson to call subcommands in one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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