我正在尝试接受 2 个命令行输入 0 0,即 sys.argv[1] = 0 和 sys.argv[2] = 0,如果不是,则应打印出一系列错误消息。但是无论我解析了多少命令行参数,我的代码都不会打印出任何输出。代码不应该终止并且 sys.argv[1] 和 sys.argv[2] 应该设置为默认值 0程序执行后。您必须验证命令行参数是否为整数。
import sys
import stdarray
import stdio
game_mode = sys.argv[1]
graphics_mode = sys.argv[2]
if len(sys.argv) > 2 :
stdio.writeln("Too many command-line arguments were given.")
if len(sys.argv) < 2 :
stdio.writeln(" Too few command-line arguments were given.")
if game_mode.isdigit:
game_mode = int(sys.argv[1])
if graphics_mode.isdigit:
graphics_mode = int(sys.argv[2])
if game_mode != 0:
stdio.writeln("Invalid game mode argument. Using the default value for game_mode instead.")
if graphics_mode != 0:
stdio.writeln("'Invalid graphics mode indicator argument. Using the default value for graphics_mode instead.")
game_mode_val , graphics_mode_val = sys.argv[1], sys.argv[2]
if game_mode_val or graphics_mode_val == 1:
stdio.writeln("This feature is not implemented for the current game mode!")
game_mode = 0
graphics_mode = 0