Class: MiGA::Cli

Inherits:
MiGA
  • Object
show all
Includes:
Base, ObjectsHelper, OptHelper
Defined in:
lib/miga/cli.rb,
lib/miga/cli/base.rb

Overview

MiGA Command Line Interface API.

Defined Under Namespace

Modules: Base, ObjectsHelper, OptHelper Classes: Action

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ObjectsHelper

#add_metadata, #load_and_filter_datasets, #load_dataset, #load_project, #load_project_or_dataset, #load_result

Methods included from OptHelper

#banner, #opt_common, #opt_filter_datasets, #opt_flag, #opt_object

Methods inherited from MiGA

CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?

Methods included from MiGA::Common::Path

#root_path, #script_path

Methods included from MiGA::Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Methods included from MiGA::Common::Net

#download_file_ftp, #known_hosts, #remote_connection

Methods included from MiGA::Common::SystemCall

#run_cmd, #run_cmd_opts

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

Instance Attribute Details

#actionObject

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



27
28
29
# File 'lib/miga/cli.rb', line 27

def action
  @action
end

#argvObject

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



23
24
25
# File 'lib/miga/cli.rb', line 23

def argv
  @argv
end

#dataObject (readonly)

Parsed values as a Hash



59
60
61
# File 'lib/miga/cli.rb', line 59

def data
  @data
end

#defaultsObject

Default values as a Hash



55
56
57
# File 'lib/miga/cli.rb', line 55

def defaults
  @defaults
end

#expect_filesObject

If files are expected after the parameters, a boolean



31
32
33
# File 'lib/miga/cli.rb', line 31

def expect_files
  @expect_files
end

#expect_operationObject

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



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

def expect_operation
  @expect_operation
end

#filesObject

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



35
36
37
# File 'lib/miga/cli.rb', line 35

def files
  @files
end

#interactiveObject

Interactivity with the user is expected



43
44
45
# File 'lib/miga/cli.rb', line 43

def interactive
  @interactive
end

#operationObject

Operation preceding all other options, if #expect_operation = true



47
48
49
# File 'lib/miga/cli.rb', line 47

def operation
  @operation
end

#opt_common=(value) ⇒ Object (writeonly)

Include common options, a boolean (true by default)



51
52
53
# File 'lib/miga/cli.rb', line 51

def opt_common=(value)
  @opt_common = value
end

#taskObject

Task to execute, a symbol



19
20
21
# File 'lib/miga/cli.rb', line 19

def task
  @task
end

Class Method Details

.EXECSObject



111
112
113
# File 'lib/miga/cli/base.rb', line 111

def EXECS
  @@EXECS
end

.FILE_REGEXP(paired = false) ⇒ Object



115
116
117
# File 'lib/miga/cli/base.rb', line 115

def FILE_REGEXP(paired = false)
  paired ? @@PAIRED_FILE_REGEXP : @@FILE_REGEXP
end

.TASK_ALIASObject



107
108
109
# File 'lib/miga/cli/base.rb', line 107

def TASK_ALIAS
  @@TASK_ALIAS
end

.TASK_DESCObject



103
104
105
# File 'lib/miga/cli/base.rb', line 103

def TASK_DESC
  @@TASK_DESC
end

Instance Method Details

#[](k) ⇒ Object

Access parsed data



148
149
150
151
# File 'lib/miga/cli.rb', line 148

def [](k)
  k = k.to_sym
  @data[k].nil? ? @defaults[k] : @data[k]
end

#[]=(k, v) ⇒ Object

Set parsed data



155
156
157
# File 'lib/miga/cli.rb', line 155

def []=(k, v)
  @data[k.to_sym] = v
end

#advance(*par) ⇒ Object

Same as MiGA::MiGA#advance, but checks if the CLI is verbose



114
115
116
# File 'lib/miga/cli.rb', line 114

def advance(*par)
  super(*par) if self[:verbose]
end

#ask_user(question, default = nil, answers = nil, force = false) ⇒ Object

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/miga/cli.rb', line 123

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

#ensure_par(req, msg = '%<name>s is mandatory: please provide %<flag>s') ⇒ Object

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



207
208
209
210
211
# File 'lib/miga/cli.rb', line 207

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) ⇒ Object

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



215
216
217
218
219
220
# File 'lib/miga/cli.rb', line 215

def ensure_type(klass)
  ensure_par(type: '-t')
  if klass.KNOWN_TYPES[self[:type]].nil?
    raise "Unrecognized type: #{self[:type]}"
  end
end

#launch(abort_on_error = false) ⇒ Object

Perform the task requested (see #task); if abort_on_error, abort on error



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/miga/cli.rb', line 173

def launch(abort_on_error = false)
  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}" }
    abort if abort_on_error
    err
  end
end

#parse(&fun) ⇒ Object

Parse the #argv parameters



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/miga/cli.rb', line 189

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. If the first parameter is IO, the output is sent there, otherwise it’s sent to $stdout



89
90
91
92
# File 'lib/miga/cli.rb', line 89

def print(*par)
  io = par.first.is_a?(IO) ? par.shift : $stdout
  io.print(*par)
end

#puts(*par) ⇒ Object

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



80
81
82
83
# File 'lib/miga/cli.rb', line 80

def puts(*par)
  io = par.first.is_a?(IO) ? par.shift : $stdout
  io.puts(*par)
end

#reset_actionObject

Redefine #action based on #task



161
162
163
164
165
166
167
168
# File 'lib/miga/cli.rb', line 161

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) ⇒ Object

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



106
107
108
109
110
# File 'lib/miga/cli.rb', line 106

def say(*par)
  return unless self[:verbose]

  super(*par)
end

#table(header, values, io = $stdout) ⇒ Object

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



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

def table(header, values, io = $stdout)
  self.puts(io, MiGA.tabulate(header, values, self[:tabular]))
end

#task_descriptionObject

Task description



224
225
226
# File 'lib/miga/cli.rb', line 224

def task_description
  @@TASK_DESC[task]
end