Class: ClaudeMemory::Commands::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/commands/base_command.rb

Overview

Base class for all CLI commands. Provides consistent interface for commands with I/O isolation for testing.

Examples:

Implementing a command

class MyCommand < BaseCommand
  def call(args)
    opts = parse_options(args, {verbose: false}) do |o|
      OptionParser.new do |parser|
        parser.on("-v", "--verbose") { o[:verbose] = true }
      end
    end
    return 1 if opts.nil?

    # ... command logic ...
    success("Done!")
  end
end

Testing a command

stdout = StringIO.new
stderr = StringIO.new
command = MyCommand.new(stdout: stdout, stderr: stderr)
exit_code = command.call(["--verbose"])
expect(exit_code).to eq(0)
expect(stdout.string).to include("Done!")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stderr: $stderr, stdin: $stdin) ⇒ BaseCommand

Returns a new instance of BaseCommand.

Parameters:

  • stdout (IO) (defaults to: $stdout)

    output stream (default: $stdout)

  • stderr (IO) (defaults to: $stderr)

    error stream (default: $stderr)

  • stdin (IO) (defaults to: $stdin)

    input stream (default: $stdin)



38
39
40
41
42
# File 'lib/claude_memory/commands/base_command.rb', line 38

def initialize(stdout: $stdout, stderr: $stderr, stdin: $stdin)
  @stdout = stdout
  @stderr = stderr
  @stdin = stdin
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



33
34
35
# File 'lib/claude_memory/commands/base_command.rb', line 33

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



33
34
35
# File 'lib/claude_memory/commands/base_command.rb', line 33

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



33
34
35
# File 'lib/claude_memory/commands/base_command.rb', line 33

def stdout
  @stdout
end

Instance Method Details

#call(args) ⇒ Integer

Execute the command with given arguments

Parameters:

  • args (Array<String>)

    command line arguments

Returns:

  • (Integer)

    exit code (0 for success, non-zero for failure)

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/claude_memory/commands/base_command.rb', line 47

def call(args)
  raise NotImplementedError, "Subclass must implement #call"
end