Module: Adhearsion::CLI::AhnCommand

Defined in:
lib/adhearsion/cli.rb

Defined Under Namespace

Modules: CommandHandler

Constant Summary collapse

USAGE =
"Usage:\n   ahn create /path/to/directory\n   ahn start [daemon] [/path/to/directory]\n   ahn version|-v|--v|-version|--version\n   ahn help|-h|--h|--help|-help\n   \n   ahn  enable component COMPONENT_NAME\n   ahn disable component COMPONENT_NAME\n   ahn  create component COMPONENT_NAME\n"

Class Method Summary collapse

Class Method Details

.execute!Object



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

def self.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().



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

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

.parse_arguments(args = ARGV.clone) ⇒ Object



31
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
# File 'lib/adhearsion/cli.rb', line 31

def self.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