Python 主要功能命令行参数长列表
Python Main Function Command Line Argument Long List
我正在尝试为 Caesars Shift 密码程序创建一个简单的主要功能,但是我不太确定如何配置所有这些命令行 opt/arg 东西。我想将程序配置为接受命令行参数,如下所示:
./caesars.py -e 13 message to encrypt
其中 13 是移位量,以下行是要加密的消息。我定义了函数,但我只是不确定如何配置 opt arg 内容以接受 argv[1] 之后的第一个参数作为键,然后将其后的所有内容拆分为 list/long 字符串。这是我目前所拥有的:
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"
提前致谢。
如果你还没有,你应该看看 docopt。虽然我自己从未使用过它,但它非常受欢迎,应该非常适合您。
我正在尝试为 Caesars Shift 密码程序创建一个简单的主要功能,但是我不太确定如何配置所有这些命令行 opt/arg 东西。我想将程序配置为接受命令行参数,如下所示:
./caesars.py -e 13 message to encrypt
其中 13 是移位量,以下行是要加密的消息。我定义了函数,但我只是不确定如何配置 opt arg 内容以接受 argv[1] 之后的第一个参数作为键,然后将其后的所有内容拆分为 list/long 字符串。这是我目前所拥有的:
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"
提前致谢。
如果你还没有,你应该看看 docopt。虽然我自己从未使用过它,但它非常受欢迎,应该非常适合您。