Created
May 3, 2012 05:31
-
-
Save phinze/2583533 to your computer and use it in GitHub Desktop.
CLI with sub-commands using dynamic dispatch and a whitelist
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
# revision of https://gist.github.com/2583429 with a whitelist | |
class PunCommands | |
PUN_WORDS = %w[foo bar baz] | |
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 | |
def self.valid_command?(command) | |
PUN_WORDS.include?(command.to_s) | |
end | |
def self.defined_commands | |
PUN_WORDS | |
end | |
end | |
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.valid_command?(input_command) | |
PunCommands.send(input_command) | |
exit 0 | |
else | |
puts "no command by that name, try: #{PunCommands.defined_commands.join(', ')}" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment