how do you create a custom command in claude code that accepts runtime parameters?
How to Create a Custom Command in Claude Code with Runtime Parameters
Key Takeaways
- Custom commands in Claude Code are defined within the
.claudecode.jsonconfiguration file. - To accept runtime parameters, you use positional arguments or environment variables within the configuration schema.
- These commands allow you to automate repetitive developer workflows like linting, testing, or specific build scripts.
To create a custom command that accepts runtime parameters in Claude Code, you must define a new entry in your project’s .claudecode.json file. By using placeholder syntax or shell-scripting logic within the command string, you can pass arguments dynamically when executing the command from the Claude Code interface.
Table of Contents
- Defining Commands in .claudecode.json
- Passing Runtime Parameters
- Comparison of Command Types
- Summary Table
- Frequently Asked Questions
Defining Commands in .claudecode.json
Claude Code looks for a configuration file in your project root to understand project-specific capabilities. You define custom commands under the commands key. Each command requires a name, a description (so Claude knows when to use it), and the actual shell command to execute.
Pro Tip: Always provide a detailed description. Claude uses this description to map your natural language requests to the specific custom command.
Passing Runtime Parameters
There are two primary ways to handle dynamic input in your custom commands:
1. Shell Argument Passing
Since Claude Code executes these commands in your local shell environment, you can write scripts that expect arguments.
Example .claudecode.json:
{
"commands": {
"analyze-file": {
"description": "Analyzes a specific file for security vulnerabilities. Requires a file path.",
"command": "python3 scripts/analyze.py $1"
}
}
}
When you tell Claude: “Analyze the file src/auth.py”, Claude interprets the file path as the first argument ($1) and executes: python3 scripts/analyze.py src/auth.py.
2. Environment Variables
You can also structure commands to use environment variables if your runtime parameters are meant to configure the behavior of a tool (like setting a NODE_ENV).
Comparison of Command Types
| Feature | Static Command | Parameterized Command |
|---|---|---|
| Complexity | Low | Moderate |
| Flexibility | Fixed output (e.g., npm test) |
Dynamic (e.g., npm test -- [file]) |
| Use Case | General CI/CD tasks | Targeted debugging or specific builds |
| Claude’s Role | Triggering execution | Mapping intent to arguments |
Summary Table
| Component | Description |
|---|---|
| Config File | .claudecode.json located in the root directory. |
| Key Syntax | "command": "your-script.sh $1 $2" for positional params. |
| Description | Vital for Claude’s “agentic” ability to choose the command. |
| Execution | Triggered via natural language within the Claude Code CLI. |
Frequently Asked Questions
1. Can I use multiple parameters in one command?
Yes. You can define commands like gcc $1 -o $2. Claude will attempt to extract both the source file and the output name from your natural language prompt.
2. What happens if I don’t provide a required parameter?
Claude will typically realize the command is missing information based on the shell requirement and ask you: “Which file should I use for this command?” before executing.
3. Does this work with global Claude Code config?
Currently, custom commands are most effective when defined per-project in the local root, ensuring the environment and paths are correct.
Next Steps
Shall I help you draft a specific .claudecode.json configuration for a particular tool or language you are currently using?