Skip to content

Instantly share code, notes, and snippets.

@mdub
Created Jun 14, 2011
Embed
What would you like to do?
Clamp global option example
rrequire "clamp"
class Logger
DEFAULT_LEVEL = 0
attr_accessor :log_level
def initialize
@log_level = DEFAULT_LEVEL
end
def write(msg)
puts "(Log level #{log_level}): #{msg}"
end
end
module GitDown
class AbstractCommand < Clamp::Command
option ["-v", "--verbose"], :flag, "be verbose"
option "--version", :flag, "show version" do
puts "GitDown-0.0.0a"
exit(0)
end
protected
def logger
@logger ||= Logger.new.tap do |logger|
logger.log_level = 1 if verbose?
end
end
end
class CloneCommand < AbstractCommand
parameter "REPOSITORY", "repository to clone"
parameter "[DIR]", "working directory", :default => "."
def execute
logger.write "Executing 'clone'"
end
end
class PullCommand < AbstractCommand
option "--[no-]commit", :flag, "Perform the merge and commit the result."
def execute
logger.write "Executing 'pull'"
end
end
class StatusCommand < AbstractCommand
option ["-s", "--short"], :flag, "Give the output in the short-format."
def execute
Logger.write "Executing 'status'"
end
end
class MainCommand < AbstractCommand
subcommand "clone", "Clone a remote repository.", CloneCommand
subcommand "pull", "Fetch and merge updates.", PullCommand
subcommand "status", "Display status of local repository.", StatusCommand
end
end
GitDown::MainCommand.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment