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 #parse method can be used for parsing the command line arguments and executing the requested command.

Defined Under Namespace

Classes: ConfigurationOptionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandParser

Create a new CommandParser class.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/webgen/cli.rb', line 85

def initialize
  super(handle_exceptions: :no_help)
  @directory = nil
  @verbose = false
  @do_search = false
  @log_level = ::Logger::INFO
  @config_options = {}

  add_command(CmdParse::VersionCommand.new(add_switches: false))
  add_command(CmdParse::HelpCommand.new)

  main_options.program_name = "webgen"
  main_options.version = Webgen::VERSION
  main_options do |opts|
    opts.on("--directory DIR", "-d", String, "The website directory to use") do |d|
      @directory = d
    end
    opts.on("--search-dirs", "-s", "Search parent directories for website directory") do |s|
      @do_search = s
    end
    opts.on("--version", "-V", "Show webgen version information") do
      main_command.commands['version'].execute
    end
  end

  global_options do |opts|
    opts.on("-c", "--[no-]color", "Colorize output (default: #{Webgen::CLI::Utils.use_colors ? "yes" : "no"})") do |a|
      Webgen::CLI::Utils.use_colors = a
    end
    opts.on("-v", "--[no-]verbose", "Verbose output (default: no)") do |v|
      @verbose = v
      update_logger(@website.logger) unless @website.nil?
    end
    opts.on("-q", "--[no-]quiet", "Quiet output (default: no)") do |v|
      @log_level = (v ? ::Logger::WARN : :Logger::INFO)
      update_logger(@website.logger) unless @website.nil?
    end
    opts.on("-n", "--[no-]dry-run", "Do a dry run, i.e. don't actually write anything (default: no)") do |v|
      @config_options['website.dry_run'] = v
      unless @website.nil?
        update_config(@website.config)
        update_logger(@website.logger)
      end
    end
    opts.on("-o", "--option CONFIG_OPTION", String, "Specify a simple configuration option (key=value)") do |v|
      k, v = v.split('=')
      begin
        @config_options[k.to_s] = YAML.load(v.to_s)
      rescue YAML::SyntaxError
        raise ConfigurationOptionError.new("Couldn't parse value for '#{k}': #{$!}")
      end
      update_config(@website.config) unless @website.nil?
    end
    opts.on("--[no-]debug", "Enable debugging") do |v|
      @log_level = (v ? ::Logger::DEBUG : :Logger::INFO)
      update_logger(@website.logger) unless @website.nil?
    end
  end
  add_command(Webgen::CLI::GenerateCommand.new, default: true)
  add_command(Webgen::CLI::ShowCommand.new)
  add_command(Webgen::CLI::CreateCommand.new)
  add_command(Webgen::CLI::InstallCommand.new)
end

Instance Attribute Details

#directoryObject (readonly)

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).



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

def directory
  @directory
end

#log_levelObject (readonly)

The log level. Default: Logger::INFO



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

def log_level
  @log_level
end

#verboseObject (readonly)

Specifies whether verbose output should be used.



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

def verbose
  @verbose
end

Instance Method Details

#parse(argv = ARGV) ⇒ Object

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.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/webgen/cli.rb', line 195

def parse(argv = ARGV)
  super do |level, name|
    if level == 0
      # Create website object/automatically performs initialization; needed so that custom
      # commands can be added
      begin
        website
      rescue Webgen::BundleLoadError
      end
    end
  end
rescue
  puts "webgen encountered a problem:\n  " + $!.message
  puts $!.backtrace if @log_level == ::Logger::DEBUG
  exit(1)
end

#website(force_new = false) ⇒ Object

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.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/webgen/cli.rb', line 152

def website(force_new = false)
  return @website if defined?(@website) && !force_new

  @directory = ENV['WEBGEN_WEBSITE'] if @directory.nil? && !ENV['WEBGEN_WEBSITE'].to_s.empty?
  if @directory.nil? && @do_search
    dir = Dir.pwd
    file_missing = nil
    dir = File.dirname(dir) while dir != '/' && (file_missing = !File.exist?(File.join(dir, Webgen::Website::CONFIG_FILENAME)))
    @directory = dir if !file_missing
  end
  @directory = Dir.pwd if @directory.nil?
  @website = Webgen::Website.new(@directory, Webgen::CLI::Logger.new) do |site, before_init|
    if before_init
      site.ext.cli = self
      update_logger(site.logger)
    else
      update_config(site.config)
    end
  end
end