Appendix G — Script Entry Points and Exit Behaviour Answers
Completed Check
Keep parser setup, runtime behaviour, and script startup separate. Returning integers from main() gives the caller a clear success or failure signal, and the __name__ guard lets the file behave differently when it’s imported versus run directly.
import argparseimport errnofrom pathlib import Pathdef build_argument_parser(): parser = argparse.ArgumentParser(prog="bernauer") parser.add_argument("domain", help="domain to test, e.g. example.test") parser.add_argument("-w", "--wordlist", default="data/wordlist.txt")return parserdef main(args):ifnot Path(args.wordlist).is_file():print("No such file or directory")return errno.ENOENTprint(f"Ready to scan {args.domain}")print(f"Wordlist: {args.wordlist}")return0parser = build_argument_parser()print(main(parser.parse_args(["example.test"])))print(main(parser.parse_args(["example.test", "--wordlist", "missing.txt"])))
Ready to scan example.test
Wordlist: data/wordlist.txt
0
No such file or directory
2
In a real script, the final startup line would be raise SystemExit(main(build_argument_parser().parse_args())). This notebook simulates arguments explicitly so rendering the answer doesn’t accidentally parse Jupyter’s own command-line options.