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

Attributes included from MiGA::Common::Net

#remote_connection_uri

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, #list_to_paragraph, #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, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding, #remote_connection

Methods included from MiGA::Common::SystemCall

#run_cmd, #run_cmd_opts

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/miga/cli.rb', line 69

def initialize(argv)
  @data = {}
  @defaults = { verbose: false, tabular: false }
  @opt_common = true
  @original_argv = argv.dup
  @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



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

def action
  @action
end

#argvObject

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



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

def argv
  @argv
end

#dataObject (readonly)

Parsed values as a Hash



67
68
69
# File 'lib/miga/cli.rb', line 67

def data
  @data
end

#defaultsObject

Default values as a Hash



63
64
65
# File 'lib/miga/cli.rb', line 63

def defaults
  @defaults
end

#expect_filesObject

If files are expected after the parameters, a boolean



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

def expect_files
  @expect_files
end

#expect_operationObject

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



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

def expect_operation
  @expect_operation
end

#filesObject

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



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

def files
  @files
end

#files_labelObject

Label used in the usage instead of FILES if #expect_files = true



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

def files_label
  @files_label
end

#interactiveObject

Interactivity with the user is expected



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

def interactive
  @interactive
end

#operationObject

Operation preceding all other options, if #expect_operation = true



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

def operation
  @operation
end

#opt_common=(value) ⇒ Object (writeonly)

Include common options, a boolean (true by default)



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

def opt_common=(value)
  @opt_common = value
end

#original_argvObject

The original ARGV passed to the CLI, an Array of String



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

def original_argv
  @original_argv
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



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

def EXECS
  @@EXECS
end

.FILE_REGEXP(paired = false) ⇒ Object



119
120
121
# File 'lib/miga/cli/base.rb', line 119

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

.TASK_ALIASObject



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

def TASK_ALIAS
  @@TASK_ALIAS
end

.TASK_DESCObject



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

def TASK_DESC
  @@TASK_DESC
end

Instance Method Details

#[](k) ⇒ Object

Access parsed data



157
158
159
160
# File 'lib/miga/cli.rb', line 157

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

#[]=(k, v) ⇒ Object

Set parsed data



164
165
166
# File 'lib/miga/cli.rb', line 164

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

#advance(*par) ⇒ Object

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



123
124
125
# File 'lib/miga/cli.rb', line 123

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/miga/cli.rb', line 132

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



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

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



230
231
232
233
234
235
# File 'lib/miga/cli.rb', line 230

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



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

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



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/miga/cli.rb', line 204

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



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

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



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

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

#reset_actionObject

Redefine #action based on #task



176
177
178
179
180
181
182
183
# File 'lib/miga/cli.rb', line 176

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



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

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



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

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

#task_descriptionObject

Task description



239
240
241
# File 'lib/miga/cli.rb', line 239

def task_description
  @@TASK_DESC[task]
end

#to_hObject

Return parsed and default values as a hash



170
171
172
# File 'lib/miga/cli.rb', line 170

def to_h
  @defaults.merge(@data)
end