On Tue, 6 Dec 2005, Peter Otten wrote:
Ritesh Raj Sarraf wrote:
I'm using this for "option arguments" which are mutually inclusive.
But I want the user to pass atleast one "option argument" for the program
to function properly.
For example, I have an option "--fetch-update" which requires a file "foo"
to check what it has to fetch. If the file is provided as an argument, it
uses it, else I add a parser.set_defa ults("foo") which makes the program
to look for it in the current working directory.
WHen the program see the "--fetch-update" option, it should execute the
required code. Now how do I check if at least one option has been passed
at the command-line ?
I have multiple options but I have parser.set_defa ults() for each of them.
I'm sorry I don't understand your example. Wouldn't you need at least two
options to demonstrate "mutually inclusive" options? The set_default()
method seems to accept only keyword arguments -- but even it were used
correctly I'm still unclear why you would need it at all.
Perhaps you can post a few sample invocations (both correct and illegal) of
your script together with a description (in English, not code) of how the
script should react?
Peter, puzzled
try:
version = "0.6b"
reldate = "03/10/2005"
copyright = "(C) 2005 Ritesh Raj Sarraf - RESEARCHUT (http://www.researchut. com/)"
#FIXME: Option Parsing
# There's a flaw with either optparse or I'm not well understood with it
# Presently you need to provide all the arguments to it to work.
# No less, no more. This needs to be converted to getopt sometime.
#parser = OptionParser()
#parser = optparse.Option Parser()
parser = optparse.Option Parser(usage="% prog [OPTION1, OPTION2, ...]", version="%prog " + version)
parser.add_opti on("-d","--download-dir", dest="download_ dir", help="Root directory path to save the downloaded files", action="store", type="string")
parser.set_defa ults(download_d ir="foo")
parser.add_opti on("-s","--cache-dir", dest="cache_dir ", help="Root directory path where the pre-downloaded files will be searched. If not, give a period '.'",action="st ore", type="string", metavar=".")
parser.set_defa ults(cache_dir= ".")
#parser.set_def aults(cache_dir =".")
#parser.add_opt ion("-u","--uris", dest="uris_file ", help="Full path of the uris file which contains the main database of files to be downloaded",act ion="store", type="string")
# We'll have additional options
# --set-update - This will extract the list of uris which need to be fetched
# --fetch-update - This will fetch the list of uris which need for update.
# --install-update - This will install the fetched database files
# The same will happen for upgradation.
# --set-upgrade - This will extract the list of uris which need to be fetched
# --fetch-upgrade - This will fetch the list of uris which need for upgrade
# --install-upgrade - This will install the fetched database files
parser.add_opti on("","--set-update", dest="set_updat e", help="Extract the list of uris which need to be fetched for _updation_", action="store", type="string", metavar="foo")
parser.set_defa ults(set_update ="foo")
parser.add_opti on("","--fetch-update", dest="fetch_upd ate", help="Fetch the list of uris which are needed for _updation_.", action="store", type="string", metavar="foo")
parser.set_defa ults(fetch_upda te="foo")
parser.add_opti on("","--install-update", dest="install_u pdate", help="Install the fetched database files ", action="store", type="string", metavar="foo.zi p")
parser.set_defa ults(install_up date="foo.zip")
parser.add_opti on("","--set-upgrade", dest="set_upgra de", help="Extract the list of uris which need to be fetched ", action="store", type="string", metavar="foo.da t")
parser.set_defa ults(set_upgrad e="foo.dat")
parser.add_opti on("","--fetch-upgrade", dest="fetch_upg rade", help="Fetch the list of uris which are needed ", action="store", type="string", metavar="foo.da t")
parser.set_defa ults(fetch_upgr ade="foo.dat")
parser.add_opti on("","--install-upgrade", dest="install_u pgrade", help="Install the fetched packages ", action="store", type="string", metavar="foo-fetched.zip")
parser.set_defa ults(install_ug prade="foofetch ed.zip")
(options, args) = parser.parse_ar gs()
#parser.check_r equired("-d", "-s", "-u")
#if len(arguments) != 2:
# parser.error("E rr! Incorrect number of arguments. Exiting")
if len(options) != 1 or len(options) > 2:
print len(args)
parser.error("N o arguments were passed\n")
sys.exit(1)
elif not options.set_upg rade and options.upgrade _type:
parser.error("O nly options --set-upgrade and --upgrade-type are mutually inclusive\n")
sys.exit(1)
Thanks,
rrs
--
Ritesh Raj Sarraf
RESEARCHUT --
http://www.researchut.com
"Stealing logic from one person is plagiarism, stealing from many is research."
"Necessity is the mother of invention."