Class: PVN::App::Runner

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/pvn/app/runner.rb

Constant Summary collapse

SUBCOMMANDS =
[ PVN::Log::Command,
  PVN::Pct::Command,
  PVN::Status::Command,
  PVN::Diff::Command,
  # DescribeCommand, 
  # WhereCommand,
  # UndeleteCommand,
]

Instance Method Summary collapse

Constructor Details

#initialize(io, args) ⇒ Runner

Returns a new instance of Runner.



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
# File 'lib/pvn/app/runner.rb', line 39

def initialize io, args
  RIEL::Log.level = RIEL::Log::WARN
  RIEL::Log.set_widths(-25, 5, -35)

  if args.empty?
    run_help args
  end
  
  while args.size > 0
    arg = args.shift
    info "arg: #{arg}"

    case arg
    when "--verbose"
      RIEL::Log.level = RIEL::Log::DEBUG
    when "help", "--help", "-h"
      run_help args
    else
      SUBCOMMANDS.each do |sc|
        if sc.matches_subcommand? arg
          # run command actually exits, but this makes it clearer
          return run_command sc, args
        end
      end
      $stderr.puts "ERROR: subcommand not valid: #{arg}"
      exit(-1)
    end
  end

  run_help args
end

Instance Method Details

#run_command(cmdcls, args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/pvn/app/runner.rb', line 71

def run_command cmdcls, args
  begin
    cmdcls.new args
    exit(0)
  rescue => e
    puts e.backtrace
    $stderr.puts e
    exit(-1)
  end
end

#run_help(args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pvn/app/runner.rb', line 82

def run_help args
  forwhat = args[0]

  cls = SUBCOMMANDS.find do |cls|
    cls.getdoc.subcommands.include? forwhat
  end

  if cls
    cls.to_doc
  else
    puts "usage: pvn [--verbose] <command> [<options>] [<args>]"
    puts "PVN, version #{PVN::VERSION}"
    puts
    puts "PVN has the subcommands:"
    SUBCOMMANDS.each do |sc|
      printf "   %-10s %s\n", sc.getdoc.subcommands[0], sc.getdoc.description
    end
  end
  exit(0)
end