How to read/process command line arguments?

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

    Yet another option is argh. It builds on argparse, and lets you write things like:

    import argh
    
    # declaring:
    
    def echo(text):
        "Returns given word as is."
        return text
    
    def greet(name, greeting='Hello'):
        "Greets the user with given name. The greeting is customizable."
        return greeting + ', ' + name
    
    # assembling:
    
    parser = argh.ArghParser()
    parser.add_commands([echo, greet])
    
    # dispatching:
    
    if __name__ == '__main__':
        parser.dispatch()
    

    It will automatically generate help and so on, and you can use decorators to provide extra guidance on how the arg-parsing should work.

提交回复
热议问题