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_defaults("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_defaults() 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.OptionParser() parser = optparse.OptionParser(usage="%prog [OPTION1, OPTION2, ...]", version="%prog " + version) parser.add_option("-d","--download-dir", dest="download_dir", help="Root directory path to save the downloaded files", action="store", type="string") parser.set_defaults(download_dir="foo") parser.add_option("-s","--cache-dir", dest="cache_dir", help="Root directory path where the pre-downloaded files will be searched. If not, give a period '.'",action="store", type="string", metavar=".") parser.set_defaults(cache_dir=".") #parser.set_defaults(cache_dir=".") #parser.add_option("-u","--uris", dest="uris_file", help="Full path of the uris file which contains the main database of files to be downloaded",action="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_option("","--set-update", dest="set_update", help="Extract the list of uris which need to be fetched for _updation_", action="store", type="string", metavar="foo") parser.set_defaults(set_update="foo") parser.add_option("","--fetch-update", dest="fetch_update", help="Fetch the list of uris which are needed for _updation_.", action="store", type="string", metavar="foo") parser.set_defaults(fetch_update="foo") parser.add_option("","--install-update", dest="install_update", help="Install the fetched database files ", action="store", type="string", metavar="foo.zip") parser.set_defaults(install_update="foo.zip") parser.add_option("","--set-upgrade", dest="set_upgrade", help="Extract the list of uris which need to be fetched ", action="store", type="string", metavar="foo.dat") parser.set_defaults(set_upgrade="foo.dat") parser.add_option("","--fetch-upgrade", dest="fetch_upgrade", help="Fetch the list of uris which are needed ", action="store", type="string", metavar="foo.dat") parser.set_defaults(fetch_upgrade="foo.dat") parser.add_option("","--install-upgrade", dest="install_upgrade", help="Install the fetched packages ", action="store", type="string", metavar="foo-fetched.zip") parser.set_defaults(install_ugprade="foofetched.zip") (options, args) = parser.parse_args() #parser.check_required("-d", "-s", "-u") #if len(arguments) != 2: # parser.error("Err! Incorrect number of arguments. Exiting") if len(options) != 1 or len(options) > 2: print len(args) parser.error("No arguments were passed\n") sys.exit(1) elif not options.set_upgrade and options.upgrade_type: parser.error("Only 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." -- http://mail.python.org/mailman/listinfo/python-list