Module: CLI
- Defined in:
- lib/clir/CLI.mod.rb,
lib/clir/Replayer.cls.rb
Overview
A command line is composed by:
- app command (for example: 'rake')
- main command (main_command): firts element not leading with '-'
dans without '=' (for example: 'build' in 'rake build')
- options: elements that lead with '-' or '--'
- params : elments that contain '=' (key=value pairs)
- components: all other elements
We can get this elments with:
CLI.main_command
CLI.options[:<key>]
CLI.option(:key)
CLI.params[:<key>]
CLI.components[<index>]
App can define its own short options (to long options) with:
CLI.set_options_table({short: long, short: long...})
Note : il faut obligatoirement mettre la version courte (souvent
une seule lettre) en Symbol :
CLI.set_options_table({e: :edition})
Defined Under Namespace
Classes: Replayer
Constant Summary collapse
Class Method Summary collapse
-
.command_name ⇒ Object
Command name.
-
.components ⇒ Object
‘components’ are elements of command line that are not options (with leading ‘-’), that are not parameters (key=value paire) and that are not main_command.
-
.get_command_line_data ⇒ Object
For Replayer, return data.
-
.init ⇒ Object
First class method (call it at start-up).
- .main_command ⇒ Object
-
.option(key) ⇒ Object
Option of key
key
. -
.options ⇒ Object
CLI.init first.
- .param(key) ⇒ Object
-
.params ⇒ Object
Command line parameters.
-
.parse(argv) ⇒ Object
Main method which parse command line to get: - main command - options (leadings with -/–) - parameters (key=value pairs).
- .set_command_line_data(data) ⇒ Object
- .set_options_table(table) ⇒ Object
-
.set_tests_on_with_marker ⇒ Object
— Tests Methods —.
- .unset_tests_on_with_marker ⇒ Object
Class Method Details
.command_name ⇒ Object
Command name
Don’t confuse with ‘main command’ which is the very first argument in command line
95 96 97 98 99 |
# File 'lib/clir/CLI.mod.rb', line 95 def command_name @command_name ||= begin File.basename($PROGRAM_NAME,File.extname($PROGRAM_NAME)) end end |
.components ⇒ Object
‘components’ are elements of command line that are not options (with leading ‘-’), that are not parameters (key=value paire) and that are not main_command
83 84 85 86 |
# File 'lib/clir/CLI.mod.rb', line 83 def components defined?(@components) || self.init @components end |
.get_command_line_data ⇒ Object
For Replayer, return data
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/clir/CLI.mod.rb', line 147 def get_command_line_data { raw_command_line: @raw_command_line, command_name: command_name, main_command: main_command, components: components, options: , params: params, table_short2long_options: } end |
.init ⇒ Object
First class method (call it at start-up)
38 39 40 41 |
# File 'lib/clir/CLI.mod.rb', line 38 def init parse(ARGV) Q.init if Q.respond_to?(:init) end |
.main_command ⇒ Object
60 61 62 63 |
# File 'lib/clir/CLI.mod.rb', line 60 def main_command defined?(@main_command) || self.init @main_command end |
.option(key) ⇒ Object
Returns option of key key
.
54 55 56 |
# File 'lib/clir/CLI.mod.rb', line 54 def option(key) [key.to_sym] end |
.options ⇒ Object
CLI.init first.
48 49 50 51 |
# File 'lib/clir/CLI.mod.rb', line 48 def defined?(@options) || self.init @options end |
.param(key) ⇒ Object
73 74 75 |
# File 'lib/clir/CLI.mod.rb', line 73 def param(key) @params[key.to_sym] end |
.params ⇒ Object
Returns command line parameters.
68 69 70 71 |
# File 'lib/clir/CLI.mod.rb', line 68 def params defined?(@params) || self.init @params end |
.parse(argv) ⇒ Object
Main method which parse command line to get:
-
main command
-
options (leadings with -/–)
-
parameters (key=value pairs)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/clir/CLI.mod.rb', line 107 def parse(argv) argv = argv.split(' ') if argv.is_a?(String) if replay_it?(argv) # # Replay last command (if unable) # puts "Je dois apprendre à replayer la commande précédente".jaune puts "Pour ça, je dois enregistrer les inputs précédents.".jaune else # # Regular run # reset @raw_command_line = ([command_name]+argv).join(' ') argv.each do |arg| if arg.start_with?('--') arg, val = key_and_value_in(arg[2..-1]) @options.merge!(arg.to_sym => val) elsif arg.start_with?('-') arg, val = key_and_value_in(arg[1..-1]) arg = long_option_for(arg) @options.merge!(arg.to_sym => val) elsif arg.match?('.=.') key, val = key_and_value_in(arg) @params.merge!(key.to_sym => val) elsif @main_command.nil? @main_command = arg else @components << arg end end end end |
.set_command_line_data(data) ⇒ Object
159 160 161 |
# File 'lib/clir/CLI.mod.rb', line 159 def set_command_line_data(data) data.each do |k, v| instance_variable_set("@#{k}", v) end end |
.set_options_table(table) ⇒ Object
141 142 143 |
# File 'lib/clir/CLI.mod.rb', line 141 def (table) @_app_options_table = table end |
.set_tests_on_with_marker ⇒ Object
— Tests Methods —
165 166 167 |
# File 'lib/clir/CLI.mod.rb', line 165 def set_tests_on_with_marker File.write(MARKER_TESTS_FILE, "#{Time.now}") end |
.unset_tests_on_with_marker ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/clir/CLI.mod.rb', line 169 def unset_tests_on_with_marker if File.exist?(MARKER_TESTS_FILE) File.delete(MARKER_TESTS_FILE) else puts "Weirdly, the test marker file (CLI::MARKER_TESTS_FILE) doesn't exist…".rouge end end |