DSK-25Recommended
Show a dry run and ask before destructive commands
Give every command that deletes, overwrites or sends something a --dry-run flag that lists what it would do. Ask for confirmation before it acts, unless --yes is passed.
How to do it
Agents and tired people run commands they meant to check first. A dry run shows the exact list of records, files or messages, with a count. Make the dry run use the same code path as the action, so the list matches what happens. Without a terminal, the confirmation follows never wait for input when no person is there. Infrastructure tools follow the same idea with a plan, as review the plan and apply only from CI says.
Examples
Python example
def delete_drafts(older_than_days: int, dry_run: bool, assume_yes: bool) -> None:
drafts = invoices.drafts_older_than(older_than_days)
for draft in drafts:
print(f"would delete {draft.number}" if dry_run else f"deleting {draft.number}", file=sys.stderr)
if dry_run or not confirm(f"Delete {len(drafts)} drafts?", assume_yes):
return
invoices.delete_many([d.id for d in drafts])
Go example
func deleteDrafts(olderThanDays int, dryRun, assumeYes bool) error {
drafts := invoices.DraftsOlderThan(olderThanDays)
for _, d := range drafts {
fmt.Fprintf(os.Stderr, "would delete %s\n", d.Number)
}
if dryRun || !confirm(fmt.Sprintf("Delete %d drafts?", len(drafts)), assumeYes) {
return nil
}
return invoices.DeleteMany(ids(drafts))
}
How to check
A test runs each destructive command with --dry-run on a test copy, and nothing changes. The list it printed matches what the same command then changes with --yes.
Related rules
- DSK-23 Never wait for input when no person is there in Desktop apps, extensions and CLIs
- INF-3 Review the plan and apply only from CI in Infrastructure and environments
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/desktop-apps/command-line-tools/dry-run">Agent Wranglers desktop apps, extensions and CLIs playbook</a>. In code you write, add a comment next to it with the same link.