Class: MOSAIK::CLI
- Inherits:
-
Object
- Object
- MOSAIK::CLI
- Defined in:
- lib/mosaik/cli.rb
Overview
Command line interface
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#command_args ⇒ Object
readonly
Returns the value of attribute command_args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #parse ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mosaik/cli.rb', line 13 def initialize(args) @options = { directory: Dir.pwd, debug: false, } @args = args @command_args = [] parse end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
11 12 13 |
# File 'lib/mosaik/cli.rb', line 11 def args @args end |
#command_args ⇒ Object (readonly)
Returns the value of attribute command_args.
11 12 13 |
# File 'lib/mosaik/cli.rb', line 11 def command_args @command_args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/mosaik/cli.rb', line 11 def @options end |
Instance Method Details
#call ⇒ Object
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 |
# File 'lib/mosaik/cli.rb', line 52 def call # Extract command name command_name = command_args.shift raise UsageError, "no command specified" unless command_name klass = "MOSAIK::Commands::#{command_name.camelize}".safe_constantize raise UsageError, "unknown command: #{command_name}" unless klass # Add command arguments to global argument parser (for the usage message) klass.arguments.each do |args, kwargs, block| parser.on(*args, **kwargs, &block) end parser.separator("\n") # Execute command command = klass .new(, *command_args) command .validate command .call rescue UsageError => e # Don't print tail if no message was passed return usage if e. == e.class.name usage(tail: "#{File.basename($PROGRAM_NAME)}: #{e.}") rescue Error => e fatal "#{File.basename($PROGRAM_NAME)}: #{e.}" raise ExitError, 1 end |
#parse ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mosaik/cli.rb', line 25 def parse # Parse command line arguments (in order) and extract non-option arguments # (unrecognized option values). Raise for invalid option arguments (unrecognized # option keys). "--foo FOO --bar BAR" will result in "--foo" and "FOO" being parsed # correctly, "--bar" and "BAR" will be extracted. # This needs to be in a separate method due to the retry logic. parser.order!(args, into: ) { |value| @command_args << value } rescue OptionParser::InvalidOption => e @command_args += e.args retry end |
#validate ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mosaik/cli.rb', line 37 def validate raise OptionError, "invalid directory: #{[:directory]}" unless File.exist?([:directory]) raise OptionError, "not a directory: #{[:directory]}" unless File.directory?([:directory]) # Set log level MOSAIK.logger.level = [:debug] ? "debug" : "info" # Set configuration MOSAIK.configuration = Configuration.from(File.join([:directory], "mosaik.yml")) rescue Error => e fatal e. raise ExitError, 1 end |