Module: Adhearsion::CLI::AhnCommand

Defined in:
lib/adhearsion/cli.rb

Defined Under Namespace

Modules: CommandHandler

Constant Summary collapse

USAGE =
<<USAGE
Usage:
   ahn create /path/to/directory
   ahn start [daemon] [/path/to/directory]
   ahn version|-v|--v|-version|--version
   ahn help|-h|--h|--help|-help

   ahn  enable component COMPONENT_NAME
   ahn disable component COMPONENT_NAME
   ahn  create component COMPONENT_NAME
USAGE

Class Method Summary collapse

Class Method Details

.execute!Object



19
20
21
22
23
# File 'lib/adhearsion/cli.rb', line 19

def execute!
  CommandHandler.send(*parse_arguments)
rescue CommandHandler::CLIException => error
  fail_and_print_usage error
end

.fail_and_print_usage(error) ⇒ Object

Provides a small abstraction of Kernel::abort().



28
29
30
# File 'lib/adhearsion/cli.rb', line 28

def fail_and_print_usage(error)
  Kernel.abort "#{error.message}\n\n#{USAGE}"
end

.parse_arguments(args = ARGV.clone) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/adhearsion/cli.rb', line 32

def parse_arguments(args=ARGV.clone)
  action = args.shift
  case action
  when /^-?-?h(elp)?$/, nil   then [:help]
  when /^-?-?v(ersion)?$/     then [:version]
  when "create"
    [:create, *args]
  when 'start'
    pid_file_regexp = /^--pid-file=(.+)$/
    if args.size > 3
      fail_and_print_usage "Too many arguments supplied!" if args.size > 3
    elsif args.size == 3
      fail_and_print_usage "Unrecognized final argument #{args.last}" unless args.last =~ pid_file_regexp
      pid_file = args.pop[pid_file_regexp, 1]
    else
      pid_file = nil
    end

    if args.first == 'daemon' && args.size == 2
      path   = args.last
      daemon = true
    elsif args.size == 1
      path, daemon = args.first, false
    else
      fail_and_print_usage "Invalid format for the start CLI command!"
    end
    [:start, path, daemon, pid_file]
  when '-'
    [:start, Dir.pwd]
  when "enable", "disable"
    if args.size == 1
      raise CommandHandler::UnknownCommand, "Must supply an argument for what you wish to #{action}"
    elsif args.size == 2
      [action, *args]
    else
      raise CommandHandler::UnknownCommand, "Too many arguments supplied!"
    end
  else
    [action, *args]
  end
end