Skip to content

commandDocumentation

Document Function

Writes a docstring for the selected function in your language and style: inputs, return value, errors raised and a working example.

npx workspacetemplates add document-function --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/document-function.md16 lines · 1.2 KBFile
---
description: "Writes a docstring for the selected function in your language and style: inputs, return value, errors raised and a working example."
argument-hint: "[function name or file:line]"
---

Write a docstring for the function: $ARGUMENTS (a name, or the code selected in the editor).

1. Find the function and read its full body. Then search for two or three callers to see how it is really used and what values are passed.
2. Detect the comment style from the surrounding files: JSDoc or TSDoc, Python Google or NumPy style, Javadoc, Rust `///`, Go doc comments. Copy that format exactly.
3. Write a first line that says what the function does in one imperative sentence, not its name repeated.
4. Document every parameter with its type, units and allowed range, and mention defaults and whether null or empty values are accepted.
5. Document the return value, including shape when it is an object, and side effects such as writes, network calls or mutation of arguments.
6. List each error or exception it can raise or return, and the condition that causes it. Read the body to find them; do not guess.
7. Add a short example that really works with the current signature.

Change comments only, never code. Show the new docstring and ask before inserting it.

What it does

A good docstring answers what a caller needs without reading the body: what goes in, what comes out, what can fail. This command reads the function and the code that calls it, then writes comments that say exactly that.

It matches the comment format already used in the file, whether JSDoc, Google-style Python or Rust doc comments, and it does not change any code.

Example

you › /document-function parseDuration

agent › Parses strings like "1h30m" into milliseconds. Params: input (string, required, units d h m s, case-insensitive). Returns number. Throws RangeError when the value exceeds 365 days. Example: parseDuration("2m") // 120000. Insert it above the function?

Questions

Does it change the function itself?
Never. Only the comment above the function is added or replaced.
How does it pick the format?
It copies the doc comment style found in the same file or its neighbors, so the result looks like your other docs.
  • Explain Error

    CommandDocumentation

    Free
  • Generate API Docs

    CommandDocumentation

    Free
  • Repository Code Tour

    CommandDocumentation

    Free
  • Update README

    CommandDocumentation

    Free