2

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()
3
  • 1
    Have you read the argparse tutorial? It seems that you didn't declare any arguments, but if you want to know how to use and document them, you'll need a lot more knowledge than can be put in a quick answer. Commented Aug 8, 2019 at 11:44
  • 1
    Also consider click as an alternative, it's quite popular for being a little easier to use. Commented Aug 8, 2019 at 11:44
  • 2
    You need to add_argument with the right parameters (nargs/required/...) Commented Aug 8, 2019 at 11:46

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I realize this answer probably wasn't what the OP was looking for, but it was what I was looking for in my Google search that landed me here.
2
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))

1 Comment

That worked, I have also added the help switch to add comments about what is required from the argument.
-2

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

1 Comment

This is just simply not true. ArgumentParser supports parsing out the unknown arguments, as answered in mujjiga's answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.