推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
dengqianyi
V2EX  ›  Python

使用 python argparse 库(命令行工具)写参数说明太长会乱码

  •  
  •   dengqianyi · Aug 12, 2014 · 6056 views
    This topic created in 4479 days ago, the information mentioned may be changed or developed.
    self._parse = argparse.ArgumentParser(usage='项目编译脚本说明',description='create by dengqianyi')
    self._parse.add_argument('-file',help=str('可以从文件中读取已配置好的参数,手动输入的其他参数值将覆盖从文件中获得的参数值,文件需要和build.py同一目录下,非必须'))

    代码如上,在命令行中‘--help’时,-file的说明会如下

    -file FILE 可以从文件中读取已配置好的参数,手动?
    ?入的其他参数值将覆盖从文件中获得的?
    ?数值,文件需要和build.py同一目录下,非必?
    ??

    大家有碰到过没?有啥好的解决方案?有别的库可以使用不?
    Supplement 1  ·  Aug 13, 2014
    已经解决
    使用argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,usage='项目编译脚本说明',description='create by dengqianyi')
    方法创建对象可以免除‘被截取’的问题。
    #标准库的说明
    - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
    ArgumentDefaultsHelpFormatter -- Formatter classes which
    may be passed as the formatter_class= argument to the
    ArgumentParser constructor. HelpFormatter is the default,
    RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
    not to change the formatting for help text, and
    ArgumentDefaultsHelpFormatter adds information about argument defaults
    to the help.
    10 replies  •  2014-08-12 23:14:41 +08:00
    exoticknight
    exoticknight
    Aug 12, 2014
    1
    立刻去试了一下的确是这样咧
    感觉是换行的地方除了问题啊
    exoticknight
    exoticknight
    Aug 12, 2014
    2
    比较取巧的解决方法:在换行的地方加上空格,solved~
    ehs2013
    ehs2013
    Aug 12, 2014
    3
    被截半字了吧……
    dengqianyi
    dengqianyi
    OP
    Aug 12, 2014
    4
    @exoticknight 这解决方法不行啊,我在mac 下,可以显示的中文字就没你那么长,空格位置不好加。不知道可以换种写法,或者用其他库行不?
    exoticknight
    exoticknight
    Aug 12, 2014
    5
    @dengqianyi 这个就不清楚呢,不过原因应该就是3楼说的被截了吧……其实你看哪里被截了就在那打上两个空格就可以了
    jedihy
    jedihy
    Aug 12, 2014 via iPhone
    6
    gnu有个c的库,专门解析这个,能否参考?
    pyKun
    pyKun
    Aug 12, 2014
    7
    是个bug,刚看了下源码,http://hg.python.org/cpython/file/default/Lib/argparse.py#l217

    问题就是“截半字”

    len('中') == 3
    len('中'.decode('utf8')) == 1
    chilaoqi
    chilaoqi
    Aug 12, 2014
    8
    为毛我随便用以前的代码测了下,貌似可以啊。。。
    https://gist.github.com/chixq/375712e092dc1ebe22f3
    est
    est
    Aug 12, 2014
    9
    知足吧。 mac 下 emoji 还是占用半个字符位置呢。
    chilaoqi
    chilaoqi
    Aug 12, 2014❤️1
    10
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4639 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 03:18 · PVG 11:18 · LAX 19:18 · JFK 22:18
    ♥ Do have faith in what you're doing.