Class: Samovar::Nested
- Inherits:
-
Object
- Object
- Samovar::Nested
- Defined in:
- lib/samovar/nested.rb
Overview
Represents nested sub-commands in a command.
A ‘Nested` parser allows you to define multiple sub-commands that can be invoked from the parent command.
Instance Attribute Summary collapse
-
#commands ⇒ Object
A mapping of command names to command classes.
-
#default ⇒ Object
readonly
The default command name if none is provided.
-
#key ⇒ Object
The name of the attribute to store the selected command in.
-
#required ⇒ Object
Whether a command is required.
Instance Method Summary collapse
-
#complete(input, context, collected) ⇒ Object
Complete nested command names or continue into a selected command.
-
#initialize(key, commands, default: nil, required: false) ⇒ Nested
constructor
Initialize a new nested command parser.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse a nested command from the input.
-
#suggestions(context) ⇒ Object
Complete nested command names for the current token.
-
#to_a ⇒ Object
Generate an array representation for usage output.
-
#to_s ⇒ Object
Generate a string representation for usage output.
-
#usage(rows) ⇒ Object
Generate usage information for this nested command.
Constructor Details
#initialize(key, commands, default: nil, required: false) ⇒ Nested
Initialize a new nested command parser.
19 20 21 22 23 24 25 26 27 |
# File 'lib/samovar/nested.rb', line 19 def initialize(key, commands, default: nil, required: false) @key = key @commands = commands # This is the default name [of a command], not the default command: @default = default @required = required end |
Instance Attribute Details
#commands ⇒ Object
A mapping of command names to command classes.
37 38 39 |
# File 'lib/samovar/nested.rb', line 37 def commands @commands end |
#default ⇒ Object (readonly)
The default command name if none is provided.
42 43 44 |
# File 'lib/samovar/nested.rb', line 42 def default @default end |
#key ⇒ Object
The name of the attribute to store the selected command in.
32 33 34 |
# File 'lib/samovar/nested.rb', line 32 def key @key end |
#required ⇒ Object
Whether a command is required.
47 48 49 |
# File 'lib/samovar/nested.rb', line 47 def required @required end |
Instance Method Details
#complete(input, context, collected) ⇒ Object
Complete nested command names or continue into a selected command.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/samovar/nested.rb', line 106 def complete(input, context, collected) if input.empty? result = suggestions(context) if result.empty? && @default return Completion::Result.new(collected) + context.complete_command(@commands.fetch(@default)) end return Completion::Result.new(collected) + result end if command = @commands[input.first] input.shift return context.complete_command(command, input) elsif @default return context.complete_command(@commands.fetch(@default), input) else return Completion::Result.new(collected) end end |
#parse(input, parent = nil, default = nil) ⇒ Object
Parse a nested command from the input.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/samovar/nested.rb', line 85 def parse(input, parent = nil, default = nil) if command = @commands[input.first] name = input.shift # puts "Instantiating #{command} with #{input}" command.new(input, name: name, parent: parent, output: parent&.output) elsif default return default elsif @default @commands[@default].new(input, name: @default, parent: parent, output: parent&.output) elsif @required raise MissingValueError.new(parent, @key) end end |
#suggestions(context) ⇒ Object
Complete nested command names for the current token.
131 132 133 134 135 136 137 138 139 |
# File 'lib/samovar/nested.rb', line 131 def suggestions(context) suggestions = @commands.collect do |name, command_class| next unless name.start_with?(context.current) Completion::Suggestion.new(name, description: command_class.description, type: :command) end.compact Completion::Result.new(suggestions) end |
#to_a ⇒ Object
Generate an array representation for usage output.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/samovar/nested.rb', line 59 def to_a usage = [self.to_s] if @commands.size == 0 usage << "No commands available." elsif @commands.size == 1 usage << "Only #{@commands.first}." else usage << "One of: #{@commands.keys.join(', ')}." end if @default usage << "(default: #{@default})" elsif @required usage << "(required)" end return usage end |
#to_s ⇒ Object
Generate a string representation for usage output.
52 53 54 |
# File 'lib/samovar/nested.rb', line 52 def to_s "<#{@key}>" end |
#usage(rows) ⇒ Object
Generate usage information for this nested command.
144 145 146 147 148 149 150 |
# File 'lib/samovar/nested.rb', line 144 def usage(rows) rows << self @commands.each do |key, klass| klass.usage(rows, key) end end |