![]() |
VOOZH | about |
Argparse in Python allows you to create user-friendly command-line interfaces. By including default values in the --help output, you can make your script's behavior more transparent. There are several ways to achieve this, including using argparse.ArgumentDefaultsHelpFormatter and custom help formatters.
Below are the ways to include default values in '--help' in Python.
In this example, we are using argparse.ArgumentDefaultsHelpFormatter to automatically include the default values of the arguments in the --help output. This makes it clear to users what the defaults are for --course, --duration, and --level options.
Output:
In this example, we are using a custom help formatter by subclassing argparse.HelpFormatter to include default values in the help message. The _get_help_string method is overridden to append the default value to each argument's help text, providing clarity on what defaults are used.
Output: