Skip to content

Commit e7e1c09

Browse files
committed
feat: add command argument support to ClaudeCode terminal commands
- Extended :ClaudeCode and :ClaudeCodeOpen commands to accept arguments - Arguments are appended to the configured terminal_cmd when launching Claude - Updated terminal module to support cmd_args parameter in open() and toggle() - Added comprehensive test coverage for argument handling and edge cases - Maintained full backward compatibility with existing usage Usage examples: - :ClaudeCode --resume - :ClaudeCodeOpen --help --verbose - :ClaudeCode (works as before with no arguments) Change-Id: I5ff2867e884f234a6b4fbaac065a39aa59cbf533 Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent 6f6b8d1 commit e7e1c09

File tree

5 files changed

+609
-17
lines changed

5 files changed

+609
-17
lines changed

lua/claudecode/init.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,22 +648,24 @@ function M._create_commands()
648648

649649
local terminal_ok, terminal = pcall(require, "claudecode.terminal")
650650
if terminal_ok then
651-
vim.api.nvim_create_user_command("ClaudeCode", function(_opts)
651+
vim.api.nvim_create_user_command("ClaudeCode", function(opts)
652652
local current_mode = vim.fn.mode()
653653
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
654654
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
655655
end
656-
terminal.toggle({})
656+
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
657+
terminal.toggle({}, cmd_args)
657658
end, {
658-
nargs = "?",
659-
desc = "Toggle the Claude Code terminal window",
659+
nargs = "*",
660+
desc = "Toggle the Claude Code terminal window with optional arguments",
660661
})
661662

662-
vim.api.nvim_create_user_command("ClaudeCodeOpen", function(_opts)
663-
terminal.open({})
663+
vim.api.nvim_create_user_command("ClaudeCodeOpen", function(opts)
664+
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
665+
terminal.open({}, cmd_args)
664666
end, {
665-
nargs = "?",
666-
desc = "Open the Claude Code terminal window",
667+
nargs = "*",
668+
desc = "Open the Claude Code terminal window with optional arguments",
667669
})
668670

669671
vim.api.nvim_create_user_command("ClaudeCodeClose", function()

lua/claudecode/terminal.lua

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@ local managed_fallback_terminal_jobid = nil
3434
local native_term_tip_shown = false
3535

3636
-- Uses the `terminal_cmd` from the module's configuration, or defaults to "claude".
37+
-- @param cmd_args string|nil Optional arguments to append to the command
3738
-- @return string The command to execute.
38-
local function get_claude_command()
39+
local function get_claude_command(cmd_args)
3940
local cmd_from_config = term_module_config.terminal_cmd
41+
local base_cmd
4042
if not cmd_from_config or cmd_from_config == "" then
41-
return "claude" -- Default if not configured
43+
base_cmd = "claude" -- Default if not configured
44+
else
45+
base_cmd = cmd_from_config
46+
end
47+
48+
if cmd_args and cmd_args ~= "" then
49+
return base_cmd .. " " .. cmd_args
4250
end
43-
return cmd_from_config
51+
return base_cmd
4452
end
4553

4654
--- Configures the terminal module.
@@ -318,10 +326,11 @@ local function build_snacks_opts(effective_term_config_for_snacks, env_table)
318326
end
319327

320328
--- Gets the base claude command string and necessary environment variables.
329+
-- @param cmd_args string|nil Optional arguments to append to the command
321330
-- @return string|nil cmd_string The command string, or nil on failure.
322331
-- @return table|nil env_table The environment variables table, or nil on failure.
323-
local function get_claude_command_and_env()
324-
local cmd_string = get_claude_command()
332+
local function get_claude_command_and_env(cmd_args)
333+
local cmd_string = get_claude_command(cmd_args)
325334
if not cmd_string or cmd_string == "" then
326335
vim.notify("Claude terminal base command cannot be determined.", vim.log.levels.ERROR)
327336
return nil, nil
@@ -374,10 +383,11 @@ end
374383

375384
--- Opens or focuses the Claude terminal.
376385
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
377-
function M.open(opts_override)
386+
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
387+
function M.open(opts_override, cmd_args)
378388
local provider = get_effective_terminal_provider()
379389
local effective_config = build_effective_term_config(opts_override)
380-
local cmd_string, claude_env_table = get_claude_command_and_env()
390+
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
381391

382392
if not cmd_string then
383393
-- Error already notified by the helper function
@@ -447,10 +457,11 @@ end
447457

448458
--- Toggles the Claude terminal open or closed.
449459
-- @param opts_override table (optional) Overrides for terminal appearance (split_side, split_width_percentage).
450-
function M.toggle(opts_override)
460+
-- @param cmd_args string|nil (optional) Arguments to append to the claude command.
461+
function M.toggle(opts_override, cmd_args)
451462
local provider = get_effective_terminal_provider()
452463
local effective_config = build_effective_term_config(opts_override)
453-
local cmd_string, claude_env_table = get_claude_command_and_env()
464+
local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args)
454465

455466
if not cmd_string then
456467
return -- Error already notified

0 commit comments

Comments
 (0)