How to read/process command line arguments?

后端 未结 17 2011
离开以前
离开以前 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:22

    The docopt library is really slick. It builds an argument dict from the usage string for your app.

    Eg from the docopt readme:

    """Naval Fate.
    
    Usage:
      naval_fate.py ship new ...
      naval_fate.py ship  move   [--speed=]
      naval_fate.py ship shoot  
      naval_fate.py mine (set|remove)   [--moored | --drifting]
      naval_fate.py (-h | --help)
      naval_fate.py --version
    
    Options:
      -h --help     Show this screen.
      --version     Show version.
      --speed=  Speed in knots [default: 10].
      --moored      Moored (anchored) mine.
      --drifting    Drifting mine.
    
    """
    from docopt import docopt
    
    
    if __name__ == '__main__':
        arguments = docopt(__doc__, version='Naval Fate 2.0')
        print(arguments)
    

提交回复
热议问题