Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > CVS style argument parsing?

Reply
Thread Tools

CVS style argument parsing?

 
 
Josef Wolf
Guest
Posts: n/a
 
      07-20-2004
Hello!

Is it possible to have CVS-style command line options with the optparse
module? I could get as far as:

import sys
from optparse import OptionParser, OptionGroup
parser = OptionParser()

opt_global = OptionGroup (parser, "Global options")
opt_global.add_option("-m", "--mode", choices=["import", "dump"],
dest="mode", default="dump",
help="One out of import, dump")
opt_global.add_option("-d", "--dbname", default="test",
help="The name of the database")
opt_global.add_option("-D", "--dbpath", default=Cnf.dbpath,
help="The path to the database")

opt_dump = OptionGroup (parser, "Dumper options")
opt_dump.add_option("-v", "--dump-values",
action="store_true", dest="values", default=False,
help="Dump values.")

How can I get rid of the "-m" option so that

myscript -dfoo dump -v

would be possible?

--
Please visit and sign http://petition-eurolinux.org and http://www.ffii.org
-- Josef Wolf -- http://www.velocityreviews.com/forums/(E-Mail Removed) --
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      07-21-2004
Don't use OptionGroups for each command. Instead, you'll have the
OptionParser for the "main" arguments, and the option parser for each
command.

By using parser.disable_interspersed_args() you can make the parsing of
"-a b -c".split() handle "-a" and return "b -c".split() as the
positional args. Then, depending on what b is, send -c to a separate
parser instance.

I don't see where disable_interspersed_args() is documented, but it's
right there in optparse.py

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA/bb/Jd01MZaTXX0RAq/3AKCLvk6gBbfjgEg5DSKRIBVeXaI84wCfbl/F
HqObWHOMpSP5gp6MXHM3D4g=
=jaco
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Josef Wolf
Guest
Posts: n/a
 
      07-21-2004
Jeff Epler schrieb im Artikel <(E-Mail Removed)>:

> Don't use OptionGroups for each command. Instead, you'll have the
> OptionParser for the "main" arguments, and the option parser for each
> command.


I tried to do this before, and had exactly the problem you describe below.

> By using parser.disable_interspersed_args() you can make the parsing of

^^^^^^^^^^^^^^^^^^^^^^^^^^^
> "-a b -c".split() handle "-a" and return "b -c".split() as the
> positional args. Then, depending on what b is, send -c to a separate
> parser instance.


Aaaahhh, this was the bit of information I was missing. Is it "legal" to
use this method? It seems not to be documented in the library reference?

Thanks!

--
Please visit and sign http://petition-eurolinux.org and http://www.ffii.org
-- Josef Wolf -- (E-Mail Removed) --
 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      07-21-2004
On Wed, Jul 21, 2004 at 06:37:05AM +0000, Josef Wolf wrote:
> Jeff Epler schrieb im Artikel <(E-Mail Removed)>:
> > By using parser.disable_interspersed_args() you can make the parsing of

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^

[...]
>
> Aaaahhh, this was the bit of information I was missing. Is it "legal" to
> use this method? It seems not to be documented in the library reference?


I agree, it doesn't seem to be in the documentation. It's been in the
module since at least 2002, when it was called Optik, and has been
suggested many times to people who want to implement "cvs-like"
commandline parsers, according to a few google searches I performed.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFA/nMMJd01MZaTXX0RAkPuAJ9L2ayRtABbS3fAN25ffibDohSXgwC fYlLC
WzJvu8bOnDVOCKATpFdc0x4=
=lX2/
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Josef Wolf
Guest
Posts: n/a
 
      07-23-2004
Jeff Epler schrieb im Artikel <(E-Mail Removed)>:

>> > By using parser.disable_interspersed_args() you can make the parsing of

>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> Aaaahhh, this was the bit of information I was missing. Is it "legal" to
>> use this method? It seems not to be documented in the library reference?


I tried it now and it mostly seems to work as expected. But there are
some cosmetic drawbacks:

1. When called "main.py dump -h" then _first_ the help for the subcommand
is printed and after that the help for the global options are printed.
I think this is because I am doing something very stupid with the
exception?
2. When invalid options are given, only the usage information is printed.
The list of available options is missing. (This is for both, the global
options as well as the subcommand options.)
3. When I replace the call to parser.print_help() in the exception handler
with a "pass" statement, (1.) seems to be fixed, but then no help at
all is printed when no subcommand is given.

Maybe someone could give me hints what I am doing wrong?

(I have attached the script (consiting of three modules: main.py, Opt.py
and Dump.py) at the end of this posting.)

PS: I am very interested in comments how to make things more python-ish.
I am new to python and am probably doing very stupid things

> I agree, it doesn't seem to be in the documentation. It's been in the
> module since at least 2002, when it was called Optik, and has been
> suggested many times to people who want to implement "cvs-like"
> commandline parsers, according to a few google searches I performed.


I have tried to google, but could not find anything. I get either
thousands of hits (alsmost all related to perforce) or nothing at all.
What keywords have you used?


here come the scripts:

#File Opt.py:
import sys
from optparse import OptionParser

class MyOptionParser:
subcommands={}

def __init__(self, mode, callback, help):
self.__parser=OptionParser("%%prog %s [%s-options]" % (mode,mode))
MyOptionParser.subcommands[mode] = {"obj" : self,
"func" : callback,
"help" : help}

def __getattr__(self, name):
return getattr(self.__parser, name)

def global_parser():
global opts, args, subopts, subargs
global subcommand

subcommands = MyOptionParser.subcommands

parser = OptionParser()
parser.disable_interspersed_args()

parser.add_option("-f", help="Foo option.")
parser.add_option("-b", help="Bar option.")

parser.set_usage("%prog [options]" +
" subcommand [subcommand-options]:\n\n" +
"Available subcommands:\n" +
"\n".join([" %-8s %s" % (k,subcommands[k]["help"])
for k in subcommands.keys()]))

(opts, args) = parser.parse_args()

try:
subcommand = subcommands[args[0]]
(subopts, subargs) = subcommand["obj"].parse_args(args[1:])
subcommand["func"](subargs)

except:
parser.print_help()

sys.exit()

global_parser=staticmethod(global_parser)


#File Dump.py:
import Opt

def main(args):
print Opt.opts
print Opt.subopts
print Opt.subargs

def init_main():
opt_dump=Opt.MyOptionParser("dump", main, "Dump database contents.")
opt_dump.add_option("-z", help="Baz option.")
opt_dump.add_option("-b", help="Blah option.")

init_main()


#File main.py:
import os
import sys

import Opt
import Dump

Opt.MyOptionParser.global_parser()


--
Please visit and sign http://petition-eurolinux.org and http://www.ffii.org
-- Josef Wolf -- (E-Mail Removed) --
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Eclipse and CVS, Eclipse wipes out CVS directories xyzzy12@hotmail.com Java 1 08-31-2007 10:19 AM
Eclipse 3.2 and CVS - where and how to get and install CVS? ddog Java 3 08-04-2007 06:41 PM
[ANN] Ruby CVS IRC bot, and Ruby CVS RSS feed David Ross Ruby 5 12-05-2004 03:48 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments