0

I'm trying to make Argparse using unicode in both PY2 and PY3 preferably without complex constructs like if six.PY2: or sys.version_info.major. When it comes to using unicode for io operations or immediate string literals I know I have to use io/codecs or imports from __futute__ to resolve these issues. However not sure what is the best approach for argparse. Currently I'm using this code:

parser = argparse.ArgumentParser()
argv_enc = sys.getfilesystemencoding()
parser.add_argument('url', type=lambda s: bytearray(s,argv_enc).decode(argv_enc))

I think it's incorrect because bytearray tries to decode ASCII and I don't need this step at all. Instead I'd like to decode s only if it's not already decoded. Thanks in advance.

Alex
  • 252
  • 1
  • 6
  • 18
  • This isn't really an argparse issue, since a function that works as `type` would also work converting the `args.url` string after parsing. How about a simple `try/except` clause? – hpaulj Jul 12 '17 at 15:49
  • you're right, I just added `argparse` tag in case if someone knows a better way of doing the same stuff using argparse module. For now I decided to use `if six.PY2` approach. – Alex Jul 12 '17 at 16:09

0 Answers0