class Roger::Cli::Base

The Roger main entrypoint!

Attributes

project[RW]

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/roger/cli.rb, line 48
def exit_on_failure?
  true
end
new(*args) click to toggle source
Calls superclass method
# File lib/roger/cli.rb, line 40
def initialize(*args)
  super
  self.class.project ||= initialize_project
end

Public Instance Methods

version() click to toggle source
# File lib/roger/cli.rb, line 99
def version
  shell.say "Roger #{Roger::VERSION}"
end

Protected Instance Methods

initialize_project() click to toggle source
# File lib/roger/cli.rb, line 105
def initialize_project
  # Most unfortunately we have to hack around the
  # class_options not being set in the help command
  # since thor v0.19.4
  path = options[:path] || "."

  if (Pathname.new(path) + "../partials").exist?
    puts "[ERROR]: Don't use the \"html\" path, use the project base path instead"
    exit(1)
  end

  project_options = { shell: shell }
  project_options.update(parse_generic_options(args)[0])
  project_options.update(options)

  Project.new(path, project_options)
end
parse_generic_options(args) click to toggle source

Very simplified method to parse CLI options only works with options starting with – Will also make nested options by using “:” so –a:b:c=yes will become {a: {b: {c: “yes”}}}

# File lib/roger/cli.rb, line 127
def parse_generic_options(args)
  a = args.dup
  arguments = []
  options = {}

  until a.empty?
    arg = a.shift
    case arg
    when /\A--.+=/
      _, option, value = arg.match(/\A--(.+)=(.+)\Z/).to_a
      update_options(option, value, options)
    when /\A--.+/
      if a[0].nil? || a[0].to_s.start_with?("--")
        # Current option is a boolean
        update_options(arg, true, options)
      else
        # Take value from next
        update_options(arg, a.shift, options)
      end
    else
      arguments << arg
    end
  end

  [options, arguments]
end
parse_possible_boolean(value) click to toggle source
# File lib/roger/cli.rb, line 171
def parse_possible_boolean(value)
  case value
  when "true"
    true
  when "false"
    false
  else
    value
  end
end
update_options(composite_key, value, options) click to toggle source

Will update the passed options array by splitting the composite_key by “:” and applying the keys nested

# File lib/roger/cli.rb, line 156
def update_options(composite_key, value, options)
  nesting = options
  keys = composite_key.sub(/\A--/, "").split(":")
  keys.each_with_index do |key, i|
    key = key.to_sym
    if i < keys.length - 1
      nesting[key] ||= {}
      nesting = nesting[key]
    else
      nesting[key] = parse_possible_boolean(value)
    end
  end
  options
end