简体   繁体   中英

Allow unknown arguments using argparse

I have a python script that requires the user to enter two arguments to run it, the arguments could be named anything.

I have also used argparse to allow the users to use a switch '-h' to get instructions of what is required to run the script.

The problem is that now I have used argparse I am getting an error when I pass my two randomly named arguments with the script.

import argparse

parser = argparse.ArgumentParser(add_help=False)

parser.add_argument('-h', '--help', action='help',
                    help='To run this script please provide two arguments')
parser.parse_args()

currently when I run python test.py arg1 arg2 the error is

error: unrecognized arguments: arg1 arg2

I would like the code to allow the user to run test.py with a -h if required to see the instructions but also allow them to run the script with any two arguments as well.

Resolution with help tag to provide the user with context regarding the arguments required.

   parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument('-h', '--help', action='help', help='To run this script please provide two arguments: first argument should be your scorm package name, second argument should be your html file name. Note: Any current zipped folder in the run directory with the same scorm package name will be overwritten.')
    parser.add_argument('package_name', action="store",  help='Please provide your scorm package name as the first argument')
    parser.add_argument('html_file_name', action="store", help='Please provide your html file name as the second argument')

    parser.parse_args()
import argparse

parser = argparse.ArgumentParser(description='sample')

# Add mandatory arguments
parser.add_argument('arg1', action="store")
parser.add_argument('arg2', action="store")

# Parse the arguments
args = parser.parse_args()
# sample usage of args
print (float(args.arg1) + float(args.arg2))

You need to add those arguments to the parser:

parser.add_argument("--arg1", "-a1", dest='arg1', type=str)
parser.add_argument("--arg2","-a2", dest='arg2', type=str)

If those arguments don't have the param required=true , you will be able to call the program without this arguments, so you can run the program with only the -h flag. To run the program with the arguments:

python test.py --arg1 "Argument" --arg2 "Argument"

Then, to have the arguments in variables you have to read them:

args = parser.parse_args()
argument1=args.arg1
argument2=args.arg2

Try the following code :-

 import argparse

 parser = argparse.ArgumentParser(add_help=False)

 parser.add_argument('-h', '--help', action='help',
                help='To run this script please provide two arguments')
 parser.add_argument('arg1')
 parser.add_argument('arg2')

 args, unknown = parser.parse_known_args()

All your unknown arguments will be parsed in unknown and all known in args .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM