Class: Greenhouse::CLI

Inherits:
Object
  • Object
show all
Includes:
Scripts::Script
Defined in:
lib/greenhouse/cli.rb

Class Method Summary collapse

Methods included from Scripts::Script

included

Class Method Details

.add_arguments(*args) ⇒ Object



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

def self.add_arguments(*args)
  keep = []
  args.each_with_index do |arg,a|
    if Commands::exists?(arg)
      begin
        @command = Commands::command(arg)
        @command.set_arguments(*args.slice(a+1,args.length-a))
      rescue Scripts::InvalidArgument => e
        puts e.message
        Commands::command(arg).usage
        exit 1
      end
      break
    end

    keep << arg
  end
  super(*keep)
end

.binaryObject



48
49
50
# File 'lib/greenhouse/cli.rb', line 48

def self.binary
  @binary ||= "greenhouse"
end

.binary=(bin) ⇒ Object



44
45
46
# File 'lib/greenhouse/cli.rb', line 44

def self.binary=(bin)
  @binary = bin
end

.command_nameObject



52
53
54
# File 'lib/greenhouse/cli.rb', line 52

def self.command_name
  binary
end

.debug?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/greenhouse/cli.rb', line 12

def self.debug?
  user_arguments.map(&:key).include?("-d")
end

.exec(cmd) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/greenhouse/cli.rb', line 16

def self.exec(cmd)
  if verbose?
    system cmd
  else
    `#{cmd}`
  end
end

.startObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/greenhouse/cli.rb', line 85

def self.start
  begin
    set_arguments(*ARGV)
  rescue Scripts::InvalidArgument => e
    puts e.message
    return
  rescue Exception => e
    usage
    return
  end

  if @command.nil?
    usage
    return
  end

  @command.run
end

.usageObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/greenhouse/cli.rb', line 64

def self.usage
  puts <<USAGE
#{binary} v#{version} 

usage: #{binary} #{valid_arguments.to_s} <command> [<args>]

Arguments:
#{valid_arguments.to_help}

The available greenhouse commands are:
USAGE

  Commands::commands.each do |cmd|
    print "   %-#{Commands::commands.sort { |a,b| a.command_name.length <=> b.command_name.length }.last.command_name.length + 2}s" % cmd.command_name
    puts "# #{cmd.command_summary}"
  end

  puts
  puts "See `#{binary} help <command>` for more information on a specific command."
end

.verbose?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/greenhouse/cli.rb', line 8

def self.verbose?
  user_arguments.map(&:key).include?("-v")
end

.versionObject



60
61
62
# File 'lib/greenhouse/cli.rb', line 60

def self.version
  @version ||= VERSION
end

.version=(ver) ⇒ Object



56
57
58
# File 'lib/greenhouse/cli.rb', line 56

def self.version=(ver)
  @version = ver
end