Class: ShellOpts::Grammar::Command
- Defined in:
- lib/shellopts/grammar.rb,
lib/shellopts/dump.rb,
lib/shellopts/dump.rb,
lib/shellopts/parser.rb,
lib/shellopts/analyzer.rb,
lib/shellopts/renderer.rb,
lib/shellopts/formatter.rb
Overview
brief one-line commands should optionally use compact options
Direct Known Subclasses
Constant Summary collapse
- OPTIONS_ABBR =
"[OPTIONS]"
- COMMANDS_ABBR =
"[COMMANDS]"
- DESCRS_ABBR =
"ARGS..."
Constants inherited from Node
Instance Attribute Summary collapse
-
#brief ⇒ Object
Brief description of command.
-
#commands ⇒ Object
readonly
Array of sub-commands.
-
#description ⇒ Object
readonly
Description of command.
-
#descrs ⇒ Object
readonly
Array of ArgDescr objects.
-
#option_groups ⇒ Object
readonly
Array of option groups in declaration order.
-
#options ⇒ Object
readonly
Array of options in declaration order.
-
#specs ⇒ Object
readonly
Array of Arg objects.
Attributes inherited from IdrNode
#attr, #command, #ident, #name, #path
Attributes inherited from Node
Class Method Summary collapse
-
.command(obj) ⇒ Object
Shorthand to get the associated Grammar::Command object from a Program or a Grammar::Command object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Maps from any (sub-)path, name or identifier of an option or command (including the suffixed ‘!’) to the associated option.
- #collect_options ⇒ Object
-
#compute_command_hashes ⇒ Object
TODO Check for dash-collision.
- #compute_option_hashes ⇒ Object
- #dump_idr(short = false) ⇒ Object
- #dump_structure(device = $stdout) ⇒ Object
-
#initialize(parent, token) ⇒ Command
constructor
A new instance of Command.
- #key?(key) ⇒ Boolean
-
#keys ⇒ Object
Mostly for debug.
- #names(root: false) ⇒ Object
- #parse ⇒ Object
- #puts_brief ⇒ Object
- #puts_descr(prefix, brief: !self.brief.nil?,, name: :path) ⇒ Object
- #puts_help ⇒ Object
- #puts_usage(bol: false, max_width: Formatter::USAGE_MAX_WIDTH) ⇒ Object
-
#render(format, width, root: false, **opts) ⇒ Object
Format can be one of :abbr, :single, :enum, or :multi.
-
#render_structure ⇒ Object
Usable after parsing.
-
#reorder_options ⇒ Object
Move options before first command or before explicit COMMAND section.
Methods inherited from IdrNode
Methods inherited from Node
#analyzer_error, #ancestors, #dump_ast, #dump_attrs, #inspect, #parents, parse, #parser_error, #remove_arg_descr_nodes, #remove_arg_spec_nodes, #remove_brief_nodes, #traverse
Constructor Details
#initialize(parent, token) ⇒ Command
Returns a new instance of Command.
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/shellopts/grammar.rb', line 225 def initialize(parent, token) @brief = nil @default_brief = nil @description = [] @option_groups = [] @options = [] @options_hash = {} # Initialized by the analyzer @commands = [] @commands_hash = {} # Initialized by the analyzer @specs = [] @descrs = [] super end |
Instance Attribute Details
#brief ⇒ Object
Brief description of command
203 204 205 |
# File 'lib/shellopts/grammar.rb', line 203 def brief @brief end |
#commands ⇒ Object (readonly)
Array of sub-commands. Initialized by the parser but edited by the analyzer
217 218 219 |
# File 'lib/shellopts/grammar.rb', line 217 def commands @commands end |
#description ⇒ Object (readonly)
Description of command. Array of Paragraph or Code objects. Initialized by the parser
207 208 209 |
# File 'lib/shellopts/grammar.rb', line 207 def description @description end |
#descrs ⇒ Object (readonly)
Array of ArgDescr objects. Initialized by the parser
223 224 225 |
# File 'lib/shellopts/grammar.rb', line 223 def descrs @descrs end |
#option_groups ⇒ Object (readonly)
Array of option groups in declaration order. Initialized by the parser TODO: Rename ‘groups’
211 212 213 |
# File 'lib/shellopts/grammar.rb', line 211 def option_groups @option_groups end |
#options ⇒ Object (readonly)
Array of options in declaration order. Initialized by the analyzer
214 215 216 |
# File 'lib/shellopts/grammar.rb', line 214 def @options end |
#specs ⇒ Object (readonly)
Array of Arg objects. Initialized by the parser
220 221 222 |
# File 'lib/shellopts/grammar.rb', line 220 def specs @specs end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
Maps from any (sub-)path, name or identifier of an option or command (including the suffixed ‘!’) to the associated option. #[] and #key? can’t be used until after the analyze phase
242 243 244 245 246 247 248 249 250 |
# File 'lib/shellopts/grammar.rb', line 242 def [](key) case key when String; lookup(key.split(".")) when Symbol; lookup(key.to_s.gsub(".", "!.").split(".").map(&:to_sym)) when Array; lookup(key) else nil end end |
#collect_options ⇒ Object
23 24 25 |
# File 'lib/shellopts/analyzer.rb', line 23 def @options = option_groups.map(&:options).flatten end |
#compute_command_hashes ⇒ Object
TODO Check for dash-collision
54 55 56 57 58 59 60 61 62 |
# File 'lib/shellopts/analyzer.rb', line 54 def compute_command_hashes commands.each { |command| !@commands_hash.key?(command.name) or analyzer_error command.token, "Duplicate command name: #{command.name}" @commands_hash[command.name] = command @commands_hash[command.ident] = command command.compute_command_hashes } end |
#compute_option_hashes ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/shellopts/analyzer.rb', line 40 def compute_option_hashes .each { |option| option.idents.zip(option.names).each { |ident, name| !@options_hash.key?(name) or analyzer_error option.token, "Duplicate option name: #{name}" @options_hash[name] = option !@options_hash.key?(ident) or analyzer_error option.token, "Can't use both #{@options_hash[ident].name} and #{name}" @options_hash[ident] = option } } end |
#dump_idr(short = false) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/shellopts/dump.rb', line 102 def dump_idr(short = false) if short puts name indent { .each { |option| option.dump_idr(short) } commands.each { |command| command.dump_idr(short) } descrs.each { |descr| descr.dump_idr(short) } } else puts "#{name}: #{classname}" dump_attrs :uid, :path, :ident, :name, :options, :commands, :specs, :descrs, :brief end end |
#dump_structure(device = $stdout) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/shellopts/dump.rb', line 22 def dump_structure(device = $stdout) device.puts ident device.indent { |dev| option_groups.each { |group| dev.puts group..map(&:name).join(" ") } commands.each { |command| command.dump_structure(dev) } descrs.each { |descr| dev.puts descr.text } } end |
#key?(key) ⇒ Boolean
252 |
# File 'lib/shellopts/grammar.rb', line 252 def key?(key) !self.[](key).nil? end |
#keys ⇒ Object
Mostly for debug. Has questional semantics because it only lists local keys
255 |
# File 'lib/shellopts/grammar.rb', line 255 def keys() @options_hash.keys + @commands_hash.keys end |
#names(root: false) ⇒ Object
109 110 111 |
# File 'lib/shellopts/renderer.rb', line 109 def names(root: false) (root ? ancestors : []) + [self] end |
#parse ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/shellopts/parser.rb', line 109 def parse if parent path_names = token.source.sub("!", "").split(".") set_name( path_names.last, path_names.map { |cmd| "#{cmd}!".to_sym }) else set_name(token.source, []) end super end |
#puts_brief ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/shellopts/formatter.rb', line 49 def puts_brief width = Formatter.rest option_briefs = option_groups.map { |group| [group.render(:enum), group.brief&.words] } command_briefs = commands.map { |command| [command.render(:single, width), command.brief&.words] } widths = Formatter::compute_columns(width, option_briefs + command_briefs) if brief puts brief puts end puts "Usage" indent { puts_usage(bol: true, max_width: Formatter::HELP_MAX_WIDTH) } if .any? puts puts "Options" indent { Formatter::puts_columns(widths, option_briefs) } end if commands.any? puts puts "Commands" indent { Formatter::puts_columns(widths, command_briefs) } end end |
#puts_descr(prefix, brief: !self.brief.nil?,, name: :path) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/shellopts/formatter.rb', line 76 def puts_descr(prefix, brief: !self.brief.nil?, name: :path) # Use one-line mode if all options are declared on one line if .all? { |option| option.token.lineno == token.lineno } puts Ansi.bold([prefix, render(:single, Formatter.rest)].flatten.compact.join(" ")) = false else puts Ansi.bold([prefix, render(:abbr, Formatter.rest)].flatten.compact.join(" ")) = true end indent { if brief puts self.brief.words.wrap(Formatter.rest) else newline = false children.each { |child| puts if newline newline = true if child.is_a?(Command) child.puts_descr(prefix, name: :path) elsif child.is_a?(OptionGroup) child.puts_descr if newline = false else child.puts_descr end } end } end |
#puts_help ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/shellopts/formatter.rb', line 108 def puts_help puts Ansi.bold "NAME" full_name = [Formatter::command_prefix, name].join indent { puts brief ? "#{full_name} - #{brief}" : full_name } puts puts Ansi.bold "USAGE" indent { puts_usage(bol: true, max_width: Formatter::HELP_MAX_WIDTH) } section = { Paragraph => "DESCRIPTION", OptionGroup => "OPTION", Command => "COMMAND" } seen_sections = {} newline = false # True if a newline should be printed before child indent { children.each { |child| klass = child.is_a?(Section) ? section.key(child.name) : child.class if s = section[klass] # Implicit section section.delete(klass) section.delete(Paragraph) if klass <= OptionGroup s += "S" if .size > 1 elsif klass <= Command s += "S" if commands.size > 1 || commands.size == 1 && commands.first.commands.size > 1 end puts indent(-1).puts Ansi.bold s newline = false next if child.is_a?(Section) else # Any other node adds a newline puts if newline newline = true end if child.is_a?(Command) prefix = child.path[path.size..-2].map { |sym| sym.to_s.sub(/!/, "") } child.puts_descr(prefix, brief: false, name: :path) newline = true else child.puts_descr newline = true end } # Also emit commands not declared in nested scope (commands - children.select { |child| child.is_a?(Command) }).each { |cmd| next if cmd.parent.nil? # Skip implicit commands puts if newline newline = true prefix = cmd.command == self ? nil : cmd.command&.name cmd.puts_descr(prefix, brief: false, name: path) } } end |
#puts_usage(bol: false, max_width: Formatter::USAGE_MAX_WIDTH) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/shellopts/formatter.rb', line 33 def puts_usage(bol: false, max_width: Formatter::USAGE_MAX_WIDTH) width = [Formatter.rest, max_width].min if descrs.size == 0 print (lead = Formatter.command_prefix || "") indent(lead.size, ' ', bol: bol && lead == "") { puts render(:multi, width) } else lead = Formatter.command_prefix || "" descrs.each { |descr| print lead puts render(:multi, width, args: descr.text.split(' ')) } end end |
#render(format, width, root: false, **opts) ⇒ Object
Format can be one of :abbr, :single, :enum, or :multi. :abbr lists the command on one line with options abbreviated. :single force one-line output and compacts options and commands if needed. :enum outputs a :single line for each argument specification/description, :multi tries one-line output but wrap options if needed. Multiple argument specifications/descriptions are always compacted
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/shellopts/renderer.rb', line 98 def render(format, width, root: false, **opts) case format when :abbr; render_abbr when :single; render_single(width, **opts) when :enum; render_enum(width, **opts) when :multi; render_multi(width, **opts) else raise ArgumentError, "Illegal format: #{format.inspect}" end end |
#render_structure ⇒ Object
Usable after parsing
16 17 18 19 20 |
# File 'lib/shellopts/dump.rb', line 16 def render_structure io = StringIO.new dump_structure(io) io.string end |
#reorder_options ⇒ Object
Move options before first command or before explicit COMMAND section
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/shellopts/analyzer.rb', line 28 def if commands.any? i = children.find_index { |child| child.is_a?(Command) || child.is_a?(Section) && child.name == "COMMAND" } if i , rest = children[i+1..-1].partition { |child| child.is_a?(OptionGroup) } @children = children[0, i] + + children[i..i] + rest end end end |