Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 :: lower and upper case user input python 
Python :: keras.callbacks.History 
Python :: matplotlib remove drawn text 
Python :: librosa python 
Python :: django raw without sql injection 
Python :: how to create dynamic list in python 
Python :: how to get a specific character in a string on number python 
Python :: how to form .cleaned data in class based views in django 
Python :: python gui framework 
Python :: project euler problem 11 python 
Python :: swap two lists without using third variable python 
Python :: mean absolute error in machine learning formula 
Python :: expand pandas dataframe into separate rows 
Python :: python bin() 
Python :: Python operator to use for set union 
Python :: python if in list 
Python :: python re.sub() 
Python :: split strings around given separator/delimiter 
Python :: padnas check if string is in list of strings 
Python :: sort a list python 
Python :: max value of a list prolog 
Python :: python poetry 
Python :: Sendgrid dynamic templating 
Python :: pd.cut in pandas 
Python :: field in django 
Python :: gui button in tkinter color 
Python :: roc curve 
Python :: telegram telethon get new user details 
Python :: python pandas how to access a column 
Python :: interviewbit with Python questions solutions 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =