python argparse参数详解

                      python argparse使用注意事项!!!

import argparse

1.

parser = argparse.ArgumentParser(description="test argparse")

2.

parser.add_argument('-j','--workers', dest='pretrained', default=1, type=int, metavar='N',choices=[1,2],
                    help='number of total epochs to run')

"-"一般在最前面,是“--”的别名,在命令行中赋值采用“--”的,或者“-”均可以,调用时,若无dest,则调用“--”后面的名字,若有dest只能调用dest;

type默认str;choices是命令行赋值只能是这个里面的,否则报错,一般用 [list]

action=‘store_true’ : action参数表明:只要在终端有这个(--)参数,那么就会激活成True,否则为False。

  parser.add_argument('epoch‘,default=10,type=int)

不建议使用没有"-" or "--"的,这种是按照顺序对应的,在命令行中不能写前面的’epoch‘,直接赋值,容易出错,建议全部采用“--”。

args = parser.parse_args()

你可能感兴趣的:(python,argparse,Python)