Command Line Arguments in Python in Hindi – कमांड लाइन आर्गुमेंट्स क्या हैं?
Table of Contents
- 1. Introduction to Command Line Arguments
- 2. Accessing Arguments
- 3. Applications of Command Line Arguments
- 4. Examples
- 5. Advantages of Command Line Arguments in Python in Hindi
जब भी हम कोई Python script लिखते हैं, तो कई बार जरूरत पड़ती है कि program को run करते समय हम कुछ extra information भी program को दें, बिना code को बार-बार change किए। यही काम Command Line Arguments करते हैं।
Command Line Arguments वो values होती हैं जो हम किसी Python file को terminal या cmd से run करते समय, file के नाम के साथ पास करते हैं।
इसका सबसे बड़ा फायदा यह है कि program के अंदर values को hardcode नहीं करना पड़ता, यानी बार-बार code खोलकर edit करने की जरूरत नहीं होती।
यह concept खासकर उन programs में बहुत useful है जहाँ user अलग-अलग बार अलग input देना चाहता है, जैसे file का नाम, कोई number, या कोई mode select करना।
College exams और interviews में भी यह topic अक्सर पूछा जाता है, इसलिए इसे basic से समझना जरूरी है।
Introduction to Command Line Arguments
सबसे पहले यह समझते हैं कि Command Line Arguments होते क्या हैं। जब हम terminal में कोई Python program run करते हैं, जैसे:
python script.py hello 25
तो यहाँ script.py के बाद जो hello और 25 लिखा है, यही Command Line Arguments कहलाते हैं। इनको program खुद अंदर से पढ़ सकता है और उसके हिसाब से काम कर सकता है।
पहला argument हमेशा script का नाम खुद होता है, यानी
script.py, और उसके बाद जो values दी जाती हैं वो actual arguments होते हैं।Python में इन arguments को पढ़ने के लिए एक built-in module मिलता है जिसका नाम
sysहै।sysmodule के अंदरargvनाम की एक list होती है, जिसमें सारे arguments string के form में store रहते हैं।चूंकि सब कुछ string के रूप में आता है, अगर आपको number चाहिए तो उसे
int()याfloat()से convert करना पड़ता है।यह concept उन programs के लिए बहुत useful होता है जिन्हें बार-बार अलग-अलग data के साथ run करना पड़ता है, जैसे कोई calculator या file processing tool।
Accessing Arguments
अब बात करते हैं कि program के अंदर इन arguments को access कैसे किया जाता है। इसके लिए मुख्य रूप से दो तरीके इस्तेमाल होते हैं – sys.argv और argparse।
1. sys.argv का उपयोग
sys.argv सबसे simple और basic तरीका है arguments पढ़ने का। यह एक list होती है जिसमें index 0 पर script का नाम और उसके बाद बाकी arguments होते हैं।
import sys
print("Script ka naam:", sys.argv[0])
print("Total arguments:", len(sys.argv))
print("Sare arguments:", sys.argv)
अगर इसे इस तरह run करें:
python demo.py rahul 21
तो output कुछ इस तरह आएगा –
Script ka naam: demo.py
Total arguments: 3
Sare arguments: ['demo.py', 'rahul', '21']
यहाँ ध्यान देने वाली बात यह है कि '21' string के रूप में आया है, number के रूप में नहीं। इसलिए अगर calculation करनी हो तो इसे convert करना जरूरी होता है।
2. argparse module का उपयोग
जब program बड़ा हो जाता है और arguments की संख्या ज्यादा होती है, तो सिर्फ sys.argv से काम चलाना मुश्किल हो जाता है। ऐसे में Python का argparse module बहुत काम आता है। इससे हम named arguments बना सकते हैं, default value दे सकते हैं और help message भी automatically मिल जाता है।
import argparse
parser = argparse.ArgumentParser(description="Student data lene ke liye program")
parser.add_argument("name", help="Student ka naam")
parser.add_argument("age", type=int, help="Student ki age")
args = parser.parse_args()
print("Naam:", args.name)
print("Age:", args.age)
इसे इस तरह run किया जा सकता है –
python student.py Priya 20
argparse का सबसे बड़ा फायदा यह है कि अगर user गलत तरीके से arguments देता है, तो यह अपने आप एक clear error message और usage instructions दिखा देता है, जिससे debugging आसान हो जाती है।
यह SVG दिखा रहा है कि Terminal से दिए गए arguments, sys.argv के जरिए किस तरह list के रूप में Python program तक पहुँचते हैं।
Applications of Command Line Arguments
Command Line Arguments सिर्फ theory के लिए नहीं हैं, बल्कि real-world programs में इनका बहुत use होता है। कुछ common applications नीचे दिए गए हैं –
File Processing Tools:
बहुत सारे tools जो file पढ़ते या convert करते हैं, वो file का नाम command line argument के रूप में लेते हैं, जैसेpython convert.py report.csv।Automation Scripts:
Automation में अक्सर अलग-अलग environment (जैसे test या production) के लिए एक ही script को अलग arguments के साथ run किया जाता है।Command Line Tools और Utilities:
Linux या Windows के बहुत सारे tools command line arguments लेकर ही काम करते हैं, जैसेping,ls,gitआदि।Testing और Debugging:
Developers अक्सर एक ही program को अलग-अलग test data के साथ बार-बार run करते हैं, बिना code बदले।Machine Learning Scripts:
ML training scripts में अक्सर learning rate, epochs जैसी values command line से ही पास की जाती हैं, ताकि हर बार नया experiment आसानी से चलाया जा सके।Server और Deployment Scripts:
कई deployment scripts port number या config file का path command line argument से लेते हैं।
Examples
अब कुछ practical examples देखते हैं जिनसे यह concept और clear हो जाएगा।
Example 1: दो numbers को add करना
import sys
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
result = num1 + num2
print("Result:", result)
Run करने का तरीका –
python add.py 10 20
Output –
Result: 30
Example 2: Arguments की गिनती check करना
import sys
if len(sys.argv) < 2:
print("Kripya kam se kam ek argument dein")
else:
print("Aapne diya:", sys.argv[1])
यहाँ पर हम पहले check कर रहे हैं कि user ने कोई argument दिया भी है या नहीं, ताकि program crash न हो।
Example 3: argparse के साथ optional argument
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="File ka naam")
parser.add_argument("--verbose", action="store_true", help="Extra detail dikhaye")
args = parser.parse_args()
print("File:", args.filename)
if args.verbose:
print("Verbose mode chalu hai")
यहाँ --verbose एक optional flag है, जो अगर user दे तो extra information print होगी, नहीं तो नहीं। यह real projects में बहुत common pattern है।
यह SVG एक Terminal window को दिखा रहा है, जिसमें दो अलग-अलग Python programs को command line arguments के साथ run किया गया है।
Advantages of Command Line Arguments in Python in Hindi
Code बार-बार change नहीं करना पड़ता:
हर बार नई value देने के लिए source code खोलने की जरूरत नहीं, सिर्फ terminal से value pass कर दो।Automation आसान हो जाता है:
Scripts को दूसरे programs या shell scripts के साथ जोड़कर automatically अलग-अलग input के साथ run किया जा सकता है।Testing में समय बचता है:
अलग-अलग test values को command line से देकर जल्दी-जल्दी program को test किया जा सकता है।Flexibility मिलती है:
एक ही program कई तरह की situations में इस्तेमाल हो सकता है, बस arguments बदलने होते हैं।Real world tools जैसा experience:
Students को यह सीखने से पता चलता है कि professional command line tools अंदर से कैसे काम करते हैं।argparse से user-friendly output:
Help messages और error handling built-in मिल जाते हैं, जिससे program ज्यादा professional लगता है।