Skip to content

Lesson 7 of 9 · 20m

Building CLI Tools Analysts Actually Use

A security CLI succeeds when it behaves like a Unix citizen: composable input and output, clear errors, useful defaults, and no mystery state.

Most internal security tools die as one clever person's script. The command has twelve positional arguments, writes output to a hard-coded path, and requires a tribal incantation to run. A tool that sticks is boring in the best way: predictable input, predictable output, and help visible at the moment of need.

Design the interface before the implementation

Write the help text first. It forces you to decide what the tool is for and what it refuses to do.

usage: secscan import [--source tenable|nmap] [--since ISO8601] FILE

Import scanner output into the findings database.

positional arguments:
  FILE                  scanner export path, or '-' for stdin

options:
  --source              explicit source type; otherwise inferred
  --dry-run             parse and summarize without writing
  --json                machine-readable summary

That interface already encodes operational choices: streaming input, explicit boundaries, dry-run mode, and machine output.

Use argparse, not positional folklore

The standard library argparse is enough for most internal tools. It gives validation, help, and errors without adding a dependency.

import argparse

def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="secscan")
    parser.add_argument("file", help="scanner export path, or '-' for stdin")
    parser.add_argument("--source", choices=["tenable", "nmap", "qualys"])
    parser.add_argument("--since")
    parser.add_argument("--dry-run", action="store_true")
    parser.add_argument("--json", action="store_true")
    return parser

Do not make the fourth positional argument mean "environment." Nobody will remember, and mistyping it will eventually target production.

stdout is data, stderr is explanation

This rule makes scripts composable. A tool that prints progress chatter into stdout cannot be safely piped into jq, sort, or another automation step.

secscan import scan.xml --json | jq '.created, .updated'

Use exit codes honestly: zero for success, nonzero for failure. Default to read-only behavior, limited scope, and conservative concurrency; destructive or broad actions should require explicit flags such as --apply or --all-environments.

Field notes

  • Support --help even for tiny tools; the day you need it is not the day you want to remember implementation details.
  • Machine-readable output should be stable; humans tolerate renamed columns, pipelines do not.
  • Accept stdin where practical so small tools compose into larger workflows.
  • Include --version once a tool touches shared systems.
  • If analysts copy the script into their home directory, you built a script; if they automate around it, you built a tool.

Was this lesson helpful?

Sign in to rate