1

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()
Hutch
  • 411
  • 10
  • 32
  • 1
    Have you read the [argparse tutorial](https://docs.python.org/3.7/howto/argparse.html)? 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. – Arne Aug 08 '19 at 11:44
  • 1
    Also consider [click](https://palletsprojects.com/p/click/) as an alternative, it's quite popular for being a little easier to use. – Arne Aug 08 '19 at 11:44
  • 2
    You need to `add_argument` with the right [parameters](https://docs.python.org/3/library/argparse.html#the-add-argument-method) (`nargs`/`required`/...) – jdehesa Aug 08 '19 at 11:46

3 Answers3

7

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.

Charul
  • 202
  • 2
  • 12
  • 4
    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. – hlongmore Jun 16 '20 at 03:46
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))
mujjiga
  • 16,186
  • 2
  • 33
  • 51
  • That worked, I have also added the help switch to add comments about what is required from the argument. – Hutch Aug 08 '19 at 13:43
-1

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
Dani Gonzalez
  • 93
  • 1
  • 7
  • This is just simply not true. `ArgumentParser` supports parsing out the unknown arguments, as answered in mujjiga's answer above. – Human-Compiler Dec 07 '21 at 17:39