[getopt-sig] Discussion over?

David Boddie david@sleepydog.net
Tue, 2 Apr 2002 17:55:50 +0100


There hasn't been any discussion on this list for a while which either means
that people are too busy to move the discussion forwards, or that a consensus
has been reached behind the scenes.

In the meantime, I finally got round to writing my version of the ripoff
interface for the library comparison page:

http://www.python.org/sigs/getopt-sig/compare.html

Allowing large numbers of options to be specified in any order gave my
implementation some difficulty and an alternative approach was required.

The library and example scripts can be found via the page at

http://david.boddie.org.uk/Projects/Python/CMDSyntax/index.html

As the ripoff interface test is quite short, I've posted it here since
parts of it require comment:

> #! /usr/bin/env python
> import cmdsyntax, sys
> 
> # Define the syntax.

[] indicate optional arguments; () indicate groups of arguments;
A|B indicates that either A or B must occur.

> syntax = """
>   [-h | --help] | [--version] |
>   (
>     [
>       (-v | --verbose=V) | (-q | --quiet)
>     ]
>     
>     [( -d DEVICE) | (--device=DEVICE)]
>     [(-b DIR) | --output-base=DIR]
>   
>     [(-t TRACKS) | --tracks=TRACKS]
>   
>     [
>       (-p | --use-pipes | --synchronous)
>       |
>       (
>         (-f | --use-files | --asynchronous)
>         [
>   	      (-k | --keep-tmp) [-R | --rip-only]
>   	    ]
>       )
>     ]
>     
>     [-P | --playback]
>     [-e | --eject]
>     [(-A ARTIST) | --artist=ARTIST]
>     [(-L ALBUM) | --album=ALBUM]
>     [(-D DISC) | --disc=DISC]
>     [(-y YEAR) | --year=YEAR]
>     [--offset=OFFSET]
>   )
> """

The ripoff syntax isn't just a flat sequence of optional parameters.
Despite assertions that -v and -q should be tolerated on the same command
line for cases when aliases to the ripoff script are being used, I have
separated them and consider use of both on the same command line to be
invalid.

There may be other cases where use of one option precludes the use of
another for valid input.

> if __name__ == "__main__":
> 
> 	# Create a syntax object.
> 	syntax_obj = cmdsyntax.Syntax(syntax)
> 
> 	# Match the command line arguments against the syntax definition.
> 	matches = syntax_obj.get_args(sys.argv[1:])

This method returns a list of dictionaries. Ideally, there should be a
single dictionary returned, but some syntaxes may be ambiguous and will
return more than one match.
	
> 	# No valid matches were found.
> 	if matches == []:
> 	
> 		print "Syntax:", syntax
> 		sys.exit()
> 	
> 	# Show each valid match.
> 	for match in matches:
> 	
> 		print match
> 		print
> 	
> 	sys.exit()

Note that no coercion of types is performed. It would be up to the
developer, or another library author, to ensure that user input conformed
to that expected by the script.

Has this raised any useful points to get the discussion going again?

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
________________________________________________________________________