Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

84 lines (71 sloc) 2.266 kB
module Boson
# Base class for runners.
class BareRunner
DEFAULT_LIBRARIES = []
# Default options for parse_args
GLOBAL_OPTIONS = {
help: { type: :boolean, desc: "Displays this help message" }
}
module API
# Loads rc
def start(*)
@options ||= {}
load_rc
end
# Default libraries loaded by init
def default_libraries
DEFAULT_LIBRARIES
end
def all_libraries
default_libraries
end
# Loads default libraries
def init
Manager.load default_libraries, load_options
end
# Wrapper around abort
def abort_with(message)
abort message
end
end
class<<self
include API
# Executes a command and handles invalid args
def execute_command(cmd, args)
Boson.full_invoke(cmd, args)
rescue ArgumentError
raise if !allowed_argument_error?($!, cmd, args)
abort_with "'#{cmd}' was called incorrectly.\nUsage: " + Command.usage(cmd)
rescue NoMethodError => err
raise if !err.backtrace.first.include?('`full_invoke')
no_command_error cmd
end
# Use to abort when no command found
def no_command_error(cmd)
abort_with %[Could not find command "#{cmd}"]
end
# Determines if a user command argument error or an internal Boson one
def allowed_argument_error?(err, cmd, args)
(err.message[/wrong number of arguments/] &&
(cmd_obj = Command.find(cmd)) && cmd_obj.arg_size != args.size)
end
private
def parse_args(args)
@option_parser = OptionParser.new(self::GLOBAL_OPTIONS)
options = @option_parser.parse(args.dup, :opts_before_args=>true)
new_args = @option_parser.non_opts
[new_args[0], options, new_args[1..-1]]
end
def load_rc
rc = ENV['BOSONRC'] || '~/.bosonrc'
load(rc) if !rc.empty? && File.exists?(File.expand_path(rc))
rescue StandardError, SyntaxError, LoadError => err
warn "Error while loading #{rc}:\n"+
"#{err.class}: #{err.message}\n #{err.backtrace.join("\n ")}"
end
def load_options
{:verbose=>@options[:verbose]}
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.