Class: VGH::CLI
- Inherits:
-
Object
- Object
- VGH::CLI
- 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.
puts cli[:app]
puts cli[:verbose]
puts cli[:logging]
puts cli[:confdir]
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Command line options.
Instance Method Summary collapse
-
#available_apps(apps) ⇒ Object
Returns a list of available apps.
-
#banner(apps) ⇒ Object
Returns the banner.
-
#check_app(apps, app) ⇒ Object
Checks if the provided app is in the available list.
-
#confdir ⇒ Object
Loads the configuration directory.
-
#defaults ⇒ Object
We set default values here.
-
#examples ⇒ Object
Creates sample config folder.
-
#global_options ⇒ Object
Returns the header of the global options section.
-
#header ⇒ Object
Returns the header of the banner.
-
#help ⇒ Object
Loads the help argument.
-
#initialize(apps = APPS.list, app = , args = ARGV) ⇒ CLI
constructor
Collect options.
-
#logging ⇒ Object
Loads the logging argument.
-
#show_help ⇒ Object
Returns the help.
-
#show_version ⇒ Object
Returns a message with the script’s version.
-
#validate(args) ⇒ Object
Parses the arguments.
-
#verbose ⇒ Object
Loads the verbosity argument.
-
#version ⇒ Object
Loads the version argument.
Constructor Details
#initialize(apps = APPS.list, app = , args = ARGV) ⇒ CLI
Collect options
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 (apps) confdir verbose logging examples help version validate(args) check_app(apps, app) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Command line options
46 47 48 |
# File 'lib/vgh/cli.rb', line 46 def @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 |
#banner(apps) ⇒ Object
Returns the banner
87 88 89 90 91 |
# File 'lib/vgh/cli.rb', line 87 def (apps) header available_apps(apps) 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 |
#confdir ⇒ Object
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.(config_dir) if File.directory?(path) @options[:confdir] = path else puts "The configuration directory '#{config_dir}' does not exist!" exit end end end |
#defaults ⇒ Object
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 |
#examples ⇒ Object
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.('../../conf/*', File.dirname(__FILE__))) destination = File.(path) FileUtils.cp examples, destination, :verbose => true exit end end |
#global_options ⇒ Object
Returns the header of the global options section
106 107 108 109 |
# File 'lib/vgh/cli.rb', line 106 def @optparse.separator '' @optparse.separator 'Options:' end |
#header ⇒ Object
Returns the header of the banner
94 95 96 97 |
# File 'lib/vgh/cli.rb', line 94 def header @optparse. = 'Usage: vgh app [options]' @optparse.separator '========================' end |
#help ⇒ Object
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 |
#logging ⇒ Object
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_help ⇒ Object
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_version ⇒ Object
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 |
#verbose ⇒ Object
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 |
#version ⇒ Object
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 |