【发布时间】:2015-03-17 02:57:18
【问题描述】:
我正在尝试为 Caesars Shift 加密程序创建一个简单的 main 函数,但是我不太确定如何配置所有这些命令行 opt/arg 内容。我想将程序配置为接受命令行参数,如下所示:
./caesars.py -e 13 message to encrypt
其中 13 是班次的数量,以下几行是要加密的消息。我已经定义了函数,但我只是不确定如何配置 opt arg 东西以接受 argv[1] 之后的第一个参数作为键,然后将之后的所有内容拆分为列表/长字符串。这是我目前所拥有的:
def main():
global decoder
global encoder
if not len(sys.argv[1:]):
usage()
# read the commandline options
try:
opts, args = getopt.getopt(sys.argv[1:],"hd:e:", ¬
["help","decode","encode"])
except getopt.GetoptError as err:
print str(err)
usage()
for o,a in opts:
if o in ("-h","--help"):
usage()
elif o in ("-d","--decode"):
decode = True
elif o in ("-e", "--encode"):
encode = True
else:
assert False,"Unhandled Option"
提前致谢。
【问题讨论】:
标签: command