Python Argparse: Issue with optional arguments which are negative numbers

后端 未结 7 1998
走了就别回头了
走了就别回头了 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:31

    One workaround I've found is to quote the value, but adding a space. That is,

    ./blaa.py --xlim " -2.e-3" 1e4
    

    This way argparse won't think -2.e-3 is an option name because the first character is not a hyphen-dash, but it will still be converted properly to a float because float(string) ignores spaces on the left.

提交回复
热议问题