type=dict in argparse.add_argument()

后端 未结 11 691
既然无缘
既然无缘 2020-12-03 07:02

I\'m trying to set up a dictionary as optional argument (using argparse); the following line is what I have so far:

parser.add_argument(\'-i\',\'--image\', t         


        
11条回答
  •  自闭症患者
    2020-12-03 07:19

    Using simple lambda parsing is quite flexible:

    parser.add_argument(
        '--fieldMap',
        type=lambda v: {k:int(v) for k,v in (x.split(':') for x in v.split(','))},
        help='comma-separated field:position pairs, e.g. Date:0,Amount:2,Payee:5,Memo:9'
    )
    

提交回复
热议问题