Class: Webgen::CLI::CommandParser

Inherits:
CmdParse::CommandParser
  • Object
show all
Defined in:
lib/webgen/cli.rb

Overview

This is the command parser class used for handling the webgen command line interface. After creating an instance, the inherited #parse method can be used for parsing the command line arguments and executing the requested command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandParser

Create a new CommandParser class. The default webgen website (if not specified via the -d option) is taken from the environment variable WEBGEN_WEBSITE or, if it is not set or empty, the current working directory.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/webgen/cli.rb', line 86

def initialize # :nodoc:
  super(true)
  @directory = (ENV['WEBGEN_WEBSITE'].to_s.empty? ? Dir.pwd : ENV['WEBGEN_WEBSITE'])
  @verbosity = :normal
  @log_level = ::Logger::WARN
  @log_filter = nil

  self.program_name = "webgen"
  self.program_version = Webgen::VERSION
  self.options = CmdParse::OptionParserWrapper.new do |opts|
    opts.separator "Global options:"
    opts.on("--directory DIR", "-d", String, "The website directory (default: the current directory)") {|p| @directory = p}
    opts.on("--verbose", "-v", "Print more output") { @verbosity = :verbose }
    opts.on("--quiet", "-q", "No output") { @verbosity = :quiet }
    opts.on("--log-level LEVEL", "-l", Integer, "The logging level (0..debug, 3..error)") {|p| @log_level = p}
    opts.on("--log-filter", "-f", Regexp, 'Filter for logging events') {|p| @log_filter = p}
  end
  self.add_command(CmdParse::HelpCommand.new)
  self.add_command(CmdParse::VersionCommand.new)
  Webgen::CLI.constants.select {|c| c =~ /.+Command$/ }.each do |c|
    self.add_command(Webgen::CLI.const_get(c).new, (c.to_s == 'RunCommand' ? true : false))
  end
end

Instance Attribute Details

#directoryObject (readonly)

The website directory. Default: the current working directory.



75
76
77
# File 'lib/webgen/cli.rb', line 75

def directory
  @directory
end

#log_levelObject (readonly)

The log level. Default: Logger::WARN



81
82
83
# File 'lib/webgen/cli.rb', line 81

def log_level
  @log_level
end

#verbosityObject (readonly)

The verbosity level. Default: :normal



78
79
80
# File 'lib/webgen/cli.rb', line 78

def verbosity
  @verbosity
end

Instance Method Details

#create_websiteObject

Utility method for sub-commands to create the correct Webgen::Website object.



111
112
113
114
115
116
117
118
# File 'lib/webgen/cli.rb', line 111

def create_website
  website = Webgen::Website.new(@directory) do |config|
    config['logger.mask'] = @log_filter
  end
  website.logger.level = @log_level
  website.logger.verbosity = @verbosity
  website
end