#!/usr/bin/python
import sys
import getopt
class
usage
(exception)
:def
__init__
(self, msg)
: self.msg = msg
defmain
(ar**=
none):
if ar** is
none
: ar** = sys.ar**
try:try
: opts, args = getopt.getopt(ar**[1:
],"h",
["help"])
except getopt.error, msg:
raise usage(msg)
except usage, err:
print
>>sys.stderr, err.msg
print
>>sys.stderr,
"for help use --help"
return
2if __name__ ==
"__main__"
: sys.exit(main(
))
getopt模組用於抽出命令列選項和引數,也就是sys.ar**。
命令列選項使得程式的引數更加靈活。支援短選項模式和長選項模式
opts, args = getopt.getopt( sys.ar**[1:
], shortargs, longargs )
getopt.getopt ( [命令列引數列表], 『短選項』, [長選項列表] )
'''
'''>>
>
import getopt, sys
>>
> arg =
'-a -b -c foo -d bar a1 a2'
>>
> optlist, args = getopt.getopt( sys.ar**[1:
],'abc:d:'
)>>
> optlist[(
'-a',''
),('-b',''
),('-c'
,'foo'),
('-d'
,'bar')]
>>
> args
['a1'
,'a2'
]>>
> arg =
'--condition=foo --testing --output-file abc.def -x a1 a2'
>>
> optlist, args = getopt.getopt( sys.ar**[1:
],'x',
['condition='
,'output-file='
,'testing'])
>>
> optlist[(
'--condition'
,'foo'),
('--testing',''
),('--output-file'
,'abc.def'),
('-x',''
)]>>
> args
['a1'
,'a2'
]
main函式的多種寫法
int main int argc,char argv int main void 最好main 函式向系統返回乙個整型,表示函式正常結束。我只推薦上面兩種寫法 關於main函式的其他幾種寫法,也列舉出來做比較 void main 我們一般使用這種寫法,第一很多書上也是這樣寫的,第二不要int也能正...
python中的main函式
python是一種解釋型指令碼語言,和c c 語言不同,c c 程式從main函式開始執行,python程式從開始到結尾順序執行。先總結下python中的main函式的作用 讓模組 函式 可以自己單獨執行 除錯 相當於構造了呼叫其它函式的入口,這就類似於c c 裡面的main函式了。一方面 我們想要...
python中main函式的用法
什麼場景下會有main函式?當該python指令碼被作為模組 module 引入 import 時,其中的main 函式將不會被執行。main函式的作用?name main 是python的main函式入口。並非說,加入這句才能使用python py來執行,而是說,這裡可以判斷,當前是否是直接被py...