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

:nodoc:



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

def initialize # :nodoc:
  super(true)
  @directory = Dir.pwd
  @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)") {|@directory|}
    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)") {|@log_level|}
    opts.on("--log-filter", "-f", Regexp, 'Filter for logging events') {|@log_filter|}
  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 == 'RunCommand' ? true : false))
  end
end

Instance Attribute Details

#directoryObject (readonly)

The website directory. Default: the current working directory.



72
73
74
# File 'lib/webgen/cli.rb', line 72

def directory
  @directory
end

#log_levelObject (readonly)

The log level. Default: Logger::WARN



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

def log_level
  @log_level
end

#verbosityObject (readonly)

The verbosity level. Default: :normal



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

def verbosity
  @verbosity
end

Instance Method Details

#create_websiteObject

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



105
106
107
108
109
110
111
112
# File 'lib/webgen/cli.rb', line 105

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