Horje
Argparse: Way to include default values in '-help'?

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.

Include default values in ‘–help’

Below are the ways to include default values in ‘–help’ in Python.

  • Using argparse.ArgumentDefaultsHelpFormatter
  • Using Custom Help Formatter

Include default values in ‘–help’ Using argparse.ArgumentDefaultsHelpFormatter

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.

Python
import argparse
parser = argparse.ArgumentParser(description="GeeksforGeeks argparse Example1",
                                 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--course', type=str, default='Python', help='Course name at GeeksforGeeks')
parser.add_argument('--duration', type=int, default=10, help='Course duration in weeks')
parser.add_argument('--level', type=str, default='Beginner', help='Course difficulty level')
args = parser.parse_args()
print(f"Course: {args.course}")
print(f"Duration: {args.duration} weeks")
print(f"Level: {args.level}")

Output:

EX1

Include default values in ‘–help’ Using Custom Help Formatter

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.

Python
import argparse
class CustomHelpFormatter(argparse.HelpFormatter):
    def _get_help_string(self, action):
        help = action.help
        if action.default is not argparse.SUPPRESS:
            help += f' (default: {action.default})'
        return help
parser = argparse.ArgumentParser(description="GeeksforGeeks argparse Example 2",
                                 formatter_class=CustomHelpFormatter)
parser.add_argument('--course', type=str, default='Python', help='Course name at GeeksforGeeks')
parser.add_argument('--duration', type=int, default=10, help='Course duration in weeks')
parser.add_argument('--level', type=str, default='Beginner', help='Course difficulty level')
args = parser.parse_args()
print(f"Course: {args.course}")
print(f"Duration: {args.duration} weeks")
print(f"Level: {args.level}")

Output:

Ex2



Reffered: https://www.geeksforgeeks.org


Python

Related
Python Pyramid - Application Configuration Python Pyramid - Application Configuration
Building Powerful Telegram Bots with Telethon in Python Building Powerful Telegram Bots with Telethon in Python
Working with multiple environments in Python Poetry Working with multiple environments in Python Poetry
Create a Single Executable from a Python Project Create a Single Executable from a Python Project
How to Fix the "TypeError: descriptors cannot be created directly" in Python How to Fix the "TypeError: descriptors cannot be created directly" in Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15