Currently I am learning command line arguments in python. So How do you pass optional command line arguments in Python?
What is the concept of comment line argument passing in python? can anyone show you example program how we can pass comment line arguments and what is optional command line arguments in Python? And How do we pass optional command line arguments in Python?
Nadiminti Naga Satya veni
The Arguments that are given after the name of program in the Command Line Shell of the Operating system are known as “Command Line Arguments”.Command Line Argument is a parameter supplied to the program when it is invoked.It is mostly used when you need to Control your program from outside.
To Run a command Line Arguments in Python IDLE:
1.You can call your “main” function directly on the IDLE Console with arguments if you want.
2.You can add a test line in front of your main function,call which supplies an array of arguments.
The Optional Arguments value can be set at run time from the command line like this:- Python my-example.py–my_optional=3.Then output is 3.
Python provides various types of arguments:
*Using Sys.argv
*Using get opt.module
*Using argparse.module
Example program using Command line Arguments:
# Python program to demonstrate
# command line arguments
import getopt, sys
# Remove 1st argument from the
# list of command line arguments
argumentList = sys.argv[1:]
# Options
options = “hmo:”
# Long options
long_options = [“Help”, “My_file”, “Output =”]
try:
# Parsing argument
arguments, values = getopt.getopt(argumentList, options, long_options)
# checking each argument
for currentArgument, currentValue in arguments:
if currentArgument in (“-h”, “–Help”):
print (“Displaying Help”)
elif currentArgument in (“-m”, “–My_file”):
print (“Displaying file_name:”, sys.argv[0])
elif currentArgument in (“-o”, “–Output”):
print ((“Enabling special output mode (% s)”) % (currentValue))
except getopt.error as err:
# output error, and return with an error code
print (str(err))
Output:
Hope this will help you.
Thank you!
How to create a dictionary in python?
How python is used in data science?