Class: Codestrap::Cli

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

Defined Under Namespace

Classes: CodestrapPathError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



43
44
45
46
47
# File 'lib/codestrap/cli.rb', line 43

def initialize argv
  self.abspath(argv.shift)
  @command = File.basename self.abspath
  @argv    = argv
end

Instance Attribute Details

#argvString

Same as ARGV

Returns:

  • (String)


14
15
16
# File 'lib/codestrap/cli.rb', line 14

def argv
  @argv
end

#commandString

Calling command

Returns:

  • (String)


9
10
11
# File 'lib/codestrap/cli.rb', line 9

def command
  @command
end

Instance Method Details

#abspath(path = nil) ⇒ String

Absolute path of calling command

Parameters:

  • path (String) (defaults to: nil)

    Path to command

Returns:

  • (String)

    Absolute path to command



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/codestrap/cli.rb', line 22

def abspath path=nil
  return @abspath if @abspath and not path
  if path =~ /\//
    p        = File.expand_path(path)
    @abspath = p if File.exist? p
  else
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |d|
      p = File.join(d, path)
      next unless d.length > 0
      next unless File.exist? p
      @abspath = File.expand_path(p)
      break
    end
  end
  if @abspath
    @abspath
  else
    raise CodestrapPathError, "Could not find #{path}"
  end
end

#optionsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/codestrap/cli.rb', line 49

def options
  @options ||= (
  options          = OpenStruct.new
  options.generate = false
  OptionParser.new do |opts|
    opts.banner = "Usage: #{self.command} [-?|-h|--help]"
    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('--version', 'Show version') do
      puts Codestrap::VERSION
      exit 0
    end

    opts.on('-?', '-h', '--help', 'Show this message') do
      puts opts
      exit 0
    end

    opts.on('-g', '--generate', 'Generate links') do
      options.generate = true
    end
  end.parse!(self.argv)
  options
  )
end