class Webgen::CLI::CommandParser

This is the command parser class used for handling the webgen command line interface.

After creating an instance, the parse method can be used for parsing the command line arguments and executing the requested command.

Attributes

directory[R]

The website directory. Default: the value of the WEBGEN_WEBSITE environment variable or the current working directory.

Note: Only available after the website method has been called (which is always the case when a command is executed).

log_level[R]

The log level. Default: Logger::INFO

verbose[R]

Specifies whether verbose output should be used.

Public Class Methods

new() click to toggle source

Create a new CommandParser class.

Calls superclass method
    # File lib/webgen/cli.rb
 84 def initialize
 85   super(handle_exceptions: :no_help)
 86   @directory = nil
 87   @verbose = false
 88   @do_search = false
 89   @log_level = ::Logger::INFO
 90   @config_options = {}
 91 
 92   add_command(CmdParse::VersionCommand.new(add_switches: false))
 93   add_command(CmdParse::HelpCommand.new)
 94 
 95   main_options.program_name = "webgen"
 96   main_options.version = Webgen::VERSION
 97   main_options do |opts|
 98     opts.on("--directory DIR", "-d", String, "The website directory to use") do |d|
 99       @directory = d
100     end
101     opts.on("--search-dirs", "-s", "Search parent directories for website directory") do |s|
102       @do_search = s
103     end
104     opts.on("--version", "-V", "Show webgen version information") do
105       main_command.commands['version'].execute
106     end
107   end
108 
109   global_options do |opts|
110     opts.on("-c", "--[no-]color", "Colorize output (default: #{Webgen::CLI::Utils.use_colors ? "yes" : "no"})") do |a|
111       Webgen::CLI::Utils.use_colors = a
112     end
113     opts.on("-v", "--[no-]verbose", "Verbose output (default: no)") do |v|
114       @verbose = v
115       update_logger(@website.logger) unless @website.nil?
116     end
117     opts.on("-q", "--[no-]quiet", "Quiet output (default: no)") do |v|
118       @log_level = (v ? ::Logger::WARN : :Logger::INFO)
119       update_logger(@website.logger) unless @website.nil?
120     end
121     opts.on("-n", "--[no-]dry-run", "Do a dry run, i.e. don't actually write anything (default: no)") do |v|
122       @config_options['website.dry_run'] = v
123       unless @website.nil?
124         update_config(@website.config)
125         update_logger(@website.logger)
126       end
127     end
128     opts.on("-o", "--option CONFIG_OPTION", String, "Specify a simple configuration option (key=value)") do |v|
129       k, v = v.split('=')
130       begin
131         @config_options[k.to_s] = YAML.load(v.to_s)
132       rescue YAML::SyntaxError
133         raise ConfigurationOptionError.new("Couldn't parse value for '#{k}': #{$!}")
134       end
135       update_config(@website.config) unless @website.nil?
136     end
137     opts.on("--[no-]debug", "Enable debugging") do |v|
138       @log_level = (v ? ::Logger::DEBUG : :Logger::INFO)
139       update_logger(@website.logger) unless @website.nil?
140     end
141   end
142   add_command(Webgen::CLI::GenerateCommand.new, default: true)
143   add_command(Webgen::CLI::ShowCommand.new)
144   add_command(Webgen::CLI::CreateCommand.new)
145   add_command(Webgen::CLI::InstallCommand.new)
146 end

Public Instance Methods

parse(argv = ARGV) click to toggle source

Parse the command line arguments.

Once the website directory information is gathered, the Webgen::Website is initialized to allow additional CLI commands to be added by extensions.

Calls superclass method
    # File lib/webgen/cli.rb
194 def parse(argv = ARGV)
195   super do |level, name|
196     if level == 0
197       # Create website object/automatically performs initialization; needed so that custom
198       # commands can be added
199       begin
200         website
201       rescue Webgen::BundleLoadError
202       end
203     end
204   end
205 rescue
206   puts "webgen encountered a problem:\n  " + $!.message
207   puts $!.backtrace if @log_level == ::Logger::DEBUG
208   exit(1)
209 end
website(force_new = false) click to toggle source

Utility method for getting the Webgen::Website object.

Returns a new website object (not an already created one) if force_new is set to true.

    # File lib/webgen/cli.rb
151 def website(force_new = false)
152   return @website if defined?(@website) && !force_new
153 
154   @directory = ENV['WEBGEN_WEBSITE'] if @directory.nil? && !ENV['WEBGEN_WEBSITE'].to_s.empty?
155   if @directory.nil? && @do_search
156     dir = Dir.pwd
157     file_missing = nil
158     dir = File.dirname(dir) while dir != '/' && (file_missing = !File.exist?(File.join(dir, Webgen::Website::CONFIG_FILENAME)))
159     @directory = dir if !file_missing
160   end
161   @directory = Dir.pwd if @directory.nil?
162   @website = Webgen::Website.new(@directory, Webgen::CLI::Logger.new) do |site, before_init|
163     if before_init
164       site.ext.cli = self
165       update_logger(site.logger)
166     else
167       update_config(site.config)
168     end
169   end
170 end

Private Instance Methods

update_config(config) click to toggle source
    # File lib/webgen/cli.rb
179 def update_config(config)
180   @config_options.each do |k, v|
181     if config.option?(k)
182       config[k] = v
183     else
184       raise ConfigurationOptionError.new("Unknown option '#{k}'")
185     end
186   end
187 end
update_logger(logger) click to toggle source
    # File lib/webgen/cli.rb
172 def update_logger(logger)
173   logger.level = @log_level
174   logger.verbose = @verbose
175   logger.prefix = '[DRY-RUN] ' if @config_options['website.dry_run']
176 end