04 / PACKAGE TESTED TOOLS · LESSON 13

Turn command arguments into a reviewed plan

Parse explicit options without mixing command-line handling with network execution.

15 min · learn at your pace

Parse at the edge of the program

Command-line arguments arrive as strings. argparse handles repeated options, numeric conversion and unknown flags, but domain rules still belong to the program. Parse arguments, validate the timeout and reuse scan_plan for scope, port and endpoint limits. Return structured options rather than opening connections inside the parser.

The caller supplies argv as a list, making the function easy to test in the browser and independent of the host process's sys.argv. Repeated --target, --port and --allow options build lists. The planning layer then normalizes and deduplicates them consistently with the earlier lessons.

Make execution an explicit choice

The execute flag defaults to False. Printing a plan and running a plan are different operations, so the caller must make that transition visible. The optional local scanner uses --execute to request real connections; this lesson only returns the parsed decision and never performs network work.

Normal argparse applications often report an error by terminating the process. A reusable library function should instead raise ValueError so the caller can display a message or recover. This teaching parser disables built-in help and overrides error; the standalone download retains conventional --help at its application boundary.

Do not split a shell command string yourself. Passing an argument list keeps each value explicit and avoids introducing shell evaluation into a configuration task. No account secrets or credentials are needed for this scanner's options.

Try an example.py
import argparse
p = argparse.ArgumentParser(add_help=False)
p.add_argument("--port", action="append", type=int)
print(p.parse_args(["--port", "443", "--port", "80"]).port)
YOUR TURN

Put it into practice.

Define parse_options(argv). Parse required repeatable --target, --port (integer), --allow; optional --timeout-ms (default 500, 1..5000), --max-endpoints (default 64, scan_plan limits) and boolean --execute (default False). Reuse supplied scan_rules.scan_plan. Return plan, timeout_ms, execute. Reject malformed, missing, unknown or invalid domain arguments with ValueError rather than exiting. This teaching parser has no --help option and never probes. argv is an argument list, not a shell command string.

See an explained solution
One way to solve it.py
import argparse
from scan_rules import scan_plan

def parse_options(argv):
    class Parser(argparse.ArgumentParser):
        def error(self,message):raise ValueError(message)
    parser=Parser(add_help=False,exit_on_error=False)
    parser.add_argument("--target",action="append",required=True)
    parser.add_argument("--port",action="append",type=int,required=True)
    parser.add_argument("--allow",action="append",required=True)
    parser.add_argument("--timeout-ms",type=int,default=500)
    parser.add_argument("--max-endpoints",type=int,default=64)
    parser.add_argument("--execute",action="store_true")
    try:
        options=parser.parse_args(argv)
    except argparse.ArgumentError as error:
        raise ValueError(str(error)) from error
    if not 1<=options.timeout_ms<=5000:raise ValueError("Invalid timeout")
    plan=scan_plan(options.target,options.port,options.allow,options.max_endpoints)
    return {"plan":plan,"timeout_ms":options.timeout_ms,"execute":options.execute}

if __name__ == "__main__":
    print(parse_options(["--target","192.0.2.10","--port","443","--allow","192.0.2.0/24"]))

The parser returns an explicit decision without performing it. Reusing the plan function keeps command-line and browser scope rules aligned.