This answer works because it declares the required target first, then adds the optional wordlist flag. argparse turns those command-line values into attributes on the parsed args object.
The charm is that you’re not rolling your own parser over sys.argv. You describe the command-line shape once, and argparse handles parsing, basic usage output, and invalid-argument errors for you.
import argparseclass Args:""" Simulate command-line arguments in Jupyter notebooks. """ target ="https://example.com" wordlist ="wordlist.txt"def is_running_in_jupyter():""" Check whether the code is running in a Jupyter notebook. Returns: bool: True when running in a Jupyter notebook, False otherwise. """try:from IPython import get_ipythonif'IPKernelApp'in get_ipython().config:returnTrueexcept:passreturnFalsedef build_argument_parser():""" Create an argument parser for the command-line interface. This sets up an argument parser using the `argparse` module. It defines two arguments: 1. `target`: A positional argument that represents the domain or IP address to scan. - The `help` parameter should provide a relevant description of what this argument is for. 2. `-w`, `--wordlist`: An optional argument that specifies the path to a wordlist file for the scan. - The `help` parameter should provide a relevant description of what this argument is for. Returns: argparse.ArgumentParser: The configured argument parser. """ parser = argparse.ArgumentParser() parser.add_argument('target',help="The fixture-backed domain to scan. Example: example.test." ) parser.add_argument('-w', '--wordlist',help='Path to a wordlist file for the scan.' )return parserif__name__=='__main__': parser = build_argument_parser()if is_running_in_jupyter(): args = Args()else: args = parser.parse_args()print(args.target)if args.wordlist:print(args.wordlist)
1
Set up an argument parser using the argparse module.
2
Add one positional argument (host), with appropriate help text.
3
Add one optional argument (-w, --wordlist), with appropriate help text.
4
Return the argument parser for the rest of the script to use.
https://example.com
wordlist.txt
Here’s what’s happening: the parser is the command centre, and the returned object is the doorway the rest of the application uses to access user choices.
Extension Activity
The extended answer adds a description and RawTextHelpFormatter so the generated --help output can include readable multi-line text.
If you’re the decorating and/or customisation type, this is where you can add a sweet little banner or version string. Just remember that a help message is for the tired person at the terminal first, and for your aesthetic ambitions second.
import argparseclass Args:""" Simulate command-line arguments in Jupyter notebooks. """ target ="https://example.com" wordlist ="wordlist.txt"def is_running_in_jupyter():""" Check whether the code is running in a Jupyter notebook. Returns: bool: True when running in a Jupyter notebook, False otherwise. """try:from IPython import get_ipythonif'IPKernelApp'in get_ipython().config:returnTrueexcept:passreturnFalsedef build_argument_parser():""" Create an argument parser for the command-line interface. This sets up an argument parser using the `argparse` module. It defines two arguments: 1. `target`: A positional argument that represents the domain or IP address to scan. - The `help` parameter should provide a relevant description of what this argument is for. 2. `-w`, `--wordlist`: An optional argument that specifies the path to a wordlist file for the scan. - The `help` parameter should provide a relevant description of what this argument is for. Returns: argparse.ArgumentParser: The configured argument parser. """ parser = argparse.ArgumentParser( description=f""" _ | |_ ___ ___ ___ ___ _ _ ___ ___ | . | -_| _| | .'| | | -_| _||___|___|_| |_|_|__,|___|___|_| Version: 0.0.1""", formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('target',help="The fixture-backed domain to scan. Example: example.test." ) parser.add_argument('-w', '--wordlist',help='Path to a wordlist file for the scan.' )return parserif__name__=='__main__': parser = build_argument_parser()if is_running_in_jupyter(): args = Args()else: args = parser.parse_args()print(args.target)if args.wordlist:print(args.wordlist)
1
Set up an argument parser using the argparse module, with a custom description and argparse.RawTextHelpFormatter to prevent wrapping.
2
Add one positional argument (host), with appropriate help text.
3
Add one optional argument (-w, --wordlist), with appropriate help text.
4
Return the argument parser for the rest of the script to use.
https://example.com
wordlist.txt
Run the script with -h or --help in a real terminal and compare the generated output with the parser definition. If everything went the way we expected, the help text should now describe both the required target and the optional wordlist path.