w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python command line argument with args.parse argument form list doesnt work

You need to pass -c as a string. Wrap it in quotes:

parser.add_argument('-c', action='store', dest='colour',
type=str, 
                    help='Choose Colour')

Otherwise python will pass the value of the variable -c, whatever it is. Hope it helps!

Edit: Oh, that wasn't the problem... hahaha

Well, i figured out what it is anyway. When you're passing WH as an argument to the script, you are not passing the value of the variable WH - which is 255, 255, 255 - but instead you're passing the string literal "WH" as the argument to draw.line. Hence the error.

To fix this, you should specify the parameter nargs, and discard the type parameter, since the latter isn't needed:

parser.add_argument('-c', action='store', dest='colour',
nargs=3,
                    help='Choose Colour')
...
colours = (int(i) for i in args.colours)
pygame.draw.line(screen, *colours,(nSX),(nSY),(nEX,nEY),1)

And when you're running it:

python LineGenerator.py -c 255, 255, 255

That's all! Hope it helps!





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.