class SuperDocopt::Base

Attributes

args[R]
docopt[RW]
subcommands[RW]
version[RW]

Public Class Methods

docopt(path) click to toggle source
# File lib/super_docopt/base.rb, line 19
def self.docopt(path)
  instance.docopt = path
end
execute(argv=[]) click to toggle source
# File lib/super_docopt/base.rb, line 11
def self.execute(argv=[])
  instance.execute_cli argv
end
subcommands(list) click to toggle source
# File lib/super_docopt/base.rb, line 23
def self.subcommands(list)
  instance.subcommands = list
end
version(version) click to toggle source
# File lib/super_docopt/base.rb, line 15
def self.version(version)
  instance.version = version
end

Public Instance Methods

execute_cli(argv=[]) click to toggle source
# File lib/super_docopt/base.rb, line 27
def execute_cli(argv=[])
  doc = File.read docopt
  begin
    @args = Docopt::docopt(doc, argv: argv, version: version)
    handle_subcommand
  rescue Docopt::Exit => e
    puts e.message
  end
end

Private Instance Methods

after_execute() click to toggle source
# File lib/super_docopt/base.rb, line 73
def after_execute; end
before_execute() click to toggle source

Overridable Hooks

# File lib/super_docopt/base.rb, line 72
def before_execute; end
execute_subcommand(input, method) click to toggle source
# File lib/super_docopt/base.rb, line 46
def execute_subcommand(input, method)
  raise NotImplementedError, 
    "Please implement ##{method}" unless respond_to? method

  before_execute
  send method
  after_execute
end
handle_subcommand() click to toggle source
# File lib/super_docopt/base.rb, line 39
def handle_subcommand
  subcommands.each do |subcommand|
    input, method = translate_subcommand subcommand
    return execute_subcommand input, method if args[input] 
  end
end
translate_subcommand(subcommand) click to toggle source
# File lib/super_docopt/base.rb, line 55
def translate_subcommand(subcommand)
  input  = subcommand.to_s
  method = subcommand.to_s

  if subcommand.is_a? Hash
    input  = subcommand.keys.first.to_s
    method = subcommand.values.first.to_s
  end

  input  = input.gsub '_', '-'
  method = method.gsub  '-', '_'

  [input, method]
end