Class: PVN::App::Runner

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(io, args) ⇒ Runner

Returns a new instance of Runner.



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

def initialize io, args
  Logue::Log.level = Logue::Log::WARN
  Logue::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 "-v", "--version"
      show_version
    when "--verbose"
      Logue::Log.level = Logue::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



74
75
76
77
78
79
80
81
82
83
# File 'lib/pvn/app/runner.rb', line 74

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



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

def run_help args
  forwhat = args[0]

  SUBCOMMANDS.each do |sc|
    info "sc: #{sc}"
    if sc.matches_subcommand? forwhat
      sc.new(%w{ --help })
      exit 0
    end
  end

  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
  exit 0
end

#show_versionObject



67
68
69
70
71
72
# File 'lib/pvn/app/runner.rb', line 67

def show_version
  puts "pvn, version #{PVN::VERSION}"
  puts "Written by Jeff Pace ([email protected])."
  puts "Released under the I Haven't Decided Yet License."
  exit 0
end