개발

Python Argparse 예제들

Cho et al. 2023. 1. 5.
import argparse 
parser = argparse.ArgumentParser(prog='myprogram', description='Process some files') 

# Required arguments 
parser.add_argument('input', help='The input file') 
parser.add_argument('output', help='The output file') 

# Optional arguments 
parser.add_argument('-v', ' - verbose', action='store_true', help='Enable verbose output')
parser.add_argument('-o', ' - overwrite', action='store_true', help='Overwrite the output file if it exists')
parser.add_argument('-n', ' - num-records', type=int, default=10, help='The number of records to process') 

# Mutually exclusive arguments 
group = parser.add_mutually_exclusive_group() 
group.add_argument('-u', ' - uppercase', action='store_true', help='Convert the output to uppercase') 
group.add_argument('-l', ' - lowercase', action='store_true', help='Convert the output to lowercase') 

# Arguments with choices 
parser.add_argument('-c', ' - compression', choices=['gzip', 'bzip2', 'lzma'], help='The compression algorithm to use') 

# Positional arguments with variable number of inputs 
parser.add_argument(' - exclude', nargs='+', help='Exclude the specified files from processing') 

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

 

From : Mastering Command Line Arguments in Python: A Comprehensive Guide with argparse | by Ethan Jones | Dec, 2022 | Python in Plain English

 

Mastering Command Line Arguments in Python: A Comprehensive Guide with argparse

In this blog, we will explore the various ways in which you can use the argparse module in Python to process command line arguments.

python.plainenglish.io

 

댓글