想了想应该用参数的话更好些加入getopt包

#!/usr/bin/python2

#auth:codewalker #mail:001@codewalker.me

#version 0.3

#date 2013-10-20
from time import sleep
from progressbar import ETA,ProgressBar,Bar, Percentage
import getopt
import sys

class timer:
def __init__(self, sec):
self.sec = sec

def main(self, sec):
widgets = [Bar(‘>’),
Percentage(),
“ “,
ETA()]
pbar = ProgressBar(widgets=widgets, maxval=(self.sec+1)10).start()
for i in range(1,(self.sec+1)
10):
sleep(0.1)
pbar.update(i+1)
pbar.finish()

def start(self):
self.main(self)

def USAGE():
print ‘’’
example:
timer.py -t|–total_time=5
‘’’

if __name__ == “__main__“:
if len(sys.argv) <= 1:
print “ERROR: No argvs”
USAGE()
exit(1)
try:
opts, args = getopt.getopt(sys.argv[1:],
“ht:”,
[“help”, “total_time=”])
except getopt.GetoptError, err:
print str(err)
USAGE()
sys.exit(2)

for o, a in opts:
if o in (“-h”, “–help”):
USAGE()
exit(1)
elif o in (“-t”, “–total_time”):
total_time = int(a)
else:
assert False, “unhandled option”

t = timer(total_time)
t.start()