如何在argparse python中传递多个标志



我试图传递多个标志,基本上是两个标志。这就是我的代码

parser.add_argument('--naruto', action='store_true')
parser.add_argument('--transformers', action='store_true')
parser.add_argument('--goku', action='store_true')
parser.add_argument('--anime', action='store_true')

我知道action='store_true'使它成为一个标志。基本上在命令行中,我将传递像=>nameOfTheScript.py --goku --anime,在此基础上,我将检查是否"动画"作为参数发送,do x else do y。我怎样才能做到这一点呢?

如果我理解正确,您会问如何解析这些参数。下面是一个参数解析的例子:

parser.add_argument('--naruto', action='store_true')
parser.add_argument('--transformers', action='store_true')
parser.add_argument('--goku', action='store_true')
parser.add_argument('--anime', action='store_true')
args = parser.parse_args()
# You can access your args with dot notation
if args.goku:
print("goku arg was passed in")
if args.anime:
print("anime arg was passed in")

最新更新