extending optionparser to accept multiple comma delimited input forone arg

C

cmptrwhz

I have read the manual first of all :). on using optionparser for the
input of command line arguments and inputs. I understand everything
there was in the manual except how to extend the parser to accept a
multiple list of input that is comma delimited....

for example:

../convertContacts3.py -i a.vcf,b.vcf,c.vcf,d.vcf

I want to be able to allow multiple filenames for one argument
variable. Here is what I have so far and what I was wondering is how
do I apply this subclass to the -i option so that it will gather the
multiple filename inputs?

...........
from optparse import OptionParser
...........
class MyOption(Option):

ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(self, action, dest, opt, value, values, parser)

def main():
usage = "usage: %prog -i<filename>|-p<pathname> -o<filename> -
d<option> -q -v"
version = "%prog v0.3.000 2009-11-25 -
by .........................................."
description = "This program was designed to take the information
within a vcard and export it's contents to a csv file for easy import
into other address book clients."
parser = OptionParser(usage, version, description)
parser.add_option("-i", "--input", action="store", dest="input_file",
default="None", help="Read data from FILENAME (required if no path
specified)")
parser.add_option("-p", "--path", action="store", dest="input_path",
default="None", help="Process all vcards within specified directory
(required if no filename specified)")
parser.add_option("-o", "--output", action="store",
dest="output_file", default="addrs.csv", help="Name of .csv file to
output too (default is addrs.csv)")
parser.add_option("-d", "--delim", action="", dest="delimiter",
default="\t", help="Delimiter to use: comma, semicolon, newline, tab
(default is tab)")
parser.add_option("-q", "--quote", action="store_true", dest="quote",
default=False, help="Double quote the output strings (default is
off)")
parser.add_option("-v", "--verbose", action="store_false",
dest="verbose", default=True, help="Show processing information
(default is on)")
parser.add_option("--trace", action="store_true", dest="trace",
default=False, help="Displays a ton of debugging information.")

(options, args) = parser.parse_args()
...........
 
P

Peter Otten

cmptrwhz said:
I have read the manual first of all :). on using optionparser for the
input of command line arguments and inputs. I understand everything
there was in the manual except how to extend the parser to accept a
multiple list of input that is comma delimited....

for example:

./convertContacts3.py -i a.vcf,b.vcf,c.vcf,d.vcf

I want to be able to allow multiple filenames for one argument
variable. Here is what I have so far and what I was wondering is how
do I apply this subclass to the -i option so that it will gather the
multiple filename inputs?

from optparse import OptionParser, Option

class MyOption (Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(
self, action, dest, opt, value, values, parser)

parser = OptionParser(option_class=MyOption)
parser.add_option("-i", "--input", action="extend")

options, args = parser.parse_args()
print options

If you had problems understanding the documentation perhaps you can suggest
an improvement.

Peter
 
C

cmptrwhz

If you had problems understanding the documentation perhaps you can suggest
an improvement.

Peter
First I would like to thank you very much for the quick and concise
answer this is very clear to me as to what I need to do now. Thank you
so much.

The one thing that threw me off and confused me is that I went back in
the docs to see what actions are available to see if extend was an
allowable action. in section 14.3.2.4 Other actions, I did not see
that extend was a valid action so I looked thru section 14.3.3.3
Defining options and this section did not show extend as a valid
action. I believe that if there was a complete disclosure of actions
available then I would have seen the error in my thinking. Everything
else made sense up to the point of where the subclass was described in
the docs as above but if the next couple lines you gave me was added
to this section (14.3.5.2 Adding new actions) it was have completed
the confirmation I needed that it was ok to type in extend as an
action. Maybe I am still not understanding this concept fully still
but I hope I am making sense with my point.

So my suggestion would be to include this tidbit of extra as you have
described to me in the section 14.3.5.2 and all would have been as
clear as sunshine. :) thank you again for your time and listening to
my thoughts.

parser = OptionParser(option_class=MyOption)
parser.add_option("-i", "--input", action="extend")

options, args = parser.parse_args()
print options
 
T

Terry Reedy

cmptrwhz said:
First I would like to thank you very much for the quick and concise
answer this is very clear to me as to what I need to do now. Thank you
so much.

The one thing that threw me off and confused me is that I went back in
the docs to see what actions are available to see if extend was an
allowable action. in section 14.3.2.4 Other actions, I did not see
that extend was a valid action so I looked thru section 14.3.3.3
Defining options and this section did not show extend as a valid
action. I believe that if there was a complete disclosure of actions
available then I would have seen the error in my thinking. Everything
else made sense up to the point of where the subclass was described in
the docs as above but if the next couple lines you gave me was added
to this section (14.3.5.2 Adding new actions) it was have completed
the confirmation I needed that it was ok to type in extend as an
action. Maybe I am still not understanding this concept fully still
but I hope I am making sense with my point.

So my suggestion would be to include this tidbit of extra as you have
described to me in the section 14.3.5.2 and all would have been as
clear as sunshine. :) thank you again for your time and listening to
my thoughts.

parser = OptionParser(option_class=MyOption)
parser.add_option("-i", "--input", action="extend")

options, args = parser.parse_args()
print options

Please go to bugs.python.org, register if you have not, and add a type:
feature request, component: documentation issue giving the exact
sentence you suggest adding (keeping in mind the existing doc style) and
the exact proposed location. Then explain, briefly, why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top