Created
March 20, 2014 02:23
-
-
Save ctb/9656001 to your computer and use it in GitHub Desktop.
weird behavior by argparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
def main(): | |
argParser = argparse.ArgumentParser(description='Test argparser') | |
argParser.add_argument('-A', type=str, nargs=1, \ | |
default='myarg', \ | |
choices=['myarg', 'otherarg'], \ | |
help='Select which arg to run', dest='arg') | |
argVals = argParser.parse_args() | |
print (argVals.arg,) | |
# $ ./argparse_demo.py | |
# ('myarg',) | |
# $ ./argparse_demo.py -A myarg | |
# (['myarg'],) | |
# $ ./argparse_demo.py -A otherarg | |
# (['otherarg'],) | |
if __name__ == "__main__": | |
main() |
It's because nargs
will always make the result a list. If you leave nargs=1
out, it will return strings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why does '-A myarg' make argVals.arg a list, while default keeps it a string?