How to read/process command line arguments?

后端 未结 17 2488
离开以前
离开以前 2020-11-21 05:14

I am originally a C programmer. I have seen numerous tricks and \"hacks\" to read many different arguments.

What are some of the ways Python programmers can do this

17条回答
  •  深忆病人
    2020-11-21 05:42

    I use optparse myself, but really like the direction Simon Willison is taking with his recently introduced optfunc library. It works by:

    "introspecting a function definition (including its arguments and their default values) and using that to construct a command line argument parser."

    So, for example, this function definition:

    def geocode(s, api_key='', geocoder='google', list_geocoders=False):
    

    is turned into this optparse help text:

        Options:
          -h, --help            show this help message and exit
          -l, --list-geocoders
          -a API_KEY, --api-key=API_KEY
          -g GEOCODER, --geocoder=GEOCODER
    

提交回复
热议问题