Class: VGH::CLI

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

Overview

Description:

Returns a structure describing the command line options and arguments. See Command line options in the README file.

Usage:

cli = CLI.new.options
puts cli[:app]
puts cli[:verbose]
puts cli[:logging]
puts cli[:confdir]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apps = APPS.list, app = , args = ARGV) ⇒ CLI

Collect options

Parameters:

  • apps (Array) (defaults to: APPS.list)

    A list of available apps

  • app (String) (defaults to: )

    The name of the app

  • args (Array) (defaults to: ARGV)

    The command line arguments



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vgh/cli.rb', line 60

def initialize(apps = APPS.list, app = ARGV[0], args = ARGV)
  @options = {}
  @optparse = OptionParser.new()
  defaults
  banner(apps)
  confdir
  verbose
  logging
  examples
  help
  version
  validate(args)
  check_app(apps, app)
end

Instance Attribute Details

#optionsObject (readonly)

Command line options



46
47
48
# File 'lib/vgh/cli.rb', line 46

def options
  @options
end

Instance Method Details

#available_apps(apps) ⇒ Object

Returns a list of available apps



100
101
102
103
# File 'lib/vgh/cli.rb', line 100

def available_apps(apps)
  @optparse.separator ''
  @optparse.separator "Available apps: #{apps.join(', ')}"
end

Returns the banner



87
88
89
90
91
# File 'lib/vgh/cli.rb', line 87

def banner(apps)
  header
  available_apps(apps)
  global_options
end

#check_app(apps, app) ⇒ Object

Checks if the provided app is in the available list



76
77
78
79
80
81
82
83
84
# File 'lib/vgh/cli.rb', line 76

def check_app(apps, app)
  if apps.include? app
    @options[:app] = app
  else
    puts "ERROR: You need to specify one of the following apps: \
#{apps.join(', ')}"
    exit
  end
end

#confdirObject

Loads the configuration directory



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vgh/cli.rb', line 112

def confdir
  @optparse.on('--confdir=PATH', 'Configuration directory') do |config_dir|
    path = File.expand_path(config_dir)
    if File.directory?(path)
      @options[:confdir] = path
    else
      puts "The configuration directory '#{config_dir}' does not exist!"
      exit
    end
  end
end

#defaultsObject

We set default values here.



49
50
51
52
53
54
# File 'lib/vgh/cli.rb', line 49

def defaults
  @options[:app]     = false
  @options[:verbose] = false
  @options[:logging] = false
  @options[:confdir] = nil
end

#examplesObject

Creates sample config folder



139
140
141
142
143
144
145
146
# File 'lib/vgh/cli.rb', line 139

def examples
  @optparse.on_tail('-e', '--example=PATH', 'Generate example configuration') do |path|
    examples = Dir.glob(File.expand_path('../../conf/*', File.dirname(__FILE__)))
    destination = File.expand_path(path)
    FileUtils.cp examples, destination, :verbose => true
    exit
  end
end

#global_optionsObject

Returns the header of the global options section



106
107
108
109
# File 'lib/vgh/cli.rb', line 106

def global_options
  @optparse.separator ''
  @optparse.separator 'Options:'
end

#headerObject

Returns the header of the banner



94
95
96
97
# File 'lib/vgh/cli.rb', line 94

def header
  @optparse.banner = 'Usage: vgh app [options]'
  @optparse.separator '========================'
end

#helpObject

Loads the help argument



149
150
151
152
153
# File 'lib/vgh/cli.rb', line 149

def help
  @optparse.on_tail('-h', '--help', 'Show this message') do
    show_help
  end
end

#loggingObject

Loads the logging argument



132
133
134
135
136
# File 'lib/vgh/cli.rb', line 132

def logging
  @optparse.on('-l', '--[no-]logging', 'Enable logging') do |logging|
    @options[:logging] = logging
  end
end

#show_helpObject

Returns the help



177
178
179
180
181
182
# File 'lib/vgh/cli.rb', line 177

def show_help
  puts @optparse
  puts ''
  show_version
  exit
end

#show_versionObject

Returns a message with the script’s version



185
186
187
188
# File 'lib/vgh/cli.rb', line 185

def show_version
  puts "VGH Scripts: v#{VGH::VERSION}"
  exit
end

#validate(args) ⇒ Object

Parses the arguments



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vgh/cli.rb', line 163

def validate(args)
  begin
    @optparse.parse!(args)
  rescue OptionParser::ParseError,
         OptionParser::InvalidArgument,
         OptionParser::InvalidOption,
         OptionParser::MissingArgument
    puts $!.to_s
    show_help
    exit
  end
end

#verboseObject

Loads the verbosity argument



125
126
127
128
129
# File 'lib/vgh/cli.rb', line 125

def verbose
  @optparse.on('-v', '--[no-]verbose', 'Run verbosely') do |verbose|
    @options[:verbose] = verbose
  end
end

#versionObject

Loads the version argument



156
157
158
159
160
# File 'lib/vgh/cli.rb', line 156

def version
  @optparse.on_tail('-V', '--version', 'Show version') do
    show_version
  end
end