[파이썬] argparse RawDescriptionHelpFormatter 활용

In Python, the argparse module provides a simple way to handle command-line arguments and options. One of the useful features of argparse is the ability to customize how the help messages are formatted. In this blog post, we will explore the RawDescriptionHelpFormatter class in argparse and see how it can be used to improve the formatting of our command-line help text.

The RawDescriptionHelpFormatter class in argparse allows us to preserve the formatting of the description and epilog text that we pass to our argument parser. By default, argparse reflows the text to fit within the available width of the terminal, but by using RawDescriptionHelpFormatter, we can retain the original line breaks and indentation. This can be especially useful when we have complex help messages or code examples that need to be displayed in a specific format.

To use RawDescriptionHelpFormatter, we need to create an instance of it and pass it as the formatter_class argument when creating our argument parser. Here’s an example:

import argparse

# Create an argument parser with RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)

# Add your argument and options
parser.add_argument("-f", "--file", help="Specify the input file")
# ...

# Parse the command-line arguments
args = parser.parse_args()

Now, let’s take a closer look at a real-world example to understand how RawDescriptionHelpFormatter works. Suppose we have a command-line tool that helps users convert images from one format to another. We want to provide a detailed description and usage examples in the help message. Here’s a sample code snippet:

import argparse

description = """
A command-line tool to convert images from one format to another.

Usage examples:
- Convert a PNG image to JPEG:
  $ python image_converter.py --input image.png --output image.jpg

- Resize an image by specifying the dimensions:
  $ python image_converter.py --input image.jpg --output image_resized.jpg --width 800 --height 600
"""

# Create an argument parser with RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(description=description, formatter_class=argparse.RawDescriptionHelpFormatter)

# Add your argument and options
parser.add_argument("--input", help="Specify the input image file")
parser.add_argument("--output", help="Specify the output image file")
parser.add_argument("--width", type=int, help="Specify the width of the output image")
parser.add_argument("--height", type=int, help="Specify the height of the output image")

# Parse the command-line arguments
args = parser.parse_args()

In this example, we provide a detailed description of our image converter tool using triple quotes ("""). We can then add usage examples with proper indentation. When the user runs the script with the -h or --help option, they will see the description and examples displayed exactly as we formatted them.

Using the argparse RawDescriptionHelpFormatter class allows us to maintain the readability and clarity of our help messages, making it easier for users to understand the available options and usage examples. It is a powerful tool for creating user-friendly command-line interfaces with Python.