V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2014-08-12 19:02:24 +08:00 · 4797 次点击
    这是一个创建于 3535 天前的主题,其中的信息可能已经有所发展或是发生改变。
    self._parse = argparse.ArgumentParser(usage='项目编译脚本说明',description='create by dengqianyi')
    self._parse.add_argument('-file',help=str('可以从文件中读取已配置好的参数,手动输入的其他参数值将覆盖从文件中获得的参数值,文件需要和build.py同一目录下,非必须'))

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

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

    大家有碰到过没?有啥好的解决方案?有别的库可以使用不?
    第 1 条附言  ·  2014-08-13 09:33:52 +08:00
    已经解决
    使用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 条回复    2014-08-12 23:14:41 +08:00
    exoticknight
        1
    exoticknight  
       2014-08-12 19:51:17 +08:00
    立刻去试了一下的确是这样咧
    感觉是换行的地方除了问题啊
    exoticknight
        2
    exoticknight  
       2014-08-12 19:53:44 +08:00
    比较取巧的解决方法:在换行的地方加上空格,solved~
    ehs2013
        3
    ehs2013  
       2014-08-12 21:16:31 +08:00
    被截半字了吧……
    dengqianyi
        4
    dengqianyi  
    OP
       2014-08-12 21:51:21 +08:00
    @exoticknight 这解决方法不行啊,我在mac 下,可以显示的中文字就没你那么长,空格位置不好加。不知道可以换种写法,或者用其他库行不?
    exoticknight
        5
    exoticknight  
       2014-08-12 22:02:31 +08:00
    @dengqianyi 这个就不清楚呢,不过原因应该就是3楼说的被截了吧……其实你看哪里被截了就在那打上两个空格就可以了
    jedihy
        6
    jedihy  
       2014-08-12 22:08:26 +08:00 via iPhone
    gnu有个c的库,专门解析这个,能否参考?
    pyKun
        7
    pyKun  
       2014-08-12 22:19:47 +08:00
    是个bug,刚看了下源码,http://hg.python.org/cpython/file/default/Lib/argparse.py#l217

    问题就是“截半字”

    len('中') == 3
    len('中'.decode('utf8')) == 1
    chilaoqi
        8
    chilaoqi  
       2014-08-12 23:07:29 +08:00
    为毛我随便用以前的代码测了下,貌似可以啊。。。
    https://gist.github.com/chixq/375712e092dc1ebe22f3
    est
        9
    est  
       2014-08-12 23:10:48 +08:00
    知足吧。 mac 下 emoji 还是占用半个字符位置呢。
    chilaoqi
        10
    chilaoqi  
       2014-08-12 23:14:41 +08:00   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:07 · PVG 04:07 · LAX 13:07 · JFK 16:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.