Zachary Ware added the comment:

The type parameter of ArgumentParser is a callable that will be called with the 
value of some member of sys.argv, it does *not* specify what the returned type 
will be.  You can just use os.fsencode as the type argument:

>>> import os
>>> import argparse
>>> p = argparse.ArgumentParser()
>>> p.add_argument('somebytes', type=os.fsencode, help='i want some bytes')
_StoreAction(option_strings=[], dest='somebytes', nargs=None, const=None, 
default=None, type=<function _fscodec.<locals>.fsencode at 0x00677AE0>, 
choices=None, help='i want some bytes', metavar=None)
>>> p.parse_args(['test'])
Namespace(somebytes=b'test')
>>> p.parse_args([os.fsdecode(b'\xde\xad\xbe\xef')])
Namespace(somebytes=b'\xde\xad\xbe\xef')

----------
nosy: +zach.ware

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue21416>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to