Python 参数传递

#!-*-coding:utf8 -*-
#要在首行才可以在后面用中文注释    
import getopt
import sys
import os


if __name__ == '__main__':
#    ...
    helpdoc = '''
Description

    ...

Usage

    python pyscript.py -i/--ifile <input file> -o/--ofile <output file> -t/--time <int> ...

Parameters

    -h/--help
        Print helpdoc
    -i/--ifile
        Input file,including only one column with sampleId
    -o/--ofile
        Output file, including two columns, the 1st column is sampleId, the 2nd column is attribute information
    -t/--time
        Time for interval (seconds, default 5s)
    ...
'''

try:
    opts,args = getopt.getopt(sys.argv[1:], "hi:o:t:n:", ["help","ifile=", "ofile=", "time="])
    if len(opts) == 0:
        print("Options Error!\n\n"+helpdoc)
        sys.exit(2)
except getopt.GetoptError:
    print("Options two Error!\n\n"+helpdoc)
    sys.exit(2)

for opt,arg in opts:
    if opt in ("-h","--help"):
        print(helpdoc)
        sys.exit()
    elif opt in ("-i","--ifile"):
        infile = arg
    elif opt in ("-o","--ofile"):
        outfile = arg
    elif opt in ("-t","--time"):
        sleep_time = int(float(arg))
    elif opt in ("-n","--requests-number"):
        requestNum = int(float(arg))

print (infile)
print (outfile)
print (sleep_time)
print (requestNum)

参考 1、python参数输入 2、cmdbtools

此条目发表在Python分类目录。将固定链接加入收藏夹。

发表评论

邮箱地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Protected with IP Blacklist CloudIP Blacklist Cloud