Python Argparse: Issue with optional arguments which are negative numbers

后端 未结 7 2016
走了就别回头了
走了就别回头了 2020-12-08 13:59

I\'m having a small issue with argparse. I have an option xlim which is the xrange of a plot. I want to be able to pass numbers like <

7条回答
  •  没有蜡笔的小新
    2020-12-08 14:21

    If you specify the value for your option with an equals sign, argparse will not treat it as a separate option, even if it starts with -:

    ./blaa.py --xlim='-0.002 1e4'
    # As opposed to --xlim '-0.002 1e4'
    

    And if the value does not have spaces in it, you can drop the quotes:

    ./blaa.py --xlim=-0.002
    

    See: https://www.gnu.org/software/guile/manual/html_node/Command-Line-Format.html

    With this, there is no need to write your own type= parser or redefine the prefix character from - to @ as the accepted answer suggests.

提交回复
热议问题