On Sat, Dec 27, 2008 at 2:55 PM, Matt Herzog <m...@blisses.org> wrote: > What I can't figure out now is how to pass a compiled regex to an optparse > option. I'm confused ias to "option" versus "arg" when using the optparse > module. In fact there seems to be no way to define what the arg should be; > only options. Is the arg always implied? I have read several pages on > optparse and am none the wiser.
You want to *retrieve* the regex from the option, not pass it to the option. See below. "args" is the positional arguments - any parameters that aren't prefixed with a switch like "-x". They don't need any configuration. > How do I fix the rx = re.compile('-x') line below so that the string I pass > on the command line gets passed into the re.compile? > > #!/usr/bin/python > import fileinput, sys, string, optparse, re > > #def main(): > optparser = optparse.OptionParser() > optparser.add_option("-x", "--regx", help="regular expression") > # take the first argument out of sys.argv and assign it to searchterm > #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] > (options, args) = optparser.parse_args() > > rx = re.compile('-x') Try rx = re.compile(options.regx) Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor