[issue21416] argparse should accept bytes arguments as originally passed
Changes by Zachary Ware zachary.w...@gmail.com: -- resolution: - not a bug stage: - resolved status: open - closed ___ 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
[issue21416] argparse should accept bytes arguments as originally passed
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
[issue21416] argparse should accept bytes arguments as originally passed
paul j3 added the comment: Two points to keep in mind: 'argparse' works with 'sys.argv[1:]'. If that does not contain what you want, then you can pass your own 'argv' to 'parse_args'. 'type=bytes' means, call the builtin 'bytes' function with one of the argv strings. If 'bytes' does not handle the string as you want, then you need to write your own function. -- nosy: +paul.j3 ___ 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
[issue21416] argparse should accept bytes arguments as originally passed
paul j3 added the comment: 'invalid bytes value' is the error message generated by 'argparse'. The underlying error (for a string like 'xxx') is: print(bytes(sys.argv[1])) TypeError: string argument without an encoding You could use 'bytes' if you somehow supply the encoding, as in: def mybytes(astr): return bytes(astr, 'utf-8') -- ___ 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
[issue21416] argparse should accept bytes arguments as originally passed
New submission from Derek Wilson: If I create an argument parser like: parser = argparse.ArgumentParser() parser.add_argument('somebytes', type=bytes, help='i want some bytes') parser.parse_args() the parse_args() call will raise an exception printing usage info indicating that an invalid bytes value was passed if any of the bytes on the command line are 127. if i'm specifying that i want bytes then i should expect that the argument should be interpreted as bytes and not text. I get that #8776 was closed because it makes sense not to clutter up internals, but in this instance i am building a command line parser and telling it exactly what i expect. if the solution from #8776 of os.fsencode(sys.argv) will definitely always work then argparse should do this internally if i tell it i expect bytes on the command line. -- components: Library (Lib) messages: 217761 nosy: underrun priority: normal severity: normal status: open title: argparse should accept bytes arguments as originally passed type: behavior versions: Python 3.4, Python 3.5 ___ 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