Class: Swamp::CLI
- Inherits:
-
Object
- Object
- Swamp::CLI
- Defined in:
- lib/swamp/cli.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #add(command) ⇒ Object
- #command(name) ⇒ Object
- #dispatch(argv, *additional_args) ⇒ Object
-
#initialize(app_name, options = {}) ⇒ CLI
constructor
A new instance of CLI.
- #load_from_directory(path) ⇒ Object
Constructor Details
#initialize(app_name, options = {}) ⇒ CLI
Returns a new instance of CLI.
15 16 17 18 19 |
# File 'lib/swamp/cli.rb', line 15 def initialize(app_name, = {}) @app_name = app_name @version = [:version] @commands = {} end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
12 13 14 |
# File 'lib/swamp/cli.rb', line 12 def app_name @app_name end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
11 12 13 |
# File 'lib/swamp/cli.rb', line 11 def commands @commands end |
#version ⇒ Object
Returns the value of attribute version.
13 14 15 |
# File 'lib/swamp/cli.rb', line 13 def version @version end |
Instance Method Details
#add(command) ⇒ Object
21 22 23 |
# File 'lib/swamp/cli.rb', line 21 def add(command) @commands[command.name.to_sym] = command end |
#command(name) ⇒ Object
25 26 27 |
# File 'lib/swamp/cli.rb', line 25 def command(name) @commands[name.to_sym] end |
#dispatch(argv, *additional_args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/swamp/cli.rb', line 29 def dispatch(argv, *additional_args) command_name = argv.shift if command_name && command = command(command_name) context = Context.new(self) context. = command.(argv) context.args = argv command.action.call(context, *additional_args) else raise Error, "Command not found" end end |
#load_from_directory(path) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/swamp/cli.rb', line 42 def load_from_directory(path) unless File.directory?(path) raise Error, "No commands directory at #{path}" end Dir[File.join(path, '**', '*.rb')].each do |path| CLIDSL.new(self).instance_eval(File.read(path), path) end end |