Skip to content

Instantly share code, notes, and snippets.

@sagar-ranglani
Created June 20, 2013 03:17
Show Gist options
  • Save sagar-ranglani/5820062 to your computer and use it in GitHub Desktop.
Save sagar-ranglani/5820062 to your computer and use it in GitHub Desktop.
Example usage of Thor (A command line building gem) In a Thor class, public methods become commands.
# Sample for Thor
# https://github.com/erikhuda/thor/wiki/Getting-Started
# Important command
# - thor list => This lists down all the .thor files with the tasks
# - thor help test:example
# Usage: "thor test:example".
# Usage: "thor test:example_with_arg FILE".
# Usage:
# thor test:example_with_options FILE
# thor test:example_with_options FILE --delete
# thor test:example_with_options FILE -d
# Options:
# -d, [--delete=DELETE] # Delete the file after parsing it
# More details: https://github.com/erikhuda/thor/wiki/Method-Options
require "thor"
class Test < Thor
desc "example_with_arg FILE", "an example command which take arguments"
def example_with_arg(file)
puts "The arg you provided is #{file}"
end
desc "simple_command", "an example command"
def simple_command
puts "This is an example of Thor command"
end
desc "example_with_options FILE", "an example command which take an argument and option"
method_option :delete, :aliases => "-d", :desc => "Delete the file after parsing it"
def example(file)
puts "You supplied the file: #{file}"
delete_file = options[:delete]
if delete_file
puts "You specified that you would like to delete #{file}"
else
puts "You do not want to delete #{file}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment