Skip to content

Commit 13a6efe

Browse files
committed
fix(api): custom commands with arguments
I had broken it because I didn't think it was supported. But it was :)
1 parent 0373797 commit 13a6efe

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

lua/opencode/api.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,10 @@ function M.get_slash_commands()
12311231
table.insert(result, {
12321232
slash_cmd = '/' .. name,
12331233
desc = def.description or 'User command',
1234-
fn = function()
1235-
M.run_user_command(name, {})
1234+
fn = function(...)
1235+
M.run_user_command(name, ...)
12361236
end,
1237-
args = false,
1237+
args = config_file.command_takes_arguments(def),
12381238
})
12391239
end
12401240
end

lua/opencode/config_file.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ function M.get_mcp_servers()
126126
return cfg and cfg.mcp or nil
127127
end
128128

129+
---Does this opencode user command take arguments?
130+
---@param command OpencodeCommand
131+
---@return boolean
132+
function M.command_takes_arguments(command)
133+
return command.template and command.template:find('$ARGUMENTS') ~= nil or false
134+
end
135+
129136
return M

tests/unit/api_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ describe('opencode.api', function()
404404
config_file.get_user_commands = function()
405405
return {
406406
['build'] = { description = 'Build the project' },
407-
['test'] = { description = 'Run tests' },
407+
['test'] = { description = 'Run tests', template = 'Run tests with $ARGUMENTS' },
408408
}
409409
end
410410

@@ -423,7 +423,7 @@ describe('opencode.api', function()
423423
test_found = true
424424
assert.equal('Run tests', cmd.desc)
425425
assert.is_function(cmd.fn)
426-
assert.falsy(cmd.args)
426+
assert.truthy(cmd.args)
427427
end
428428
end
429429

0 commit comments

Comments
 (0)