Created
May 3, 2012 05:21
-
-
Save phinze/2583429 to your computer and use it in GitHub Desktop.
CLI with sub-commands using dynamic dispatch
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
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