http://s3.amazonaws.com/designandcode/s3cli.rb
http://s3.amazonaws.com/designandcode/s3cli-readme.html
I used the "send" method in ruby to eliminate a lot of code that would have been needed to parse the command and call the required method/function. The concerned code looks like
s = S3.newHere the "send" method allows us to call a method of the S3 class by name. So all we need to do is parse the command name from the arguments and use "send" to call the correct method. The class defines the method "method_missing" which catches any calls for commands that do not exist yet. This eliminates the need for something like a "case" statement or a data structure mapping commands to function pointers. Further, adding a new command involves just adding another method to the class. We don't need to modify any existing code or data structure.
command = ARGV.shift
args = ARGV
if (command)
s.send(command,args)
else
s.usage nil
end
I already have a TODO list in the readme file. Please give me feedback on any new features you would like see, any bugs in my code or advice/critique on the ruby side of the code. Of course I will be happy to integrate any code snippets you provide into the script.
2 comments:
Thanks for the nice post!
Post a Comment