Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python argparse

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--date', help='date of event', type=str)
parser.add_argument('-t', '--time', help='time of event', type=str)
args = parser.parse_args()

print(f'Event was on {args.date} at {args.time}')
Comment

python argparse

import argparse

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--name", required=True, help="name of the user")
args = vars(ap.parse_args())

# display a friendly message to the user
print("Hi there {}, it's nice to meet you!".format(args["name"]))
Comment

python argparse file argument

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()

print(args.file.readlines())
Comment

how to use argparse

import argparse

if __name__ == "__main__":
	#add a description
	parser = argparse.ArgumentParser(description="what the program does")

	#add the arguments
	parser.add_argument("arg1", help="advice on arg")
	parser.add_argument("arg2", help="advice on arg")
#						.
# 						.
#   					.
	parser.add_argument("argn", help="advice on arg")

	#this allows you to access the arguments via the object args
	args = parser.parse_args()

	#how to use the arguments
	args.arg1, args.arg2 ... args.argn
Comment

use argparse to call function and use argument in function

# Parse the subcommand argument first
parser = ArgumentParser(add_help=False)
parser.add_argument("function", 
                    nargs="?",
                    choices=['function1', 'function2', 'function2'],
                    )
parser.add_argument('--help', action='store_true')
args, sub_args = parser.parse_known_args(['--help'])

# Manually handle help
if args.help:
    # If no subcommand was specified, give general help
    if args.function is None: 
        print parser.format_help()
        sys.exit(1)
    # Otherwise pass the help option on to the subcommand
    sub_args.append('--help')

# Manually handle the default for "function"
function = "function1" if args.function is None else args.function

# Parse the remaining args as per the selected subcommand
parser = ArgumentParser(prog="%s %s" % (os.path.basename(sys.argv[0]), function))
if function == "function1":
    parser.add_argument('-a','--a')
    parser.add_argument('-b','--b')
    parser.add_argument('-c','--c')
    args = parser.parse_args(sub_args)
    function1(args.a, args.b, args.c)
elif function == "function2":
    ...
elif function == "function3":
    ...
Comment

PREVIOUS NEXT
Code Example
Python :: python list elements 
Python :: def function in python 
Python :: how to run pyttsx3 in a loop 
Python :: django check user admin 
Python :: How to loop over grouped Pandas dataframe? 
Python :: pandas read excel with two headers 
Python :: sklearn support vector machine 
Python :: object value python 
Python :: python http request params 
Python :: what is the difference between tuples and lists in python 
Python :: tab of nbextensions not showing in jupyter notebook 
Python :: args kwargs python 
Python :: import file in parent directory python 
Python :: dataframe string find count 
Python :: deleting models with sqlalchemy orm 
Python :: discord.py reference 
Python :: install coverage python 
Python :: numpy random in python 
Python :: suppress python vs try/except 
Python :: breadth first search python 
Python :: pyspark now 
Python :: python script to copy files to remote server 
Python :: keyboardinterrupt python 
Python :: install python 3.8 
Python :: uninstall python using powershell 
Python :: algorithms for Determine the sum of al digits of n 
Python :: how to update sklearn 
Python :: how do i get parent directory python 
Python :: d-tale colab 
Python :: filter django or 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =