class Vidispine::CLI

Constants

LOGGING_LEVELS

Attributes

argument_parser[W]
arguments[W]
arguments_from_command_line[W]
arguments_from_options_file[W]
default_arguments[W]
help_usage[W]
arguments[RW]
initial_arguments[RW]
initial_options[RW]
logger[RW]
options[RW]

Public Class Methods

argument_parser(options = { }) click to toggle source
# File lib/vidispine/cli.rb, line 67
def self.argument_parser(options = { })
  return @argument_parser if @argument_parser and !options[:force_new]
  @argument_parser = OptionParser.new
  arguments and define_parameters if options.fetch(:define_parameters, true)
  @argument_parser
end
arguments() click to toggle source
# File lib/vidispine/cli.rb, line 74
def self.arguments
  @arguments ||= begin
    default_arguments.dup
  end
end
arguments_from_command_line(array_of_arguments = ARGV) click to toggle source
# File lib/vidispine/cli.rb, line 80
def self.arguments_from_command_line(array_of_arguments = ARGV)
  @arguments_from_command_line ||= parse_arguments_from_command_line(array_of_arguments)
end
arguments_from_options_file(options_file_path = arguments[:options_file_path]) click to toggle source
# File lib/vidispine/cli.rb, line 84
def self.arguments_from_options_file(options_file_path = arguments[:options_file_path])
  @arguments_from_options_file ||= parse_arguments_from_options_file(options_file_path)
end
clear_cached_arguments() click to toggle source
# File lib/vidispine/cli.rb, line 88
def self.clear_cached_arguments
  @arguments_from_command_line = nil
  @arguments_from_options_file = nil
  @arguments = nil
  @default_arguments = nil
  argument_parser(:force_new => true)
  true
end
default_arguments() click to toggle source
# File lib/vidispine/cli.rb, line 21
def self.default_arguments
  @default_arguments ||= {
    :options_file_path => default_options_file_path,
    :log_level => Logger::ERROR,
    :log_to => STDERR,
  }
end
default_options_file_path() click to toggle source
# File lib/vidispine/cli.rb, line 97
def self.default_options_file_path
  File.expand_path(File.basename($0, '.*'), '~/.options')
end
define_parameters() click to toggle source
# File lib/vidispine/cli.rb, line 16
def self.define_parameters
  # To be implemented by the child class
  argument_parser.on_tail('-h', '--help', 'Display this message.') { puts help; exit }
end
executable_name() click to toggle source
# File lib/vidispine/cli.rb, line 101
def self.executable_name
  @executable_name ||= File.basename($0)
end
help() click to toggle source
# File lib/vidispine/cli.rb, line 43
    def self.help
      @help_usage ||= help_usage_default
      argument_parser.banner = <<-BANNER
Usage:
  #{help_usage}

Options:
      BANNER
      argument_parser
    end
help_usage() click to toggle source
# File lib/vidispine/cli.rb, line 29
def self.help_usage
  # To be implemented by the child class
  help_usage_default
end
help_usage_append(string = '') click to toggle source
# File lib/vidispine/cli.rb, line 34
def self.help_usage_append(string = '')
  usage_string = "\n    #{executable_name} #{string}"
  @help_usage << usage_string unless (@help_usage ||= help_usage_default).include?(usage_string)
end
help_usage_default() click to toggle source
# File lib/vidispine/cli.rb, line 39
def self.help_usage_default
  "  #{executable_name} -h | --help"
end
log_to_as_string() click to toggle source
# File lib/vidispine/cli.rb, line 105
def self.log_to_as_string
  _log_to = arguments[:log_to]
  case _log_to
    when STDERR; 'STDERR'
    when STDOUT; 'STDOUT'
    else _log_to
  end
end
new(args = self.class.parse_arguments, opts = { }) click to toggle source
# File lib/vidispine/cli.rb, line 146
def initialize(args = self.class.parse_arguments, opts = { })
  @initial_arguments = args.dup
  @initial_options = opts.dup

  @arguments = args.dup
  @options = opts.dup

  initialize_logger(@arguments)
  after_initialize
end
parse_arguments() click to toggle source
# File lib/vidispine/cli.rb, line 114
def self.parse_arguments
  argument_parser
  @arguments = default_arguments.merge(arguments_from_options_file).merge(arguments_from_command_line)
end
parse_arguments_from_command_line(array_of_arguments = ARGV) click to toggle source
# File lib/vidispine/cli.rb, line 119
def self.parse_arguments_from_command_line(array_of_arguments = ARGV)
  arguments_before = arguments.dup
  arguments.clear

  argument_parser.parse!(array_of_arguments.dup)
  _arguments_from_options_file = arguments.dup
  @arguments = arguments_before
  _arguments_from_options_file
end
parse_arguments_from_options_file(options_file_path = arguments[:options_file_path]) click to toggle source
# File lib/vidispine/cli.rb, line 129
def self.parse_arguments_from_options_file(options_file_path = arguments[:options_file_path])
  arguments_before = arguments.dup
  arguments.clear
  argument_parser.load(options_file_path)
  _arguments_from_options_file = arguments.dup
  @arguments = arguments_before
  _arguments_from_options_file
end
run(args = nil, init_options = { }, run_options = nil) click to toggle source
# File lib/vidispine/cli.rb, line 138
def self.run(args = nil, init_options = { }, run_options = nil)
  args ||= parse_arguments
  run_options ||= init_options
  new(args, init_options).run(args, run_options)
end

Public Instance Methods

after_initialize() click to toggle source
# File lib/vidispine/cli.rb, line 54
def after_initialize
  # To be implemented by the child class
end
initialize_logger(args = { }) click to toggle source

@param [Hash] args @option args [Logger] :logger A logger to be used @option args [IO, String] :log_to An IO device or file to log to @option args [Integer] :log_level (Logger::DEBUG) The logging level to be set to the logger

# File lib/vidispine/cli.rb, line 161
def initialize_logger(args = { })
  @logger = args[:logger] ||= Logger.new(args[:log_to] ||= STDERR)
  @logger.level = args.fetch(:log_level, Logger::DEBUG)
  args[:logger] = @logger
  args[:log_level] ||= @logger.level
  @logger
end