Class: Ergane::CommandDefinition
- Defined in:
- lib/ergane/command_definition.rb
Direct Known Subclasses
Defined Under Namespace
Classes: DSL
Instance Attribute Summary collapse
-
#chain ⇒ Object
readonly
Returns the value of attribute chain.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#requirements_block ⇒ Object
readonly
Returns the value of attribute requirements_block.
-
#run_block ⇒ Object
readonly
Returns the value of attribute run_block.
-
#switch_definitions ⇒ Object
readonly
Returns the value of attribute switch_definitions.
-
#switch_parser ⇒ Object
readonly
Returns the value of attribute switch_parser.
Class Method Summary collapse
Instance Method Summary collapse
- #arguments ⇒ Object
- #commands ⇒ Object
- #default_switches ⇒ Object
- #define(chain: [], &block) ⇒ Object
- #parse_args(args, path = []) ⇒ Object
- #pretty_print(q) ⇒ Object
- #run(*args) ⇒ Object
Instance Attribute Details
#chain ⇒ Object (readonly)
Returns the value of attribute chain.
14 15 16 |
# File 'lib/ergane/command_definition.rb', line 14 def chain @chain end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
15 16 17 |
# File 'lib/ergane/command_definition.rb', line 15 def description @description end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
13 14 15 |
# File 'lib/ergane/command_definition.rb', line 13 def label @label end |
#requirements_block ⇒ Object (readonly)
Returns the value of attribute requirements_block.
18 19 20 |
# File 'lib/ergane/command_definition.rb', line 18 def requirements_block @requirements_block end |
#run_block ⇒ Object (readonly)
Returns the value of attribute run_block.
19 20 21 |
# File 'lib/ergane/command_definition.rb', line 19 def run_block @run_block end |
#switch_definitions ⇒ Object (readonly)
Returns the value of attribute switch_definitions.
16 17 18 |
# File 'lib/ergane/command_definition.rb', line 16 def switch_definitions @switch_definitions end |
#switch_parser ⇒ Object (readonly)
Returns the value of attribute switch_parser.
17 18 19 |
# File 'lib/ergane/command_definition.rb', line 17 def switch_parser @switch_parser end |
Class Method Details
.define(label, chain: [], &block) ⇒ Object
41 42 43 44 45 |
# File 'lib/ergane/command_definition.rb', line 41 def self.define(label, chain: [], &block) new(label, *chain).tap do |c| c.define(chain: chain, &block) end end |
Instance Method Details
#arguments ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/ergane/command_definition.rb', line 55 def arguments if commands.any? commands.keys else instance_method(:run).parameters.map { |arg| arg[1] } end end |
#commands ⇒ Object
51 52 53 |
# File 'lib/ergane/command_definition.rb', line 51 def commands self.to_h end |
#default_switches ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/ergane/command_definition.rb', line 63 def default_switches @default_switches ||= switch_definitions.inject({}) do |collector, (label, switch)| collector[label] = switch.default collector end @default_switches.dup end |
#define(chain: [], &block) ⇒ Object
47 48 49 |
# File 'lib/ergane/command_definition.rb', line 47 def define(chain: [], &block) dsl_parse(&block) end |
#parse_args(args, path = []) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ergane/command_definition.rb', line 71 def parse_args(args, path = []) if args.any? && args.first.match(/\A(\w+)\z/) && word = args.first.to_sym case command = self[word] when CommandDefinition path << args.shift command.parse_args(args, path) else puts "no such subcommand #{word} for #{self}" end else # These are args for this command now. Ergane.logger.debug "Now, process these args #{args} for #{label}" switch_parser.order_recognized!(args) [self, args || []] end end |
#pretty_print(q) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ergane/command_definition.rb', line 21 def pretty_print(q) q.text "#{self.class.name} (#{label})" q.text "\n\tCommands:" q.group(1, " {", "}") do self.each_pair do |key, value| q.breakable q.text "#{key}: #{value}" end end end |
#run(*args) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/ergane/command_definition.rb', line 32 def run(*args) if run_block instance_exec(&requirements_block) if requirements_block instance_exec(*args, &run_block) if run_block else puts "show help for this command" end end |