Class: Caml::CLI
- Inherits:
-
Thor
- Object
- Thor
- Caml::CLI
- Defined in:
- lib/caml/cli.rb
Constant Summary collapse
- THOR_TYPES =
{ 'string' => :string, 'boolean' => :boolean, 'numeric' => :numeric }.freeze
Class Method Summary collapse
- .define_dispatcher(task, runner) ⇒ Object
- .exit_on_failure? ⇒ Boolean
- .register(config:, runner:) ⇒ Object
- .register_option(opt) ⇒ Object
- .register_task(task, runner) ⇒ Object
- .thor_type(yaml_type) ⇒ Object
- .usage(task) ⇒ Object
Instance Method Summary collapse
Class Method Details
.define_dispatcher(task, runner) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/caml/cli.rb', line 36 def self.define_dispatcher(task, runner) define_method(task.name) do |*positional| args_hash = task.args.each_with_index.to_h { |arg, i| [arg.name, positional[i]] } runner.run(task.name, args: args_hash, opts: .to_h) end end |
.exit_on_failure? ⇒ Boolean
11 12 13 |
# File 'lib/caml/cli.rb', line 11 def self.exit_on_failure? true end |
.register(config:, runner:) ⇒ Object
25 26 27 |
# File 'lib/caml/cli.rb', line 25 def self.register(config:, runner:) config.tasks.each { |task| register_task(task, runner) } end |
.register_option(opt) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/caml/cli.rb', line 43 def self.register_option(opt) attrs = { type: thor_type(opt.type), desc: opt.desc.to_s, aliases: opt.aliases.map { |a| "-#{a}" } } attrs[:default] = opt.default unless opt.default.nil? method_option opt.name, **attrs end |
.register_task(task, runner) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/caml/cli.rb', line 29 def self.register_task(task, runner) task.opts.each { |opt| register_option(opt) } desc usage(task), task.desc.to_s task.aliases.each { |alias_name| map alias_name => task.name.to_sym } define_dispatcher(task, runner) end |
.thor_type(yaml_type) ⇒ Object
53 54 55 |
# File 'lib/caml/cli.rb', line 53 def self.thor_type(yaml_type) THOR_TYPES.fetch(yaml_type, :string) end |
.usage(task) ⇒ Object
57 58 59 60 |
# File 'lib/caml/cli.rb', line 57 def self.usage(task) positional = task.args.map { |arg| arg.name.upcase }.join(' ') positional.empty? ? task.name : "#{task.name} #{positional}" end |