Skip to content

Instantly share code, notes, and snippets.

@phinze
Created May 3, 2012 05:21
Show Gist options
  • Save phinze/2583429 to your computer and use it in GitHub Desktop.
Save phinze/2583429 to your computer and use it in GitHub Desktop.
CLI with sub-commands using dynamic dispatch
class PunCommands
def self.foo
puts "you could have fooed me"
end
def self.bar
puts "guy walks into a bar"
end
def self.baz
puts "bazically it works"
end
end
defined_commands = PunCommands.methods - PunCommands.superclass.methods
unless ARGV.length == 1
puts "requires one argument, the command you'd like to run"
exit 1
end
input_command = ARGV.first.to_sym
if PunCommands.respond_to?(input_command)
PunCommands.send(input_command)
exit 0
else
puts "no command by that name, try: #{defined_commands.join(', ')}"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment