Class: CommandMapper::Gen::Command Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • command_name (String)

    The command name or path to the 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

#argumentsHash{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.

Returns:



29
30
31
# File 'lib/command_mapper/gen/command.rb', line 29

def arguments
  @arguments
end

#command_nameString

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.

Returns:

  • (String)


18
19
20
# File 'lib/command_mapper/gen/command.rb', line 18

def command_name
  @command_name
end

#optionsHash{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.

Returns:



26
27
28
# File 'lib/command_mapper/gen/command.rb', line 26

def options
  @options
end

#parent_commandCommand? (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.

Returns:



23
24
25
# File 'lib/command_mapper/gen/command.rb', line 23

def parent_command
  @parent_command
end

#subcommandsHash{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.

Returns:



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.

Parameters:

  • name (Symbol)
  • kwargs (Hash{Symbol => Object})


93
94
95
# File 'lib/command_mapper/gen/command.rb', line 93

def argument(name,**kwargs)
  @arguments[name] = Argument.new(name,**kwargs)
end

#class_nameString?

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.

Returns:

  • (String, nil)

    The class name or nil if #command_name is also nil.



116
117
118
# File 'lib/command_mapper/gen/command.rb', line 116

def class_name
  @command_name.split(/[_-]+/).map(&:capitalize).join
end

#command_stringString

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.

Returns:

  • (String)


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_pageString

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.

Returns:

  • (String)


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.

Parameters:

  • flag (String)
  • kwargs (Hash{Symbol => Object})


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.

Parameters:

  • path (String)


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.

Parameters:

  • name (String)

    The subcommand name.

Returns:

  • (Command)

    The newly defined subcommand.



106
107
108
# File 'lib/command_mapper/gen/command.rb', line 106

def subcommand(name)
  @subcommands[name] = Command.new(name,self)
end

#to_rubyString

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.

Returns:

  • (String)

    The generated ruby source code for the command.



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