Class: Vidispine::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vidispine/cli.rb

Constant Summary collapse

LOGGING_LEVELS =
{
  :debug => Logger::DEBUG,
  :info => Logger::INFO,
  :warn => Logger::WARN,
  :error => Logger::ERROR,
  :fatal => Logger::FATAL
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = self.class.parse_arguments, opts = { }) ⇒ CLI

Returns a new instance of CLI.



146
147
148
149
150
151
152
153
154
155
# 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

Class Attribute Details

.argument_parser(options = { }) ⇒ Object



67
68
69
70
71
72
# 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

.argumentsObject



74
75
76
77
78
# 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) ⇒ Object



80
81
82
# 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]) ⇒ Object



84
85
86
# 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

.default_argumentsObject



21
22
23
24
25
26
27
# 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

.help_usageObject



29
30
31
32
# File 'lib/vidispine/cli.rb', line 29

def self.help_usage
  # To be implemented by the child class
  help_usage_default
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



144
145
146
# File 'lib/vidispine/cli.rb', line 144

def arguments
  @arguments
end

#initial_argumentsObject

Returns the value of attribute initial_arguments.



144
145
146
# File 'lib/vidispine/cli.rb', line 144

def initial_arguments
  @initial_arguments
end

#initial_optionsObject

Returns the value of attribute initial_options.



144
145
146
# File 'lib/vidispine/cli.rb', line 144

def initial_options
  @initial_options
end

#loggerObject

Returns the value of attribute logger.



144
145
146
# File 'lib/vidispine/cli.rb', line 144

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



144
145
146
# File 'lib/vidispine/cli.rb', line 144

def options
  @options
end

Class Method Details

.clear_cached_argumentsObject



88
89
90
91
92
93
94
95
# 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_options_file_pathObject



97
98
99
# File 'lib/vidispine/cli.rb', line 97

def self.default_options_file_path
  File.expand_path(File.basename($0, '.*'), '~/.options')
end

.define_parametersObject



16
17
18
19
# 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_nameObject



101
102
103
# File 'lib/vidispine/cli.rb', line 101

def self.executable_name
  @executable_name ||= File.basename($0)
end

.helpObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/vidispine/cli.rb', line 43

def self.help
  @help_usage ||= help_usage_default
  argument_parser.banner = "Usage:\n  \#{help_usage}\n\nOptions:\n  BANNER\n  argument_parser\nend\n"

.help_usage_append(string = '') ⇒ Object



34
35
36
37
# 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_defaultObject



39
40
41
# File 'lib/vidispine/cli.rb', line 39

def self.help_usage_default
  "  #{executable_name} -h | --help"
end

.log_to_as_stringObject



105
106
107
108
109
110
111
112
# 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

.parse_argumentsObject



114
115
116
117
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
# 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]) ⇒ Object



129
130
131
132
133
134
135
136
# 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) ⇒ Object



138
139
140
141
142
# 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

Instance Method Details

#after_initializeObject



54
55
56
# File 'lib/vidispine/cli.rb', line 54

def after_initialize
  # To be implemented by the child class
end

#initialize_logger(args = { }) ⇒ Object

Parameters:

  • args (Hash) (defaults to: { })

Options Hash (args):

  • :logger (Logger)

    A logger to be used

  • :log_to (IO, String)

    An IO device or file to log to

  • :log_level (Integer) — default: Logger::DEBUG

    The logging level to be set to the logger



161
162
163
164
165
166
167
# 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