argparse用法

从下面的代码中main.py说明:

import argparse

parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
args = parser.parse_args()

print(args)

命令行执行1:
python main.py
输出:
Namespace(lr=0.1, resume=False)

命令行执行2:
python main.py -h
输出:

usage: main.py [-h] [--lr LR] [--resume]

PyTorch CIFAR10 Training

optional arguments:
  -h, --help    show this help message and exit
  --lr LR       learning rate
  --resume, -r  resume from checkpoint

命令行执行1:
python main.py --lr 0.8 -r
输出:
Namespace(lr=0.8, resume=True)

命令行执行1:
python main.py --lr .8 --resume
输出:
Namespace(lr=0.8, resume=True)

你可能感兴趣的:(python,python语法)