lib/rmega/cli.rb in rmega-0.2.2 vs lib/rmega/cli.rb in rmega-0.2.4

- old
+ new

@@ -4,11 +4,11 @@ module Rmega module CLI module Helpers def cli_options - $cli_options ||= {} + $cli_options ||= read_configuration_file end def cli_prompt_password print("Enter password: ") password = STDIN.noecho(&:gets) @@ -25,16 +25,18 @@ def configuration_filepath File.expand_path('~/.rmega') end def read_configuration_file - return unless File.exists?(configuration_filepath) - cli_options = YAML.load_file(configuration_filepath) - cli_options.keys.each { |k| cli_options[k.to_sym] = cli_options.delete(k) } - puts "Loaded configuration file #{configuration_filepath}" if cli_options[:debug] + return {} unless File.exists?(configuration_filepath) + + opts = YAML.load_file(configuration_filepath) + opts.keys.each { |k| opts[k.to_sym] = opts.delete(k) } # symbolize_keys! + + return opts rescue Exception => ex - raise(ex) if cli_options[:debug] + raise(ex) end def apply_cli_options cli_options.each do |key, value| Rmega.options.__send__("#{key}=", value) @@ -42,12 +44,20 @@ Rmega.logger.level = ::Logger::DEBUG if cli_options[:debug] Rmega.options.show_progress = true end def apply_opt_parser_options(opts) - opts.on("-t NUM", "--thread_pool_size", "Number of threads to use") { |n| - cli_options[:thread_pool_size] = n.to_i + opts.on("-t NUM", "--thread_pool_size", "Number of threads to use [1-8], default and recommended is #{Rmega.options.thread_pool_size}") { |num| + num = num.to_i + + if num <= 0 + num = 1 + elsif num > 8 + num = 8 + end + + cli_options[:thread_pool_size] = num } opts.on("--proxy-addr ADDRESS", "Http proxy address") { |value| cli_options[:http_proxy_address] = value } @@ -69,11 +79,11 @@ } opts.on("-v", "--version", "Print the version number") { puts Rmega::VERSION puts Rmega::HOMEPAGE - exit!(0) + exit(0) } end def traverse_storage(node, path, opts = {}) path.gsub!(/^\/|\/$/, "") @@ -97,10 +107,9 @@ end end end def cli_rescue - read_configuration_file apply_cli_options yield rescue Interrupt puts "\nInterrupted" rescue Exception => ex