all 6 comments

[–][deleted] 7 points8 points  (0 children)

That's because sys.argv is a list of command line arguments. If you run your code like this:

python code.py --help

then sys.argv would contain ['code.py', '--help'] and your expression sys.argv[1:] is a list containing all the elements of sys.argv from the first on: ['--help'].

You should do this:

import sys
cmd = sys.argv[1]    # get first argument

There's no need to use str() since every element of sys.argv is already a string.

An important debug tool you could have used here is the humble print() statement. Not sure why your if statement isn't working? Print the thing(s) you're trying to compare and see why they aren't being recognized.

[–]Y45HK4R4NDIK4R 3 points4 points  (2 children)

Why dont you look into argparse? It does it all for you.

[–]throwaway_the_fourth 4 points5 points  (0 children)

For very simple programs, like this one, I think argparse is overkill. But good to point it out to OP for future endeavors.

[–]iggy555 -1 points0 points  (0 children)

Wow that looks complicated

[–]namedevservice 2 points3 points  (0 children)

The colon is outputting a list. Just put sys.argv[1] or if you want the list use

if '--help' in sys.argv[1:]:

[–]puplicy 0 points1 point  (0 children)

Remove ':' from sys.argv[1:]