Class: Exctl::CLI

Inherits:
Object show all
Defined in:
lib/exctl/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
10
# File 'lib/exctl/cli.rb', line 3

def initialize(argv)
  @args = argv.clone
  @cmd = (@args.shift || 'help').strip.downcase
  @cmd = 'help'    if [nil,'-h','--help','h'].include?(@cmd)
  @cmd = 'version' if [nil,'-v','--version','v'].include?(@cmd)
  @cmd = ('cmd_' + @cmd.gsub(/^-+/,'').gsub('-','_')).to_sym
  @opts = {}
end

Instance Method Details

#avail_cmdsObject



21
# File 'lib/exctl/cli.rb', line 21

def avail_cmds; @avail_cmds ||= (self.methods-Object.methods).map(&:to_s).select{|m|m[0..3]=='cmd_'}.map{|m|m[4..-1]} end

#cmd_helpObject



25
26
27
28
29
30
# File 'lib/exctl/cli.rb', line 25

def cmd_help
  puts "exctl - Execution control; project command-line generator."
  puts "        v#{Exctl.version}\n\n"
  puts "USAGE: exctl (#{avail_cmds.join('|')}) [OPTIONS]\n\n"
  puts avail_cmds.map{|c| hc='doc_'+c; respond_to?(hc) ? '   ' + c.ljust(10) + ':  ' + send(hc) : nil}.compact.join("\n") + "\n\n"
end

#cmd_initObject



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

def cmd_init
  require 'erb'
  binname = @args.shift
  dir     = @args.shift || '.'
  if binname.nil?
    $stderr.puts "ERROR: project/binary name not specified."
    $stderr.puts "USAGE: exctl init BIN-NAME [DIR=.]"
    exit 3
  end

  b = binding

  libdest = Path["./.exctl"]
  $stderr.puts "  create dir:  #{libdest}/"
  libdest.dir!

  LIB_DIR['**/**',false].each do |f|
    relative = f.short(LIB_DIR)
    unless relative.to_s[0..0]=='.' || relative.to_s == 'exctl/cli.rb'
      dest = libdest ** relative
      if f.dir?
        $stderr.puts "  create dir:  #{dest}/"
        dest.dir!
      else
        $stderr.puts "  create file: #{dest}"
        system "cp", f, dest
      end
    end
  end

  $stderr.puts "  create file: #{binname}"
  dispatch_wrapper = ERB.new(File.read(TEMPLATE_DIR ** 'dispatch-wrapper.erb')).result(b)
  File.write(binname, dispatch_wrapper)
  $stderr.puts "  chmod  file: #{binname}"
  File.chmod(0774, binname)
end

#cmd_versionObject



22
# File 'lib/exctl/cli.rb', line 22

def cmd_version; puts Exctl.version end

#doc_helpObject



24
# File 'lib/exctl/cli.rb', line 24

def doc_help; 'Output this help.' end

#doc_initObject



32
# File 'lib/exctl/cli.rb', line 32

def doc_init; 'BIN-NAME [DIR=.] - Creates/updates main ctl script and support files in the given DIR.' end

#run!Object



12
13
14
15
16
17
18
19
# File 'lib/exctl/cli.rb', line 12

def run!()
  if self.respond_to?(@cmd) then self.send @cmd
  else
    $stderr.puts "ERROR: Command '#{@cmd}' not implemented."
    $stderr.puts "USAGE: exctl (#{avail_cmds.join('|')}) [OPTIONS]"
    exit 1
  end
end