[파이썬] argparse 인수의 선택적 값 설정

Python provides the argparse module, which makes it easy to parse command-line arguments and options. It allows you to define arguments, their types, and default values to create a flexible command-line interface for your Python programs.

In this blog post, we will focus on how to set optional values for arguments using argparse. This can be useful when you have arguments that are not required but can be provided if needed.

Setting up argparse

To begin, let’s import the argparse module and create a new parser object:

import argparse

parser = argparse.ArgumentParser()

Adding Optional Arguments

To define an optional argument, you can use the add_argument() method of the parser object. The add_argument() method takes several parameters, including the name of the argument, its type, default value, and help message.

parser.add_argument("-o", "--optional", type=int, default=0, help="Optional argument with default value 0")

In the above example, we have defined an optional argument with the name -o and --optional, which can be used interchangeably. The type parameter specifies the type of the argument (in this case, an integer), and the default parameter sets the default value to 0. The help parameter provides a description of the argument when the --help option is used.

Retrieving Optional Argument Values

To retrieve the value of an optional argument provided by the user, you can access it using the namespace returned by the parse_args() method of the parser object.

args = parser.parse_args()

optional_value = args.optional

In the above code snippet, parse_args() will parse the command-line arguments and store them in the args variable. To retrieve the value of the optional argument, we can simply access the optional attribute of args.

Example Usage

Here’s an example of how you can use the optional arguments in a Python script:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-o", "--optional", type=int, default=0, help="Optional argument with default value 0")

args = parser.parse_args()

optional_value = args.optional

print("Optional value:", optional_value)

Running the script without providing any optional argument will result in the default value being used:

$ python script.py
Optional value: 0

Using the optional argument:

$ python script.py -o 42
Optional value: 42

Conclusion

In this blog post, we have learned how to set optional values for arguments using argparse in Python. With the help of the add_argument() method, we can define optional arguments, specify their types, and set default values. By using parse_args(), we can access the provided optional arguments and use them in our Python scripts.

Argparse is a powerful and flexible module that simplifies the process of handling command-line arguments and options. It gives you the ability to create robust command-line interfaces for your Python applications with ease.

I hope this blog post has been helpful in understanding how to set optional values for arguments using argparse in Python.