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 unparsed CLI parameters (except the task), an 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).
-
#original_argv ⇒ Object
The original ARGV passed to the CLI, an Array of String.
-
#task ⇒ Object
Task to execute, a symbol.
Attributes included from MiGA::Common::Net
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, #http_request, #known_hosts, #main_server, #net_method, #normalize_encoding, #remote_connection
Methods included from MiGA::Common::SystemCall
Constructor Details
#initialize(argv) ⇒ Cli
Returns a new instance of Cli.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/miga/cli.rb', line 65 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
#action ⇒ Object
Action to launch, an object inheriting from MiGA::Cli::Action
31 32 33 |
# File 'lib/miga/cli.rb', line 31 def action @action end |
#argv ⇒ Object
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 |
#data ⇒ Object (readonly)
Parsed values as a Hash
63 64 65 |
# File 'lib/miga/cli.rb', line 63 def data @data end |
#defaults ⇒ Object
Default values as a Hash
59 60 61 |
# File 'lib/miga/cli.rb', line 59 def defaults @defaults end |
#expect_files ⇒ Object
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_operation ⇒ Object
If an operation verb preceding all other arguments is to be expected
43 44 45 |
# File 'lib/miga/cli.rb', line 43 def expect_operation @expect_operation end |
#files ⇒ Object
Files passed after all other options, if #expect_files = true
39 40 41 |
# File 'lib/miga/cli.rb', line 39 def files @files end |
#interactive ⇒ Object
Interactivity with the user is expected
47 48 49 |
# File 'lib/miga/cli.rb', line 47 def interactive @interactive end |
#operation ⇒ Object
Operation preceding all other options, if #expect_operation = true
51 52 53 |
# File 'lib/miga/cli.rb', line 51 def operation @operation end |
#opt_common=(value) ⇒ Object (writeonly)
Include common options, a boolean (true by default)
55 56 57 |
# File 'lib/miga/cli.rb', line 55 def opt_common=(value) @opt_common = value end |
#original_argv ⇒ Object
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 |
#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
113 114 115 |
# File 'lib/miga/cli/base.rb', line 113 def EXECS @@EXECS end |
.FILE_REGEXP(paired = false) ⇒ Object
117 118 119 |
# File 'lib/miga/cli/base.rb', line 117 def FILE_REGEXP(paired = false) paired ? @@PAIRED_FILE_REGEXP : @@FILE_REGEXP end |
.TASK_ALIAS ⇒ Object
109 110 111 |
# File 'lib/miga/cli/base.rb', line 109 def TASK_ALIAS @@TASK_ALIAS end |
.TASK_DESC ⇒ Object
105 106 107 |
# File 'lib/miga/cli/base.rb', line 105 def TASK_DESC @@TASK_DESC end |
Instance Method Details
#[](k) ⇒ Object
Access parsed data
153 154 155 156 |
# File 'lib/miga/cli.rb', line 153 def [](k) k = k.to_sym @data[k].nil? ? @defaults[k] : @data[k] end |
#[]=(k, v) ⇒ Object
Set parsed data
160 161 162 |
# File 'lib/miga/cli.rb', line 160 def []=(k, v) @data[k.to_sym] = v end |
#advance(*par) ⇒ Object
Same as MiGA::MiGA#advance, but checks if the CLI is verbose
119 120 121 |
# File 'lib/miga/cli.rb', line 119 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/miga/cli.rb', line 128 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
212 213 214 215 216 |
# File 'lib/miga/cli.rb', line 212 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
220 221 222 223 224 225 |
# File 'lib/miga/cli.rb', line 220 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
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/miga/cli.rb', line 178 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
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/miga/cli.rb', line 194 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
94 95 96 97 |
# File 'lib/miga/cli.rb', line 94 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
85 86 87 88 |
# File 'lib/miga/cli.rb', line 85 def puts(*par) io = par.first.is_a?(IO) ? par.shift : $stdout io.puts(*par) end |
#reset_action ⇒ Object
Redefine #action based on #task
166 167 168 169 170 171 172 173 |
# File 'lib/miga/cli.rb', line 166 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
111 112 113 114 115 |
# File 'lib/miga/cli.rb', line 111 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
102 103 104 |
# File 'lib/miga/cli.rb', line 102 def table(header, values, io = $stdout) self.puts(io, MiGA.tabulate(header, values, self[:tabular])) end |
#task_description ⇒ Object
Task description
229 230 231 |
# File 'lib/miga/cli.rb', line 229 def task_description @@TASK_DESC[task] end |