Skip to content

Instantly share code, notes, and snippets.

@nanaze
Last active May 10, 2016 18:27
Show Gist options
  • Save nanaze/db63e3f63e318408e3223bf1245d9752 to your computer and use it in GitHub Desktop.
Save nanaze/db63e3f63e318408e3223bf1245d9752 to your computer and use it in GitHub Desktop.
Demonstrate strange argpase behavior
# argparse demo
import argparse
def _GetParser():
parser = argparse.ArgumentParser()
parser.add_argument('--foo', type=bool, default=False)
return parser
def _PrintArgParse(args):
print 'args:', args
parser = _GetParser()
args = parser.parse_known_args(args)
print args
print
def main():
_PrintArgParse(['--foo=True'])
_PrintArgParse(['--foo=False'])
if __name__ == '__main__':
main();
@nanaze
Copy link
Author

nanaze commented May 10, 2016

Command output:

$ python --version
Python 2.7.6

$ python argparse_demo.py 
args: ['--foo=True']
(Namespace(foo=True), [])

args: ['--foo=False']
(Namespace(foo=True), [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment