Having options in argparse with a dash

前端 未结 5 1446
长发绾君心
长发绾君心 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

    getattr(args, 'positional-arg')

    This is another OK workaround for positional arguments:

    #!/usr/bin/env python3
    
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('a-b')
    args = parser.parse_args(['123'])
    assert getattr(args, 'a-b') == '123'
    

    Tested on Python 3.8.2.

提交回复
热议问题