[issue21416] argparse should accept bytes arguments as originally passed

Zachary Ware report at bugs.python.org
Tue Jun 3 23:11:39 CEST 2014


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 <report at bugs.python.org>
<http://bugs.python.org/issue21416>
_______________________________________


More information about the Python-bugs-list mailing list