Class: MiGA::Cli
- 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
-
#action ⇒ Object
Action to launch, an object inheriting from MiGA::Cli::Action.
-
#argv ⇒ Object
The CLI parameters (except the task), and Array of String.
-
#data ⇒ Object
readonly
Parsed values as a Hash.
-
#defaults ⇒ Object
Default values as a Hash.
-
#expect_files ⇒ Object
If files are expected after the parameters, a boolean.
-
#expect_operation ⇒ Object
If an operation verb preceding all other arguments is to be expected.
-
#files ⇒ Object
Files passed after all other options, if #expect_files = true.
-
#interactive ⇒ Object
Interactivity with the user is expected.
-
#operation ⇒ Object
Operation preceding all other options, if #expect_operation = true.
-
#opt_common ⇒ Object
writeonly
Include common options, a boolean (true by default).
-
#task ⇒ Object
Task to execute, a symbol.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](k) ⇒ Object
Access parsed data.
-
#[]=(k, v) ⇒ Object
Set parsed data.
-
#advance(*par) ⇒ Object
Same as MiGA::MiGA#advance, but checks if the CLI is verbose.
-
#ask_user(question, default = nil, answers = nil, force = false) ⇒ Object
Ask a question
question
to the user (requires #interactive = true) Thedefault
is used if the answer is empty Theanswers
are supported values, unless nil If –auto, all questions are anwered withdefault
unlessforce
. -
#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. -
#ensure_type(klass) ⇒ Object
Ensure that “type” is passed and valid for the given
klass
. -
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
-
#launch(abort_on_error = false) ⇒ Object
Perform the task requested (see #task); if
abort_on_error
, abort on error. -
#parse(&fun) ⇒ Object
Parse the #argv parameters.
-
#print(*par) ⇒ Object
Print
par
. -
#puts(*par) ⇒ Object
Print
par
, ensuring new line at the end. -
#reset_action ⇒ Object
Redefine #action based on #task.
-
#say(*par) ⇒ Object
Print
par
ensuring new line at the end, iff –verbose. -
#table(header, values, io = $stdout) ⇒ Object
Display a table with headers
header
and contentsvalues
, both Array. -
#task_description ⇒ Object
Task description.
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
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
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
#action ⇒ Object
Action to launch, an object inheriting from MiGA::Cli::Action
27 28 29 |
# File 'lib/miga/cli.rb', line 27 def action @action end |
#argv ⇒ Object
The CLI parameters (except the task), and Array of String
23 24 25 |
# File 'lib/miga/cli.rb', line 23 def argv @argv end |
#data ⇒ Object (readonly)
Parsed values as a Hash
59 60 61 |
# File 'lib/miga/cli.rb', line 59 def data @data end |
#defaults ⇒ Object
Default values as a Hash
55 56 57 |
# File 'lib/miga/cli.rb', line 55 def defaults @defaults end |
#expect_files ⇒ Object
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_operation ⇒ Object
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 |
#files ⇒ Object
Files passed after all other options, if #expect_files = true
35 36 37 |
# File 'lib/miga/cli.rb', line 35 def files @files end |
#interactive ⇒ Object
Interactivity with the user is expected
43 44 45 |
# File 'lib/miga/cli.rb', line 43 def interactive @interactive end |
#operation ⇒ Object
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 |
#task ⇒ Object
Task to execute, a symbol
19 20 21 |
# File 'lib/miga/cli.rb', line 19 def task @task end |
Class Method Details
.EXECS ⇒ Object
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_ALIAS ⇒ Object
107 108 109 |
# File 'lib/miga/cli/base.rb', line 107 def TASK_ALIAS @@TASK_ALIAS end |
.TASK_DESC ⇒ Object
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| (opt) fun[opt] opt_common(opt) end.parse!(@argv) if expect_files @files = argv end end |
#print(*par) ⇒ Object
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_action ⇒ Object
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_description ⇒ Object
Task description
224 225 226 |
# File 'lib/miga/cli.rb', line 224 def task_description @@TASK_DESC[task] end |