Class: CommandMapper::Gen::Command Private
- Inherits:
-
Object
- Object
- CommandMapper::Gen::Command
- Defined in:
- lib/command_mapper/gen/command.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents a mock CommandMapper::Command
class that will be populated
by the Parsers and written out to a file.
Constant Summary collapse
- COMMAND_DOC_URL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
URL to the documentation for
CommandMapper::Command
. "https://rubydoc.info/gems/command_mapper/CommandMapper/Command"
Instance Attribute Summary collapse
- #arguments ⇒ Hash{Symbol => Argument} readonly private
-
#command_name ⇒ String
private
The command's name.
- #options ⇒ Hash{String => Option} readonly private
-
#parent_command ⇒ Command?
readonly
private
The parent command of this sub-command.
- #subcommands ⇒ Hash{String => Command} readonly private
Instance Method Summary collapse
-
#argument(name, **kwargs) ⇒ Object
private
Defines an argument for the command.
-
#class_name ⇒ String?
private
The CamelCase class name derived from the #command_name.
-
#command_string ⇒ String
private
The command string to run the command.
-
#initialize(command_name, parent_command = nil) ⇒ Command
constructor
private
Initializes the parsed command.
-
#man_page ⇒ String
private
The man-page name for the command.
-
#option(flag, **kwargs) ⇒ Object
private
Defines an option for the command.
-
#save(path) ⇒ Object
private
Saves the parsed command to the given file path.
-
#subcommand(name) ⇒ Command
private
Defines a new sub-command.
-
#to_ruby ⇒ String
private
Converts the parsed command to Ruby source code.
Constructor Details
#initialize(command_name, parent_command = nil) ⇒ Command
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the parsed command.
40 41 42 43 44 45 46 47 |
# File 'lib/command_mapper/gen/command.rb', line 40 def initialize(command_name,parent_command=nil) @command_name = command_name @parent_command = parent_command @options = {} @arguments = {} @subcommands = {} end |
Instance Attribute Details
#arguments ⇒ Hash{Symbol => Argument} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 |
# File 'lib/command_mapper/gen/command.rb', line 29 def arguments @arguments end |
#command_name ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The command's name.
18 19 20 |
# File 'lib/command_mapper/gen/command.rb', line 18 def command_name @command_name end |
#options ⇒ Hash{String => Option} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
26 27 28 |
# File 'lib/command_mapper/gen/command.rb', line 26 def @options end |
#parent_command ⇒ Command? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The parent command of this sub-command.
23 24 25 |
# File 'lib/command_mapper/gen/command.rb', line 23 def parent_command @parent_command end |
#subcommands ⇒ Hash{String => Command} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/command_mapper/gen/command.rb', line 32 def subcommands @subcommands end |
Instance Method Details
#argument(name, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines an argument for the command.
93 94 95 |
# File 'lib/command_mapper/gen/command.rb', line 93 def argument(name,**kwargs) @arguments[name] = Argument.new(name,**kwargs) end |
#class_name ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The CamelCase class name derived from the #command_name.
116 117 118 |
# File 'lib/command_mapper/gen/command.rb', line 116 def class_name @command_name.split(/[_-]+/).map(&:capitalize).join end |
#command_string ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The command string to run the command.
54 55 56 57 58 59 60 |
# File 'lib/command_mapper/gen/command.rb', line 54 def command_string if @parent_command "#{@parent_command.command_string} #{@command_name}" else @command_name end end |
#man_page ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The man-page name for the command.
67 68 69 70 71 72 73 |
# File 'lib/command_mapper/gen/command.rb', line 67 def man_page if @parent_command "#{@parent_command.man_page}-#{@command_name}" else @command_name end end |
#option(flag, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines an option for the command.
82 83 84 |
# File 'lib/command_mapper/gen/command.rb', line 82 def option(flag,**kwargs) @options[flag] = Option.new(flag,**kwargs) end |
#save(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Saves the parsed command to the given file path.
201 202 203 |
# File 'lib/command_mapper/gen/command.rb', line 201 def save(path) File.write(path,to_ruby) end |
#subcommand(name) ⇒ Command
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines a new sub-command.
106 107 108 |
# File 'lib/command_mapper/gen/command.rb', line 106 def subcommand(name) @subcommands[name] = Command.new(name,self) end |
#to_ruby ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts the parsed command to Ruby source code.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/command_mapper/gen/command.rb', line 129 def to_ruby lines = [] if @parent_command.nil? lines << "require 'command_mapper/command'" lines << "" lines << "#" lines << "# Represents the `#{@command_name}` command" lines << "#" lines << "# @see #{COMMAND_DOC_URL}" lines << "#" lines << "class #{class_name} < CommandMapper::Command" lines << "" lines << " command #{@command_name.inspect} do" indent = " " else lines << "subcommand #{@command_name.inspect} do" indent = " " end unless @options.empty? @options.each_value do |option| lines << "#{indent}#{option.to_ruby}" end end if (!@options.empty? && !@arguments.empty?) lines << '' end unless @arguments.empty? @arguments.each_value do |argument| lines << "#{indent}#{argument.to_ruby}" end end unless @subcommands.empty? if (!@options.empty? || !@arguments.empty?) lines << '' end @subcommands.each_value.each_with_index do |subcommand,index| lines << '' if index > 0 subcommand.to_ruby.each_line do |line| if line == $/ lines << '' else lines << "#{indent}#{line.chomp}" end end end end if @parent_command.nil? lines << " end" lines << '' end lines << "end" return lines.join($/) + $/ end |