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.



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

def initialize # :nodoc:
  super(true)
  @directory = nil
  @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)
end

Instance Attribute Details

#directoryObject (readonly)

The website directory. Default: the current working directory.



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

def directory
  @directory
end

#log_levelObject (readonly)

The log level. Default: Logger::WARN



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

def log_level
  @log_level
end

#verbosityObject (readonly)

The verbosity level. Default: :normal



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

def verbosity
  @verbosity
end

Instance Method Details

#create_websiteObject

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



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

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

#parse(argv = ARGV) ⇒ Object

:nodoc:



121
122
123
124
125
126
127
128
129
130
# File 'lib/webgen/cli.rb', line 121

def parse(argv = ARGV)
  super do |level, cmd_name|
    if level == 0
      create_website.init
      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
  end
end