From: cmptrwhz on
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()
...........
From: Peter Otten on
cmptrwhz wrote:

> 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