lib/arborist/cli.rb in arborist-0.0.1.pre20160128152542 vs lib/arborist/cli.rb in arborist-0.0.1.pre20160606141735

- old
+ new

@@ -41,11 +41,11 @@ # The command version version Arborist::VERSION # Use an OpenStruct for options instead of a Hash - use_openstruct( true ) + # use_openstruct( true ) # Subcommand options are independent of global[:ones] subcommand_option_handling :normal # Strict argument validation @@ -60,21 +60,22 @@ Pathname( value.strip ) end # Global options - desc "Specify the config file to load" + desc "Load the specified CONFIGFILE." + arg_name :CONFIGFILE flag [:c, :config], type: Pathname desc 'Enable debugging output' switch [:d, :debug] desc 'Enable verbose output' switch [:v, :verbose] desc 'Set log level to LEVEL (one of %s)' % [Loggability::LOG_LEVELS.keys.join(', ')] - default_value Loggability[self].level + arg_name :LEVEL flag [:l, :loglevel], must_match: Loggability::LOG_LEVELS.keys desc "Don't actually do anything, just show what would happen." switch [:n, 'dry-run'] @@ -84,28 +85,45 @@ # # GLI Event callbacks # + # Set up global options pre do |global, command, options, args| - if loglevel = global[:loglevel] - Loggability.level = loglevel.to_sym - else - Loggability.level = :fatal - end + self.set_logging_level( global[:l] ) # Include a 'lib' directory if there is one $LOAD_PATH.unshift( 'lib' ) if File.directory?( 'lib' ) - self.require_additional_libs( global.requires ) if global.requires + self.require_additional_libs( global[:r] ) if global[:r] self.load_config( global ) + self.set_logging_level( global[:l] ) if global[:l] # again; override config file self.install_highline_colorscheme + self.setup_output( global ) + true end + # Write the error to the log on exceptions. + on_error do |exception| + case exception + when OptionParser::ParseError, GLI::CustomExit + self.log.debug( exception ) + else + self.log.error( exception ) + end + + exception.backtrace.each {|frame| self.log.debug(frame) } + + true + end + + + + ## # Registered subcommand modules singleton_attr_accessor :subcommand_modules ## @@ -160,10 +178,20 @@ def self::reset_prompt @prompt = nil end + ### Set the global logging +level+ if it's defined. + def self::set_logging_level( level=nil ) + if level + Loggability.level = level.to_sym + else + Loggability.level = :fatal + end + end + + ### Load any additional Ruby libraries given with the -r global option. def self::require_additional_libs( requires) requires.each do |path| path = "arborist/#{path}" unless path.start_with?( 'arborist/' ) require( path ) @@ -179,10 +207,11 @@ cs[:error] = [ :bold, :red ] cs[:up] = [ :green ] cs[:down] = [ :red ] cs[:unknown] = [ :dark, :yellow ] cs[:disabled] = [ :dark, :white ] + cs[:quieted] = [ :dark, :green ] cs[:acked] = [ :yellow ] cs[:highlight] = [ :bold, :yellow ] cs[:search_hit] = [ :black, :on_white ] cs[:prompt] = [ :cyan ] cs[:even_row] = [ :bold ] @@ -192,11 +221,11 @@ ### Load the config file using either arborist-base's config-loader if available, or ### fall back to DEFAULT_CONFIG_FILE def self::load_config( global={} ) - Arborist.load_config( global.config ) if global.config + Arborist.load_config( global[:c] ) if global[:c] # Set up the logging formatter Loggability.format_with( :color ) if $stdout.tty? end @@ -219,24 +248,9 @@ if global[:debug] $DEBUG = true Loggability.level = :debug end - end - - - # Write the error to the log on exceptions. - on_error do |exception| - case exception - when OptionParser::ParseError, GLI::CustomExit - self.log.debug( exception ) - else - self.log.error( exception ) - end - - exception.backtrace.each {|frame| self.log.debug(frame) } - - true end # # GLI subcommands