Python-argparse-命令行与参数解析

时间:2022-10-27 16:59:28
 

 

 

import argparse
import numpy as np
import cv2
import os
import numpy.random as npr
from dface.core.utils import IoU
import dface.config as config

def parse_args():
    parser = argparse.ArgumentParser(description='Test mtcnn',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('traindata_store', help='dface train data temporary folder,include 12,24,48/postive,negative,part,landmark',
                        default='../data/wider/', type=str)
    parser.add_argument('traindata_store_plus', help='dface train data temporary folder,include 12,24,48/postive,negative,part,landmark',
                        default='../data/wider/', type=str)
    parser.add_argument('--anno_file', dest='annotation_file', help='wider face original annotation file',
                        default=os.path.join(config.ANNO_STORE_DIR,"wider_origin_anno.txt"), type=str)
    parser.add_argument('--prefix_path', dest='prefix_path', help='annotation file image prefix root path',
                        default='', type=str)




    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()
    print(args)

  

命令行调用方式:

parser.add_argument('traindata_store',)这种形式前面没有--,就说明是必填项,在命令行中直接输入参数值即可

traindata_store_plus 与 traindata_store_plus 要按顺序填充。

 

(dface) MacBook-Pro:DFace sumeng$ python parse_test.py cc ff --a dd

Namespace(annotation_file='dd', prefix_path='', traindata_store='cc', traindata_store_plus='ff')

(dface) MacBook-Pro:DFace sumeng$ python parse_test.py ffv ccv --a dd

Namespace(annotation_file='dd', prefix_path='', traindata_store='ffv', traindata_store_plus='ccv')

  

对于参数前有--的,属于非必填项,填写的时候要  --anno_file 参数值   (中间有个空格)或者直接写 --a 参数值 或者 --ann 参数值 等

 

(dface) MacBook-Pro:DFace sumeng$ python parse_test.py cc --a dd

Namespace(annotation_file='dd', prefix_path='', traindata_store='cc')