class Object

Public Instance Methods

confirm() click to toggle source

Confirmation on STDIN

# File lib/cli_framework.rb, line 41
def confirm
  printf "Are you sure? [Y/n] "
  answer = STDIN.gets.strip
  if ("Y" != answer)
    die "Command ignored."
  end
end
die(text="") click to toggle source

Displays the given text, if any, then quits.

# File lib/cli_framework.rb, line 51
def die text=""
  puts text
  exit
end
main(executorCls) click to toggle source

This is the main function of the CLI. Its parameter is an arbitrary class, whose public instance methods will be the CLI commands.

# File lib/cli_framework.rb, line 17
def main executorCls
  executor = executorCls.new
  cmds = (methods_of(executorCls) - methods_of(Object)).sort
  cmds.map! {|x| x.to_s}

  issued = ARGV.shift
  if not cmds.include? issued
    puts "No command '#{issued}' is known."
    die "Please select from: #{cmds.join ', '}"
  end
  
  task = executorCls.public_instance_method(issued) .bind(executor)
  begin
    task.call *ARGV
  rescue ArgumentError => e
    puts "Try a different parameter set.." # show cmd help
    puts e
  rescue Interrupt
    puts
  end
end
methods_of(cls) click to toggle source

Gives the set of all public instance methods of the given class.

# File lib/cli_framework.rb, line 8
def methods_of cls
  cls.public_instance_methods.to_set
end