1

I'm using argparse to set up the parameters for my python script. The last parameter is an "ID number" and it can be any mix of alphanumeric characters and hyphens.

Note: The program is a downloader/decoder for MMS messages and the "ID" parameter is provided by the cell network, so I can't change what it is.

I'm having a problem where, if the ID starts with a hyphen, argparse reads it as multiple parameters instead of as a string.

Here is the argparse portion of my code:

version = "0.4 alpha"

parser = argparse.ArgumentParser(
    description="MMS Viewer v{0}: An MMS Downloader and Decoder".format(version),
    epilog="https://github.com/NTICompass/mms-viewer"
)

parser.add_argument('-V', '--version', action='version', version=version)

parser.add_argument("file_or_phone", help="MMS File or phone number")
parser.add_argument("mmsid", nargs="?", help="MMS-Transaction-ID")

parser.add_argument('--debug', help="Print debugging info", action="store_true")

group = parser.add_mutually_exclusive_group()
group.add_argument('-x', '--extract', help="Extract image file(s)", action="store_true")
group.add_argument('-X', '--extract-original', help="Extract original image file(s) without using PIL", action="store_true")

args = parser.parse_args()

I run the program as such ./main.py 15555555555 abc123xyz (you can also run it as ./main.py file.bin).

The issue comes when the final parameter starts with hyphen, as shown:

./main.py 15555555555 -mTkKkr5e

It seems that argparse is reading the "-mTkKkr5e" as multiple different flags and thus gives the error:

usage: main.py [-h] [-V] [-p] [--debug] [-d | -x | -X] file_or_phone [mmsid]
main.py: error: unrecognized arguments: -mTkKkr5e

How can I fix this so that python/argparse will give me the final parameter as the string "-mTkKkr5e"?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337

0 Answers0