Don't argparse read unicode from commandline?

前端 未结 2 515
执念已碎
执念已碎 2021-02-03 10:15

Running Python 2.7

When executing:

$ python client.py get_emails -a \"åäö\"

I get:

usage: client.py get_emails [-h] [-a         


        
2条回答
  •  不思量自难忘°
    2021-02-03 10:56

    The command-line arguments are encoded using sys.getfilesystemencoding():

    import sys
    
    def commandline_arg(bytestring):
        unicode_string = bytestring.decode(sys.getfilesystemencoding())
        return unicode_string
    
    # ...
    parser_get_emails.add_argument('-a', '--area', type=commandline_arg)
    

    Note: You don't need it in Python 3 (the arguments are already Unicode). It uses os.fsdecode() in this case because sometimes command-line arguments might be undecodable. See PEP 383 -- Non-decodable Bytes in System Character Interfaces.

提交回复
热议问题