How to read/process command line arguments?

后端 未结 17 2010
离开以前
离开以前 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:32
    import sys
    
    print("\n".join(sys.argv))
    

    sys.argv is a list that contains all the arguments passed to the script on the command line.

    Basically,

    import sys
    print(sys.argv[1:])
    
    0 讨论(0)
  • 2020-11-21 05:33

    If you need something fast and not very flexible

    main.py:

    import sys
    
    first_name = sys.argv[1]
    last_name = sys.argv[2]
    print("Hello " + first_name + " " + last_name)
    

    Then run python main.py James Smith

    to produce the following output:

    Hello James Smith

    0 讨论(0)
  • 2020-11-21 05:33

    I recommend looking at docopt as a simple alternative to these others.

    docopt is a new project that works by parsing your --help usage message rather than requiring you to implement everything yourself. You just have to put your usage message in the POSIX format.

    0 讨论(0)
  • 2020-11-21 05:35

    As you can see optparse "The optparse module is deprecated with and will not be developed further; development will continue with the argparse module."

    0 讨论(0)
  • 2020-11-21 05:37

    Just going around evangelizing for argparse which is better for these reasons.. essentially:

    (copied from the link)

    • argparse module can handle positional and optional arguments, while optparse can handle only optional arguments

    • argparse isn’t dogmatic about what your command line interface should look like - options like -file or /file are supported, as are required options. Optparse refuses to support these features, preferring purity over practicality

    • argparse produces more informative usage messages, including command-line usage determined from your arguments, and help messages for both positional and optional arguments. The optparse module requires you to write your own usage string, and has no way to display help for positional arguments.

    • argparse supports action that consume a variable number of command-line args, while optparse requires that the exact number of arguments (e.g. 1, 2, or 3) be known in advance

    • argparse supports parsers that dispatch to sub-commands, while optparse requires setting allow_interspersed_args and doing the parser dispatch manually

    And my personal favorite:

    • argparse allows the type and action parameters to add_argument() to be specified with simple callables, while optparse requires hacking class attributes like STORE_ACTIONS or CHECK_METHODS to get proper argument checking
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题