class MiGA::Cli

MiGA Command Line Interface API.

Attributes

action[RW]

Action to launch, an object inheriting from MiGA::Cli::Action

argv[RW]

The CLI parameters (except the task), and Array of String

data[R]

Parsed values as a Hash

defaults[RW]

Default values as a Hash

expect_files[RW]

If files are expected after the parameters, a boolean

expect_operation[RW]

If an operation verb preceding all other arguments is to be expected

files[RW]

Files passed after all other options, if +#expect_files = true+

interactive[RW]

Interactivity with the user is expected

operation[RW]

Operation preceding all other options, if +#expect_operation = true+

opt_common[W]

Include common options, a boolean (true by default)

task[RW]

Task to execute, a symbol

Public Class Methods

EXECS() click to toggle source
# File lib/miga/cli/base.rb, line 100
def EXECS; @@EXECS end
FILE_REGEXP() click to toggle source
# File lib/miga/cli/base.rb, line 102
def FILE_REGEXP; @@FILE_REGEXP end
TASK_ALIAS() click to toggle source
# File lib/miga/cli/base.rb, line 98
def TASK_ALIAS; @@TASK_ALIAS end
TASK_DESC() click to toggle source
# File lib/miga/cli/base.rb, line 96
def TASK_DESC; @@TASK_DESC end
new(argv) click to toggle source
# File lib/miga/cli.rb, line 61
def initialize(argv)
  @data = {}
  @defaults = { verbose: false, tabular: false }
  @opt_common = true
  @objects = {}
  if argv[0].nil? or argv[0].to_s[0] == '-'
    @task = :generic
  else
    @task = argv.shift.to_sym
    @task = @@TASK_ALIAS[task] unless @@TASK_ALIAS[task].nil?
  end
  @argv = argv
  reset_action
end

Public Instance Methods

[](k) click to toggle source

Access parsed data

# File lib/miga/cli.rb, line 169
def [](k)
  k = k.to_sym
  @data[k].nil? ? @defaults[k] : @data[k]
end
[]=(k, v) click to toggle source

Set parsed data

# File lib/miga/cli.rb, line 176
def []=(k, v)
  @data[k.to_sym] = v
end
advance(step, n = 0, total = nil, bin = true) click to toggle source

Reports the advance of a task at step (String), the n out of total. The advance is reported in powers of 1,024 if bin is true, or powers of 1,000 otherwise. The report goes to $stderr iff –verborse

# File lib/miga/cli.rb, line 117
def advance(step, n = 0, total = nil, bin = true)
  return unless self[:verbose]

  adv = total.nil? ? (n == 0 ? '' : num_suffix(n, bin)) :
    ('%.1f%% (%s/%s)' % [100.0 * n / total,
                         num_suffix(n, bin), num_suffix(total, bin)])
  $stderr.print("[%s] %s %s    \r" % [Time.now, step, adv])
end
ask_user(question, default = nil, answers = nil, force = false) click to toggle source

Ask a question question to the user (requires +#interactive = true+) The default is used if the answer is empty The answers are supported values, unless nil If –auto, all questions are anwered with default unless force

# File lib/miga/cli.rb, line 144
def ask_user(question, default = nil, answers = nil, force = false)
  ans = " (#{answers.join(' / ')})" unless answers.nil?
  dft = " [#{default}]" unless default.nil?
  print "#{question}#{ans}#{dft} > "
  if self[:auto] && !force
    puts ''
  else
    y = gets.chomp
  end
  y = default.to_s if y.nil? or y.empty?
  unless answers.nil? or answers.map(&:to_s).include?(y)
    warn "Answer not recognized: '#{y}'"
    return ask_user(question, default, answers, force)
  end
  y
end
defaults=(hsh) click to toggle source

Set default values in the Hash hsh

# File lib/miga/cli.rb, line 163
def defaults=(hsh)
  hsh.each { |k, v| @defaults[k] = v }
end
ensure_par(req, msg = '%s is mandatory: please provide %s') click to toggle source

Ensure that these parameters have been passed to the CLI, as defined by par, a Hash with object names as keys and parameter flag as values. If missing, raise an error with message msg

# File lib/miga/cli.rb, line 226
def ensure_par(req, msg = '%<name>s is mandatory: please provide %<flag>s')
  req.each do |k, v|
    raise (msg % { name: k, flag: v }) if self[k].nil?
  end
end
ensure_type(klass) click to toggle source

Ensure that “type” is passed and valid for the given klass

# File lib/miga/cli.rb, line 234
def ensure_type(klass)
  ensure_par(type: '-t')
  if klass.KNOWN_TYPES[self[:type]].nil?
    raise "Unrecognized type: #{self[:type]}"
  end
end
launch() click to toggle source

Perform the task requested (see task)

# File lib/miga/cli.rb, line 193
def launch
  begin
    raise "See `miga -h`" if action.nil?

    action.launch
  rescue => err
    $stderr.puts "Exception: #{err}"
    $stderr.puts ''
    err.backtrace.each { |l| $stderr.puts "DEBUG: #{l}" }
    err
  end
end
num_suffix(n, bin = false) click to toggle source
# File lib/miga/cli.rb, line 126
def num_suffix(n, bin = false)
  p = ''
  { T: 4, G: 3, M: 2, K: 1 }.each do |k, x|
    v = (bin ? 1024 : 1e3)**x
    if n > v
      n = '%.1f' % (n / v)
      p = k
      break
    end
  end
  "#{n}#{p}"
end
parse(&fun) click to toggle source

Parse the argv parameters

# File lib/miga/cli.rb, line 208
def parse(&fun)
  if expect_operation
    @operation = @argv.shift unless argv.first =~ /^-/
  end
  OptionParser.new do |opt|
    banner(opt)
    fun[opt]
    opt_common(opt)
  end.parse!(@argv)
  if expect_files
    @files = argv
  end
end
print(*par) click to toggle source

Print par. If the first parameter is IO, the output is sent there, otherwise it's sent to +$stdout+

puts(*par) click to toggle source

Print par, ensuring new line at the end. If the first parameter is IO, the output is sent there, otherwise it's sent to +$stdout+

# File lib/miga/cli.rb, line 80
def puts(*par)
  io = par.first.is_a?(IO) ? par.shift : $stdout
  io.puts(*par)
end
reset_action() click to toggle source

Redefine action based on task

# File lib/miga/cli.rb, line 182
def reset_action
  @action = nil
  if @@EXECS.include? task
    @action = Action.load(task, self)
  else
    warn "No action set for #{task}"
  end
end
say(*par) click to toggle source

Print par ensuring new line at the end, iff –verbose. Date/time each line. If the first parameter is IO, the output is sent there, otherwise it's sent to +$stderr+

Calls superclass method MiGA::MiGA#say
# File lib/miga/cli.rb, line 106
def say(*par)
  return unless self[:verbose]

  super(*par)
end
table(header, values, io = $stdout) click to toggle source

Display a table with headers header and contents values, both Array. The output is printed to io

# File lib/miga/cli.rb, line 97
def table(header, values, io = $stdout)
  self.puts(io, MiGA.tabulate(header, values, self[:tabular]))
end
task_description() click to toggle source

Task description

# File lib/miga/cli.rb, line 243
def task_description
  @@TASK_DESC[task]
end