distutils: How to pass a user defined parameter to setup.py?

前端 未结 8 979
温柔的废话
温柔的废话 2020-11-30 21:32

Please prompt me how to pass a user-defined parameter both from the command line and setup.cfg configuration file to distutils\' setup.py script. I want to write a setup.py

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 22:25

    A quick and easy way similar to that given by totaam would be to use argparse to grab the -foo argument and leave the remaining arguments for the call to distutils.setup(). Using argparse for this would be better than iterating through sys.argv manually imho. For instance, add this at the beginning of your setup.py:

    argparser = argparse.ArgumentParser(add_help=False)
    argparser.add_argument('--foo', help='required foo argument', required=True)
    args, unknown = argparser.parse_known_args()
    sys.argv = [sys.argv[0]] + unknown
    

    The add_help=False argument means that you can still get the regular setup.py help using -h (provided --foo is given).

提交回复
热议问题