[getopt-sig] Parse arguments according to a usage message?
David Boddie
david@sleepydog.net
Mon, 18 Feb 2002 15:08:49 +0000
Having read the messages about separating mechanics from semantics
http://mail.python.org/pipermail/getopt-sig/2002-February/000039.html
and generating usage messages automatically
http://mail.python.org/pipermail/getopt-sig/2002-February/000024.html
I wonder whether there is any mileage in using part of the class that I
mentioned in the "RFC: Option Parsing Libraries" thread in python-dev?
http://www-solar.mcs.st-and.ac.uk/~davidb/Software/Python/cmdsyntax/
Here's some abridged output from the test.py script included in the
cmdsyntax.zip archive found from the page above. Apologies for the amount
of output, but I'm trying to illustrate the capabilities of the parser.
The first example allows either the short or long form of switches/options
to be specified, although the parser doesn't know that -d and --directory
might be equivalent in this case.
[davidb@bunsen CMDSyntax]$ test.py -d output myfile
Syntax: ./test.py [(-d dir) | --directory=dir] infile
Ordered optional arguments? [y/n] n
[Removed some output here...]
First match found:
dir : output
infile : myfile
-d : 1
[davidb@bunsen CMDSyntax]$ test.py --directory=output myfile
Syntax: ./test.py [(-d dir) | --directory=dir] infile
Ordered optional arguments? [y/n] n
[Removed some output here...]
First match found:
infile : myfile
--directory : output
Arguments can be optional and may contain optional arguments of their
own.
[davidb@bunsen CMDSyntax]$ test.py -a -b bernard
Syntax: ./test.py [-a] [[-b badger] -c]
Ordered optional arguments? [y/n] n
[Removed some output here...]
Quick match with command line? [y/n] y
No matches found
[davidb@bunsen CMDSyntax]$ test.py -b bernard -c
Syntax: ./test.py [-a] [[-b badger] -c]
Ordered optional arguments? [y/n] n
[Removed some output here...]
Quick match with command line? [y/n] y
First match found:
-b : 1
-c : 1
badger : bernard
Optional arguments placed together can be ordered arbitrarily without
disturbing mandatory arguments which must occur in the correct order.
[davidb@bunsen CMDSyntax]$ test.py input -b hello -a world output
Syntax: ./test.py infile [-a abacus] [-b binary] outfile [-c computer]
Ordered optional arguments? [y/n] n
[Removed some output here...]
Quick match with command line? [y/n] n
First match found:
infile : input
binary : hello
abacus : world
-b : 1
-a : 1
outfile : output
Is this sort of thing useful, or relevant to the discussion?
David
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________