Class: RubyClaude::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_claude/command.rb

Overview

Pure translation of a Configuration plus per-call options into the argv array and child-environment overrides for the claude CLI.

Performs no I/O, which makes flag mapping trivial to unit-test. The prompt is intentionally never part of argv — it is written to the child’s stdin by the Runner to avoid ARG_MAX limits and shell-escaping concerns.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Command

Returns a new instance of Command.

Parameters:



12
13
14
# File 'lib/ruby_claude/command.rb', line 12

def initialize(config)
  @config = config
end

Instance Method Details

#build(stream:, resume: nil) ⇒ Array(Array<String>, Hash)

Build the argv array and child-environment overrides.

Parameters:

  • stream (Boolean)

    use stream-json output (also adds --verbose, which the CLI requires for stream-json in print mode)

  • resume (String, nil) (defaults to: nil)

    a session id to resume via --resume

Returns:

  • (Array(Array<String>, Hash))

    [argv, env]



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_claude/command.rb', line 22

def build(stream:, resume: nil)
  argv = [@config.binary, "-p", "--output-format", stream ? "stream-json" : "json"]
  argv << "--verbose" if stream
  add_flag(argv, "--model", @config.model)
  add_flag(argv, "--append-system-prompt", @config.append_system_prompt)
  add_list(argv, "--allowedTools", @config.allowed_tools)
  add_list(argv, "--disallowedTools", @config.disallowed_tools)
  add_list(argv, "--add-dir", @config.add_dirs)
  add_flag(argv, "--permission-mode", @config.permission_mode)
  add_flag(argv, "--max-turns", @config.max_turns&.to_s)
  add_flag(argv, "--resume", resume)
  [argv, child_env]
end

#child_envHash{String => String, nil}

Environment overrides for the child process. In subscription mode, ANTHROPIC_API_KEY is mapped to nil, which tells Open3/spawn to remove it from the inherited environment so the CLI falls back to the logged-in subscription credentials.

Returns:

  • (Hash{String => String, nil})


42
43
44
45
46
# File 'lib/ruby_claude/command.rb', line 42

def child_env
  return {} unless @config.use_subscription

  { "ANTHROPIC_API_KEY" => nil }
end