有没有办法绕行需要多个值来解包?

时间:2017-07-06 00:24:56

标签: python options argv

我在python中的模块sys中使用argv时遇到了麻烦,我希望有一个脚本可以像vim这样的程序或者像-t这样的类似参数的其他程序。在这些类型的程序中,您有一个或多个参数。如果我想要一个参数它给我一个值错误,有没有办法使用argv有一个或多个选项?如果没有像ls或vim这样的程序如何使用选项?

import sys

argv = sys.argv

script, option1, option2, option3 = argv
if option1 or option2 or option3 == '--help':
    print 'Help will be added after the script is finished W-I-P'
elif option1 or option2 or option3 == '':
    print 'if you need help type time.py --help'
else:
    print 'Syntax error refer to --help for options'

谢谢!

1 个答案:

答案 0 :(得分:0)

这有帮助吗?

import sys

argv = sys.argv

if '--help' in argv:
    print 'Help will be added after the script is finished W-I-P'

elif len(argv) < 4:
    print 'Syntax error refer to --help for options'

else:
    script, option1, option2, option3 = argv

输出:

> python test.py --help
Help will be added after the script is finished W-I-P

> python test.py 1 2 3 --help
Help will be added after the script is finished W-I-P

> python test.py 1 2
Syntax error refer to --help for options

> python test.py 1 2 3
>