← Back to Python Course | Chapter 12: Standard Library | Lesson 8 of 9

Python argparse मॉड्यूल

argparse आपके program को terminal में उसके नाम के बाद टाइप किए गए options समझने देता है, जैसे detailed order लेता कोई waiter। यह अपने-आप helpful instructions भी बना देता है।
Syntax
python
import argparse

parser = argparse.ArgumentParser(description="text")
parser.add_argument("--option", help="text")
args = parser.parse_args()

argparse क्या है?

argparse declaratively आपकी script के लिए एक command-line interface बनाता है: आप बताते हैं कि program कौन-कौन से arguments स्वीकार करता है, और यह मॉड्यूल sys.argv parse करने, input validate करने, और आपके descriptions से --help message अपने-आप बना देने का काम संभाल लेता है -- वो भी बिना आपको कोई manual string-splitting logic लिखे।

उदाहरण: What is argparse?

python
import argparse
parser = argparse.ArgumentParser()  # builds a command-line interface declaratively
print(parser)

Positional Arguments

Positional arguments बिना किसी leading dash के declare किए जाते हैं और position के हिसाब से आवश्यक होते हैं -- किसी को छोड़ देने पर argparse आपके program logic चलने से पहले ही एक usage error print करके exit हो जाता है, जो missing required input तुरंत पकड़ने के लिए उपयोगी है।

उदाहरण: Positional Arguments

python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name")  # positional, required, no leading dash
args = parser.parse_args(["Alex"])
print(args.name)

Optional Arguments

Optional arguments एक leading -- (या छोटे -x alias) के साथ declare किए जाते हैं और ज़रूरी नहीं होते, जिससे user इन्हें छोड़ने पर भी script sensible defaults के साथ चल सके।

यह उन configuration flags के लिए स्वाभाविक फिट है जिन्हें ज़्यादातर users override करने की ज़रूरत नहीं होती।

उदाहरण: Optional Arguments

python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--greeting", default="Hello")  # optional, falls back to the default
args = parser.parse_args([])
print(args.greeting)

Argument Types बताना

Default रूप से argparse द्वारा निकाला गया हर argument value एक plain string होता है, भले ही वो command line पर numeric दिखे।

add_argument() को type=int या type=float भेजना argparse को input को अपने-आप convert और validate करने के लिए कहता है, और conversion fail होने पर एक साफ़ error raise करता है।

उदाहरण: Specifying Argument Types

python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--age", type=int)  # converts the string input to int automatically
args = parser.parse_args(["--age", "25"])
print(args.age + 1)

Choices और Boolean Flags

choices=[...] किसी argument को valid values के एक fixed set तक सीमित कर देता है, और बाकी कुछ भी दिए जाने पर एक helpful error message के साथ reject कर देता है।

action=store_true किसी argument को एक simple on/off flag बना देता है जिसे किसी साथ वाले value की ज़रूरत नहीं होती, बस command line पर उसकी मौजूदगी या अनुपस्थिति ही काफी होती है।

उदाहरण: Choices and Boolean Flags

python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--mode", choices=["fast", "slow"])  # restricted to these values
parser.add_argument("--verbose", action="store_true")  # on/off flag, no value needed
args = parser.parse_args(["--mode", "fast", "--verbose"])
print(args.mode, args.verbose)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.