Class: SpecForge::CLI::Command

Inherits:
Object
  • Object
show all
Includes:
Actions
Defined in:
lib/spec_forge/cli/command.rb

Overview

Base class for CLI commands that provides common functionality and defines the DSL for declaring command properties.

Examples:

Defining a simple command

class MyCommand < Command
  command_name "my_command"
  syntax "my_command [options]"
  summary "Does something awesome"
  description "A longer description of what this command does"

  option "-f", "--force", "Force the operation"

  def call
    # Command implementation
  end
end

Direct Known Subclasses

Docs, Init, New, Run, Serve

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

included

Constructor Details

#initialize(arguments, options) ⇒ Command

Creates a new command instance

Parameters:

  • arguments (Array)

    Any positional arguments from the command line

  • options (Hash)

    Any flag arguments from the command line



175
176
177
178
# File 'lib/spec_forge/cli/command.rb', line 175

def initialize(arguments, options)
  @arguments = arguments
  @options = options
end

Class Attribute Details

.command_name(name) ⇒ Object

Sets the command's name

Parameters:

  • name (String)

    The name of the command



57
58
59
# File 'lib/spec_forge/cli/command.rb', line 57

def command_name(name)
  self.command_name = name
end

.description(description) ⇒ Object

Sets the command's description, displayed in detailed help

Parameters:

  • description (String)

    The detailed command description



75
76
77
# File 'lib/spec_forge/cli/command.rb', line 75

def description(description)
  self.description = description
end

.options=(value) ⇒ Object (writeonly)

Sets the command's available options



50
51
52
# File 'lib/spec_forge/cli/command.rb', line 50

def options=(value)
  @options = value
end

.summary(summary) ⇒ Object

Sets the command's summary, displayed in command list

Parameters:

  • summary (String)

    The short command summary



84
85
86
# File 'lib/spec_forge/cli/command.rb', line 84

def summary(summary)
  self.summary = summary
end

.syntax(syntax) ⇒ Object

Sets the command's syntax

Parameters:

  • syntax (String)

    The command syntax to display in help



66
67
68
# File 'lib/spec_forge/cli/command.rb', line 66

def syntax(syntax)
  self.syntax = syntax
end

Instance Attribute Details

#argumentsArray (readonly)

Command arguments passed from the command line

Returns:

  • (Array)

    The positional arguments



158
159
160
# File 'lib/spec_forge/cli/command.rb', line 158

def arguments
  @arguments
end

#optionsHash (readonly)

Command options passed from the command line

Returns:

  • (Hash)

    The flag arguments



165
166
167
# File 'lib/spec_forge/cli/command.rb', line 165

def options
  @options
end

Class Method Details

.aliases(*aliases) ⇒ Object

Adds command aliases

Parameters:

  • aliases (Array<String>)

    Alias names for this command



118
119
120
121
122
# File 'lib/spec_forge/cli/command.rb', line 118

def aliases(*aliases)
  @aliases ||= []

  @aliases += aliases
end

.example(command, description) ⇒ Object

Adds an example of how to use the command

Parameters:

  • command (String)

    The example command

  • description (String)

    Description of what the example does



94
95
96
97
98
99
# File 'lib/spec_forge/cli/command.rb', line 94

def example(command, description)
  @examples ||= []

  # Commander wants it backwards
  @examples << [description, command]
end

.option(*args) {|value| ... } ⇒ Object

Adds a command line option

Parameters:

  • args (Array<String>)

    The option flags (e.g., "-f", "--force")

Yields:

  • (value)

    Block to handle the option value



107
108
109
110
111
# File 'lib/spec_forge/cli/command.rb', line 107

def option(*args, &block)
  @options ||= []

  @options << [args, block]
end

.register(context) ⇒ Object

Registers the command with Commander

Parameters:

  • context (Commander::Command)

    The Commander context



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/spec_forge/cli/command.rb', line 131

def register(context)
  raise "Missing command name" if @command_name.nil?

  context.command(@command_name) do |c|
    c.syntax = @syntax
    c.summary = @summary
    c.description = @description
    c.examples = @examples if @examples

    @options&.each do |opts, block|
      c.option(*opts, &block)
    end

    c.action { |args, opts| new(args, opts).call }
  end

  @aliases&.each do |alii|
    context.alias_command(alii, @command_name)
  end
end