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

argparse cli

# importing required modules
import argparse
 
# create a parser object
parser = argparse.ArgumentParser(description = "An addition program")
 
# add argument
parser.add_argument("add", nargs = '*', metavar = "num", type = int,
                     help = "All the numbers separated by spaces will be added.")
 
# parse the arguments from standard input
args = parser.parse_args()
 
# check if add argument has any input data.
# If it has, then print sum of the given numbers
if len(args.add) != 0:
    print(sum(args.add))
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

argparse type

parser.add_argument("arg1", type=int)
Comment

argparse python sentence

python Application.py -env="-env"
Comment

PREVIOUS NEXT
Code Example
Python :: how to add textbox in pygame window 
Python :: check regex in python 
Python :: np.zeros 
Python :: value_counts with nan 
Python :: matplotlib bar chart 
Python :: calculator in python 
Python :: how to run python file 
Python :: matplotlib python background color 
Python :: discord.py send image from url 
Python :: python pillow cut image in half 
Python :: django slug int url mapping 
Python :: can is slice list with list of indices python 
Python :: audio streaming python 
Python :: django q objects 
Python :: data normalization python 
Python :: separating tuple in pandas 
Python :: count_values in python 
Python :: What is role of ALLOWED_HOSTs in Django 
Python :: filter in pandas 
Python :: edit pandas row value 
Python :: decimal to binary in python 
Python :: double char python 
Python :: create an empty numpy array and append 
Python :: numpy random matrix 
Python :: matplotlib set integer ticks 
Python :: python Modulo 10^9+7 (1000000007) 
Python :: python smtp sendmail 
Python :: pandas plot date histogram 
Python :: how to print correlation to a feature in pyhton 
Python :: python tqdm 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =