Module: Simple::CLI
- Extended by:
- Logger
- Defined in:
- lib/simple/cli/runner.rb,
lib/simple/cli.rb,
lib/simple/cli.rb,
lib/simple/cli/helper.rb,
lib/simple/cli/version.rb,
lib/simple/cli/helper/help.rb,
lib/simple/cli/on_exception.rb,
lib/simple/cli/default_options.rb,
lib/simple/cli/helper/short_help.rb,
lib/simple/cli/helper/help_on_command.rb
Overview
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength
Defined Under Namespace
Modules: Adapter, H, Helper, Helpers, Logger, Runner Classes: DefaultOptions
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
-
.included(base) ⇒ Object
It is not strictly necessary to include this module into another module (the “target module”) to be able to run the target module via the command line.
- .on_exception(e) ⇒ Object
-
.run!(service, args: nil) ⇒ Object
Runs the service with the current command line arguments.
Methods included from Logger
Class Method Details
.included(base) ⇒ Object
It is not strictly necessary to include this module into another module (the “target module”) to be able to run the target module via the command line. It is sufficient to just include ::Simple::Service, which turns the target into a service module, and then
However, just including Simple::CLI gives you access to the Simple::CLI::Helpers module as well.
29 30 31 32 |
# File 'lib/simple/cli.rb', line 29 def self.included(base) base.include(::Simple::Service) base.include(::Simple::CLI::Helpers) end |
.on_exception(e) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/simple/cli/on_exception.rb', line 5 def self.on_exception(e) msg = e. msg += " (#{e.class.name})" unless $!.class.name == "RuntimeError" logger.error msg raise(e) if Simple::CLI.logger.level == ::Logger::DEBUG logger.info do backtrace = e.backtrace.reject { |l| l =~ /simple-cli/ } "called from\n " + backtrace[0, 10].join("\n ") end verbosity_hint = "(Backtraces are currently silenced. Run with --verbose to see backtraces.)" logger.warn verbosity_hint exit 2 end |
.run!(service, args: nil) ⇒ Object
Runs the service with the current command line arguments.
The service
argument must match a simple-service service module. The CLI application’s subcommands and their arguments are derived from the actions provided by the service module.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simple/cli.rb', line 39 def self.run!(service, args: nil) ::Simple::Service.verify_service!(service) # prepare arguments: we always duplicate the args array, to make guarantee # we don't interfere with the caller's view of the world. args ||= ARGV args = args.dup logger.level = ::Logger::DEBUG # Extract default options. This returns the command to run, the verbosity # setting, and the help flag. = DefaultOptions.new(args) # Set logger verbosity. This happens before anything else - this way # any further step which raises an exception will have the correct log # level applied during exception handling. logger.level = .log_level # Validate the command. If this command is invalid this will print a short # help message. if .command unless H.action_for_command(service, .command) logger.error "Invalid command '#{.command}'." Helper.short_help!(service) end end # Run help if requested. if .help? if .command Helper.help_on_command! service, .command, verbose: .verbose? else Helper.help! service, verbose: .verbose? end end # Run help if command is missing.. unless .command Helper.short_help! service end # Run service. Runner.run! service, .command, *args, verbose: .verbose? rescue ::Simple::Service::ArgumentError Helper.help_on_command! service, .command, verbose: false rescue StandardError => e on_exception(e) exit 3 end |