Having options in argparse with a dash

前端 未结 5 1436
长发绾君心
长发绾君心 2020-12-12 18:58

I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is n

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 19:13

    Dashes are converted to underscores:

    import argparse
    pa = argparse.ArgumentParser()
    pa.add_argument('--foo-bar')
    args = pa.parse_args(['--foo-bar', '24'])
    print args # Namespace(foo_bar='24')
    

提交回复
热议问题