Class: Cir::Cli::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/cir/cli/main.rb

Constant Summary collapse

COMMANDS =

Global commands

{
  'init'       => InitCommand,
  'status'     => StatusCommand,
  'register'   => RegisterCommand,
  'deregister' => DeregisterCommand,
  'update'     => UpdateCommand,
  'restore'    => RestoreCommand,
}

Instance Method Summary collapse

Instance Method Details

#global_optsObject

Global arguments



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cir/cli/main.rb', line 32

def global_opts
  Trollop::Parser.new do
    version "CIR - Configs in repository #{Cir::VERSION}"
    banner <<-EOS
CIR - Configs in repository

Keep your configuration files safely versioned in external repository.

Usage:
  cir command [command args]

Command is one of #{COMMANDS.keys}.

EOS
    stop_on COMMANDS.keys
  end
end

#run(argv) ⇒ Object

Process given arguments and execute them



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cir/cli/main.rb', line 52

def run(argv)
  begin
    # Parse global arguments
    global_opts.parse argv

    # Specific command (must exists)
    cmd_name = argv.shift
    raise Trollop::HelpNeeded, "" unless cmd_name

    # Given command that is current executed
    @cmd = COMMANDS[cmd_name].new

    # Finish parsing arguments
    @cmd.global_args = @global_opts
    @cmd.args = @cmd.opts.parse(argv)
    @cmd.files = argv

    # And finally run the command
    @cmd.process

  rescue Trollop::CommandlineError => e
    $stderr.puts "Error: #{e.message}."
    $stderr.puts "Try --help for help."
    exit(-1)
  rescue Trollop::HelpNeeded
    # Global arguments
    global_opts.educate

    # Help for each command
    COMMANDS.each do |name, cmd|
      puts "\nCommand :#{name}\n"
      cmd.opts.educate
    end

    exit
  rescue Trollop::VersionNeeded
    puts global_opts.version
    exit
   end
end