class CmdLineParser

Parse the cmd line arguments This implementation is heavily inspired by the following stack overflow answer: stackoverflow.com/questions/26434923/parse-command-line-arguments-in-a-ruby-script

Constants

HELP
USAGE

Attributes

args[RW]

Public Class Methods

new(cmdline_args) click to toggle source
# File lib/giblish/cmdline.rb, line 117
def initialize(cmdline_args)
  parse_cmdline cmdline_args

  # handle help and version requests
  if @args[:help]
    puts USAGE
    puts ""
    puts HELP
    exit 0
  end
  if @args[:version]
    puts "Giblish v#{Giblish::VERSION}"
    exit 0
  end

  # act on the parsed cmd line args
  set_log_level
  sanity_check_input
  set_gitrepo_root
end

Public Instance Methods

usage() click to toggle source
# File lib/giblish/cmdline.rb, line 138
def usage
  USAGE
end

Private Instance Methods

add_attribute(attrib_str) click to toggle source

adds the str (must be in key=value format) to the user defined attributes

# File lib/giblish/cmdline.rb, line 229
def add_attribute(attrib_str)
  kv = attrib_str.split("=")
  if kv.length != 2
    puts "Invalid attribute format: #{attrib_str} Must be <key>=<value>"
    exit 1
  end

  @args[:attributes] ||= {}
  @args[:attributes][kv[0]] = kv[1]
end
ensure_required_args() click to toggle source
# File lib/giblish/cmdline.rb, line 259
def ensure_required_args
  # Exit if the user does not supply the required arguments
  return unless !@args[:srcDirRoot] || !@args[:dstDirRoot]

  puts "Error: Too few arguments."
  puts USAGE
  exit 1
end
parse_cmdline(cmdline_args) click to toggle source
# File lib/giblish/cmdline.rb, line 164
def parse_cmdline(cmdline_args)
  # default values for cmd line switches
  @args = {
    help: false,
    version: false,
    force: true,
    format: "html",
    # note that the single quotes are important for the regexp
    includeRegexp: '.*\.(?i)adoc$',
    excludeRegexp: nil,
    flatten: false,
    suppressBuildRef: false,
    indexBaseName: "index",
    localRepoOnly: false,
    resolveDocid: false,
    makeSearchable: false,
    searchAssetsDeploy: nil,
    webPath: nil
  }

  # set default log level
  Giblog.logger.sev_threshold = Logger::WARN

  # defines args without a corresponding flag, the order is important
  unflagged_args = %i[srcDirRoot dstDirRoot]

  # parse cmd line
  next_arg = unflagged_args.first
  cmdline_args.each do |arg|
    case arg
    when "-h", "--help"         then @args[:help]      = true
    when "-v", "--version"      then @args[:version]   = true
    when "-f", "--format   "    then next_arg = :format
    when "-r", "--resource-dir" then next_arg = :resourceDir
    when "-n", "--no-build-ref" then @args[:suppressBuildRef] = true
    when "--index-basename"     then next_arg = :indexBaseName
    when "-i", "--include"      then next_arg = :includeRegexp
    when "-j", "--exclude"      then next_arg = :excludeRegexp
    when "-g", "--git-branches" then next_arg = :gitBranchRegexp
    when "-t", "--git-tags"     then next_arg = :gitTagRegexp
    when "-c", "--local-only"   then @args[:localRepoOnly] = true
    when "-a", "--attribute"    then next_arg = :attributes
    when "-d", "--resolve-docid" then @args[:resolveDocid] = true
    when "-m", "--make-searchable" then @args[:makeSearchable] = true
    when "-mp", "--search-assets-deploy" then next_arg = :searchAssetsDeploy
    when "-s", "--style"        then next_arg = :userStyle
    when "-w", "--web-path"     then next_arg = :webPath
    when "--log-level"          then next_arg = :logLevel
    else
      if next_arg
        if next_arg == :attributes
          # support multiple invocations of -a
          add_attribute arg
        else
          @args[next_arg] = arg
        end
        unflagged_args.delete(next_arg)
      end
      next_arg = unflagged_args.first
    end
  end
end
prevent_invalid_combos() click to toggle source
# File lib/giblish/cmdline.rb, line 240
def prevent_invalid_combos
  # Prevent contradicting options
  if !@args[:resourceDir] && @args[:userStyle]
    puts "Error: The given style would not be used since no resource dir "\
         "was specified (-s specified without -r)"
  elsif @args[:makeSearchable] && @args[:format] != "html"
    puts "Error: The --make-searchable option is only supported for "\
         "html rendering."
  elsif @args[:searchAssetsDeploy] && !@args[:makeSearchable]
    puts "Error: The --search-assets-deploy (-mp) flag is only supported in "\
         "combination with the --make-searchable (-m) flag."
  else
    return
  end

  puts USAGE
  exit 1
end
sanity_check_input() click to toggle source
# File lib/giblish/cmdline.rb, line 159
def sanity_check_input
  ensure_required_args
  prevent_invalid_combos
end
set_gitrepo_root() click to toggle source
# File lib/giblish/cmdline.rb, line 268
def set_gitrepo_root
  # if user don't want no git repo, we're done
  return unless @args[:gitBranchRegexp] || @args[:gitTagRegexp]

  # The user wants to parse a git repo, check that the srcDirRoot is within a
  # git repo if the user wants to generate git-branch specific docs
  @args[:gitRepoRoot] = Giblish::PathManager.find_gitrepo_root(
    @args[:srcDirRoot]
  )
  return unless @args[:gitRepoRoot].nil?

  # We should not get here if everything is koscher...
  puts "Error: Source dir not in a git working dir despite -g or -t option given!"
  puts USAGE
  exit 1
end
set_log_level() click to toggle source
# File lib/giblish/cmdline.rb, line 144
def set_log_level
  log_level = @args[:logLevel] || "info"
  case log_level
  when "debug" then Giblog.logger.sev_threshold = Logger::DEBUG
  when "info"  then Giblog.logger.sev_threshold = Logger::INFO
  when "warn"  then Giblog.logger.sev_threshold = Logger::WARN
  when "error" then Giblog.logger.sev_threshold = Logger::ERROR
  when "fatal" then Giblog.logger.sev_threshold = Logger::FATAL
  else
    puts "Invalid log level specified. Run with -h to see supported levels"
    puts USAGE
    exit 1
  end
end