越來越參數我想從命令行得到三個參數:命令行蟒蛇
-o (for outputfile) -k (number of clusters) -l (data to be clustered)
所以我寫了這一點。
def get_input():
print 'ARGV :', sys.argv[1:]
options, remainder = getopt.getopt(sys.argv[1:], 'o:v:k:l', ['output=',
'verbose',
'k_clust=',
'limit='])
print "options ",options
file_flag , k_flag, count_flag = False, False,False
for opt, arg in options:
print opt
if opt in ('-o', '--output'):
print "here ", opt, arg
output_filename = arg
o_flag = True
if opt in ('-v', '--verbose'):
verbose = True
if opt == '--version':
version = arg
if opt in ('-k','--k_clust'):
print "here", opt, arg
k_clust = arg
k_flag = True
if opt in ('-l','--limit'):
kcount = arg
assert kcount!=0 and kcount!= ''
print "limit ", arg
count_flag = True
if k_flag == False:
sys.exit(" no cluster specified, will be exiting now")
if o_flag == False:
print "using default outfile name ",output_filename
if count_flag == False:
kcount = 10000000
return output_filename, k_clust,kcount
一切工作的罰款,除了-l標誌 所以如果我的命令行命令是這樣的:
$python foo.py -o foo.txt -k 2 -l 2
和打印的argv打印
ARGV : ['-o', 'demo.txt', '-k', '2', '-l', '2']
但選項是:
options [('-o', 'demo.txt'), ('-k', '2'), ('-l', '')]
請注意,在「l」字段中沒有任何內容正在被解析。 笏我做錯了嗎? 謝謝
+1找到真正的問題(和解決方案)。 – unutbu 2012-01-27 20:46:45