Module: CommandLineFlunky
- Defined in:
- lib/commandlineflunky.rb,
lib/commandlineflunky.rb,
lib/commandlineflunky.rb
Constant Summary collapse
- COMMAND_FOLDER =
$stderr.puts STARTUP_MESSAGE unless $has_put_startup_message
Dir.pwd
- SCRIPT_FOLDER =
i.e. where the script using command line flunky is
File.dirname(File.(SCRIPT_FILE))
- SYS =
(ENV['COMMAND_LINE_FLUNKY_SYSTEM'] or "genericlinux")
- GLOBAL_BINDING =
binding- CLF_TO_SHORT_COPTS =
A lookup hash which gives the appropriate short command option (copt) key for a given long command flag
COMMAND_LINE_FLAGS.inject({}) do |hash, arr| unless arr.size == 2 long, short, req = arr letter = short[1,1] hash[long] = letter.to_sym end hash end
- CLF_INVERSE_BOOLS =
CLF_BOOLS = [:H, :U, :u, :A, :a, :T, :N, :q, :z, :d] CLF_BOOLS = [:s, :r, :D, :H, :U, :u, :L, :l, :A, :a, :T, :N,:V, :q, :z, :d] #
[]
- LONG_TO_SHORT =
a look up hash that converts the long form of the command options to the short form (NB command options e.g. use_large_cache have a different form from command line flags e.g. --use-large-cache)
COMMAND_LINE_FLAGS.inject({}) do |hash, arr| unless arr.size == 2 #No short version long, short, req = arr letter = short[1,1] hash[long[2, long.size].gsub(/\-/, '_').to_sym] = letter.to_sym end hash end
- DEFAULT_COMMAND_OPTIONS =
Default command options; they are usually determined by the command line flags, but can be set independently
{}
- INTERACTIVE_METHODS =
"CommandLineFlunky::COMMANDS.each do |command|\n eval(\"def #\\{command[1]}(*args) \n CommandLineFlunky.send(#\\{command[0].to_sym.inspect}, *args)\n end\")\n\n"- @@sys =
SYS
Class Method Summary collapse
- .gets ⇒ Object
- .interactive_mode(copts = {}) ⇒ Object
-
.process_command_line_option(opt, arg, copts) ⇒ Object
Converts a command line flag opt with value arg to a command option which is stored in copts.
- .read_default_command_options(copts) ⇒ Object
-
.run_script ⇒ Object
def self.setup # end.
- .runner ⇒ Object
- .set_default_command_options_from_command_line ⇒ Object
Instance Method Summary collapse
-
#gets ⇒ Object
No reading from the command line thank you very much!.
Class Method Details
.gets ⇒ Object
15 16 17 |
# File 'lib/commandlineflunky.rb', line 15 def self.gets $stdin.gets end |
.interactive_mode(copts = {}) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/commandlineflunky.rb', line 187 def CommandLineFlunky.interactive_mode(copts={}) # process_command_options(copts) unless false and FileTest.exist? (ENV['HOME'] + "/.#{PROJECT_NAME}_interactive_options.rb") File.open(ENV['HOME'] + "/.#{PROJECT_NAME}_interactive_options.rb", 'w') do |file| file.puts "$has_put_startup_message = true #please leave!\n$command_line_flunky_interactive_mode = true #please leave!\nrequire 'yaml'\n\ndef reset\n Dispatcher.reset_application!\nend\n \nIRB.conf[:AUTO_INDENT] = true\nIRB.conf[:USE_READLINE] = true\nIRB.conf[:LOAD_MODULES] = [] unless IRB.conf.key?(:LOAD_MODULES)\nunless IRB.conf[:LOAD_MODULES].include?('irb/completion')\n IRB.conf[:LOAD_MODULES] << 'irb/completion'\nend \n\n \nrequire 'irb/completion'\nrequire 'irb/ext/save-history'\nIRB.conf[:PROMPT_MODE] = :SIMPLE\nIRB.conf[:SAVE_HISTORY] = 100\nIRB.conf[:HISTORY_FILE] = \"\\#\\{ENV['HOME']}/.\#{PROJECT_NAME}_irb_save_history\"\nIRB.conf[:INSPECT_MODE] = false\n\n\n" end end File.open(".int.tmp.rb", 'w')do |file| file.puts "#{copts.inspect}.each do |key, val| CommandLineFlunky::DEFAULT_COMMAND_OPTIONS[key] = val end" file.puts CommandLineFlunky::INTERACTIVE_METHODS end exec %[#{Config::CONFIG['bindir']}/irb#{Config::CONFIG['ruby_install_name'].sub(/ruby/, '')} -f -I '#{SCRIPT_FOLDER}' -I '#{File.dirname(__FILE__)}' -I '#{Dir.pwd}' -I '#{ENV['HOME']}' -r '.#{PROJECT_NAME}_interactive_options' -r '#{File.basename(SCRIPT_FILE)}' -r .int.tmp ] end |
.process_command_line_option(opt, arg, copts) ⇒ Object
Converts a command line flag opt with value arg to a command option which is stored in copts
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/commandlineflunky.rb', line 67 def self.process_command_line_option(opt, arg, copts) if CLF_BOOLS.include? CLF_TO_SHORT_COPTS[opt] copts[CLF_TO_SHORT_COPTS[opt]] = true elsif CLF_INVERSE_BOOLS.include? CLF_TO_SHORT_COPTS[opt] copts[CLF_TO_SHORT_COPTS[opt]] = false elsif CLF_TO_SHORT_COPTS[opt] # Applies to most options copts[CLF_TO_SHORT_COPTS[opt]] = arg else copts[opt[2, opt.size].gsub(/\-/, '_').to_sym] = arg end copts end |
.read_default_command_options(copts) ⇒ Object
95 96 97 98 99 |
# File 'lib/commandlineflunky.rb', line 95 def self.(copts) DEFAULT_COMMAND_OPTIONS.each do |key, value| copts[key] ||= value end end |
.run_script ⇒ Object
def self.setup
end
231 232 233 234 235 236 237 |
# File 'lib/commandlineflunky.rb', line 231 def CommandLineFlunky.run_script CommandLineFlunky.setup(DEFAULT_COMMAND_OPTIONS) return if $command_line_flunky_interactive_mode command = COMMANDS.find{|com| com.slice(0..1).include? ARGV[0]} raise "Command #{ARGV[0]} not found" unless command send(command[0].to_sym, *ARGV.slice(1...(1+command[2])), DEFAULT_COMMAND_OPTIONS) end |
.runner ⇒ Object
183 184 185 |
# File 'lib/commandlineflunky.rb', line 183 def self.runner @runner end |
.set_default_command_options_from_command_line ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/commandlineflunky.rb', line 84 def self. opts = GetoptLong.new(*COMMAND_LINE_FLAGS) opts.each do |opt, arg| process_command_line_option(opt, arg, DEFAULT_COMMAND_OPTIONS) end raise "\n\nCannot use large cache ('-U' or '-u' ) if submitting runs" if DEFAULT_COMMAND_OPTIONS[:U] and (DEFAULT_COMMAND_OPTIONS[:s] or DEFAULT_COMMAND_OPTIONS[:P]) end |
Instance Method Details
#gets ⇒ Object
No reading from the command line thank you very much!
12 13 14 |
# File 'lib/commandlineflunky.rb', line 12 def gets #No reading from the command line thank you very much! $stdin.gets end |