How to use getopt/OPTARG in Python? How to shift arguments if too many arguments (9) are given?

后端 未结 3 1088
猫巷女王i
猫巷女王i 2021-01-31 09:25

How to use getopt/optarg in Python?

3条回答
  •  無奈伤痛
    2021-01-31 09:51

    Have you tried reading the python docs for the module getopt (http://docs.python.org/library/getopt.html?highlight=getopt#module-getopt)? It provides a simple example of how the getopt is used. What do you mean by shift arguments? If you want to check that the user does not use more than 9 arguments, you can check the length of the sys.argv list, which contains all the options/arguments passed to the script. The first element is the name of the script which is invoked, so the length is always at least 1. You could do something like:

    if len(sys.argv) > 10
        print('Too many arguments.')
    

提交回复
热议问题