There are the following modules in the standard library.
There are the following modules in the standard library: The getopt module is similar to GNU getopt. The optparse module offers object-oriented command line option parsing. Here is an example that uses the latter from the docs: from optparse import OptionParser parser = OptionParser() parser.
Add_option("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE") parser. Add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout") (options, args) = parser. Parse_args() optparse supports (among other things): Multiple options in any order.
Short and long options. Default values. Generation of a usage help message.
– edgerA Jun 17 '09 at 22:42 12 Yes, these are the best. Since they're part of the standard library, you can be sure they'll be available and they're easy to use. Optparse in particular is powerful and easy.
– Barry Wark Jun 17 '09 at 22:43 @edgerA: What does "better custom way" mean? – S. Lott Jun 17 '09 at 23:29 1 optparse is one of the best; getopt is old and really ought to be considered deprecated.
– jemfinch 12/20111 at 20:49 at this point (12/2011), argparse is now considered a better option than optparse, correct? – oob 12/20112 at 21:48.
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.
7 This is now part of standard Python as of 2.7 and 3.2 :) – orange80 Sep 3 '10 at 2:08.
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.
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.
2 For really simple stuff, this is the way to go, although you probably only want to use sys. Argv1: (avoids the script name). – Xiong Chiamiov Sep 12 '10 at 7:31 this is the right answer!
– karantan Dec 23 '11 at 8:42.
One way to do it is using sys.argv. This will print the script name as the first argument and all the other parameters that you pass to it. Import sys for arg in sys.
Argv: print arg.
There is also argparse module (an "impovement" on stdlib's optparse module). Example from the introduction to argparse: # script. Py import argparse if __name__ == '__main__': parser = argparse.ArgumentParser() parser.
Add_argument( 'integers', metavar='int', type=int, choices=xrange(10), nargs='+', help='an integer in the range 0..9') parser. Add_argument( '--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)') args = parser. Parse_args() print args.
Accumulate(args. Integers) Usage: $ script. Py 1 2 3 4 4 $ script.Py --sum 1 2 3 4 10.
As you can see optparse "The optparse module is deprecated with and will not be developed further; development will continue with the argparse module.
I like getopt from stdlib, eg: try: opts, args = getopt. Getopt(sys. Argv1:, 'h', 'help') except getopt.
GetoptError, err: usage(err) for opt, arg in opts: if opt in ('-h', '--help'): usage() if len(args)! = 1: usage("specify thing...") Lately I have been wrapping something similiar to this to make things less verbose (eg; making "-h" implicit).
You may be interested in a little Python module I wrote to make handling of command line arguments even easier (open source and free to use) - Commando.
There is already another command-line parsing module named Commando: github.com/lakshmivyas/commando. It wraps argparse by using decorators. – Roberto Bonvallet Sep 27 '11 at 15:04.
My solution is entrypoint2. Example: from entrypoint2 import entrypoint @entrypoint def add(file, quiet=True): ''' This function writes report. :param file: write report to FILE :param quiet: don't print status messages to stdout ''' print file,quiet help text: usage: report.Py -h -q --debug file This function writes report.
Positional arguments: file write report to FILE optional arguments: -h, --help show this help message and exit -q, --quiet don't print status messages to stdout --debug set logging level to DEBUG.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.