Python argparse 到游戏
Python argparse to a game
我正在尝试为我的冒险游戏找出 argparse。
这是我的代码:
parser = argparse.ArgumentParser(description='Adventure')
parser.add_argument('--cheat','-c', action='store_true', help='cheat')
parser.add_argument('--info','-i', action='store_true', help='Information' )
parser.add_argument('--version','-v', action='store_true', help='Version' )
parser.add_argument('--about', '-a', action='store_true', help='About' )
args = parser.parse_args()
if args.cheat or args.c:
print("Cheat")
sys.exit()
elif args.info or args.i:
print("Information")
sys.exit()
elif args.version or args.v:
print("Version")
sys.exit()
elif args.about or args.a:
print("About")
sys.exit()
else:
#Game code#
但是当我有所有这些参数时,我得到了一个错误。当我一开始只有作弊时效果很好,但是当我添加所有其他作弊时,它就搞砸了。要么我真的不知道我在做什么,要么我看不出有什么问题...
这是我的错误:
$ python3 adventure.py --info
Traceback (most recent call last):
File "adventure.py", line 12, in <module>
if args.cheat or args.c:
AttributeError: 'Namespace' object has no attribute 'c'
我之所以这样是因为我想在不启动游戏的情况下在终端输入这个,所以如果你只输入 python3 adventure.py 游戏就会开始。但我现在甚至不能那样做:P。
只有 python3 adventure.py -c 和 python3 adventure --cheat 有效。
有人知道解决这个问题的办法吗?
谢谢。
错误消息非常清楚:'Namespace' object has no attribute 'c'
。
argparse
库自动确定存储命令行选项值的属性名称 in the following way:
For optional argument actions, the value of dest
is normally inferred from the option strings. ArgumentParser
generates the value of dest
by taking the first long option string and stripping away the initial --
string. If no long option strings were supplied, dest
will be derived from the first short option string by stripping the initial -
character.
由于您可以选择长名称和短名称,因此使用长名称。换句话说,将 if args.cheat or args.c:
替换为 if args.cheat:
.
我正在尝试为我的冒险游戏找出 argparse。 这是我的代码:
parser = argparse.ArgumentParser(description='Adventure')
parser.add_argument('--cheat','-c', action='store_true', help='cheat')
parser.add_argument('--info','-i', action='store_true', help='Information' )
parser.add_argument('--version','-v', action='store_true', help='Version' )
parser.add_argument('--about', '-a', action='store_true', help='About' )
args = parser.parse_args()
if args.cheat or args.c:
print("Cheat")
sys.exit()
elif args.info or args.i:
print("Information")
sys.exit()
elif args.version or args.v:
print("Version")
sys.exit()
elif args.about or args.a:
print("About")
sys.exit()
else:
#Game code#
但是当我有所有这些参数时,我得到了一个错误。当我一开始只有作弊时效果很好,但是当我添加所有其他作弊时,它就搞砸了。要么我真的不知道我在做什么,要么我看不出有什么问题...
这是我的错误:
$ python3 adventure.py --info
Traceback (most recent call last):
File "adventure.py", line 12, in <module>
if args.cheat or args.c:
AttributeError: 'Namespace' object has no attribute 'c'
我之所以这样是因为我想在不启动游戏的情况下在终端输入这个,所以如果你只输入 python3 adventure.py 游戏就会开始。但我现在甚至不能那样做:P。 只有 python3 adventure.py -c 和 python3 adventure --cheat 有效。
有人知道解决这个问题的办法吗?
谢谢。
错误消息非常清楚:'Namespace' object has no attribute 'c'
。
argparse
库自动确定存储命令行选项值的属性名称 in the following way:
For optional argument actions, the value of
dest
is normally inferred from the option strings.ArgumentParser
generates the value ofdest
by taking the first long option string and stripping away the initial--
string. If no long option strings were supplied,dest
will be derived from the first short option string by stripping the initial-
character.
由于您可以选择长名称和短名称,因此使用长名称。换句话说,将 if args.cheat or args.c:
替换为 if args.cheat:
.