#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- import argparse class ArgumentParserWriteUTF8(argparse.ArgumentParser): """Subclass of ArgumentParser that outputs UTF-8 properly Standard ArgumentParser in argparse outputs help messages without converting to UTF-8. This fails if the message is a unicode string containing non-ASCII characters and output is redirected (to a pipe or a file). Unredirected output seems OK. See http://bugs.python.org/issue9779 This class fixes it. It will probably not be needed in Python 3. """ def _print_message(self, message, file=None): """Output message to file, encoded as UTF-8 """ if message: if file is None: file = _sys.stderr file.write(message.encode('utf-8')) parser = ArgumentParserWriteUTF8(description = u'שלום') parser.parse_args()