我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用optparse.OptionError()。
def register(parser, *args, **kwargs): r"""Register an option for the Option Parser provided by Flake8. :param parser: The option parser being used by Flake8 to handle command-line options. :param \*args: Positional arguments that you might otherwise pass to ``add_option``. :param \*\*kwargs: Keyword arguments you might otherwise pass to ``add_option``. """ try: # Flake8 3.x registration parser.add_option(*args, **kwargs) except (optparse.OptionError, TypeError): # Flake8 2.x registration # Pop Flake8 3 parameters out of the kwargs so they don't cause a # conflict. parse_from_config = kwargs.pop('parse_from_config', False) comma_separated_list = kwargs.pop('comma_separated_list', False) normalize_paths = kwargs.pop('normalize_paths', False) # In the unlikely event that the developer has specified their own # callback, let's pop that and deal with that as well. preexisting_callback = kwargs.pop('callback', None) callback = generate_callback_from(comma_separated_list, normalize_paths, preexisting_callback) if callback: kwargs['callback'] = callback kwargs['action'] = 'callback' # We've updated our args and kwargs and can now rather confidently # call add_option. option = parser.add_option(*args, **kwargs) if parse_from_config: parser.config_options.append(option.get_opt_string().lstrip('-'))
def main(): usage = '%s -i <input file> [-o <output file>]' % sys.argv[0] parser = OptionParser(usage=usage, version='0.1') try: parser.add_option('-i', dest='inputFile', help='Input file') parser.add_option('-o', dest='outputFile', help='Output file') (args, _) = parser.parse_args() if not args.inputFile: parser.error('Missing the input file, -h for help') except (OptionError, TypeError), e: parser.error(e) if not os.path.isfile(args.inputFile): print 'ERROR: the provided input file \'%s\' is not a regular file' % args.inputFile sys.exit(1) f = open(args.inputFile, 'r') data = f.read() f.close() if not args.outputFile: args.outputFile = args.inputFile + '.bin' f = open(args.outputFile, 'wb') f.write(safechardecode(data)) f.close()
def main(): usage = '%s [-d] -i <input file> [-o <output file>]' % sys.argv[0] parser = OptionParser(usage=usage, version='0.1') try: parser.add_option('-d', dest='decrypt', action="store_true", help='Decrypt') parser.add_option('-i', dest='inputFile', help='Input file') parser.add_option('-o', dest='outputFile', help='Output file') (args, _) = parser.parse_args() if not args.inputFile: parser.error('Missing the input file, -h for help') except (OptionError, TypeError), e: parser.error(e) if not os.path.isfile(args.inputFile): print 'ERROR: the provided input file \'%s\' is non existent' % args.inputFile sys.exit(1) if not args.decrypt: data = cloak(args.inputFile) else: data = decloak(args.inputFile) if not args.outputFile: if not args.decrypt: args.outputFile = args.inputFile + '_' else: args.outputFile = args.inputFile[:-1] f = open(args.outputFile, 'wb') f.write(data) f.close()
def bgarg(bg_instance): """ ???????? try: opts, args = getopt.getopt(sys.argv[1:], 'hu:m:d:', ['help', 'url=', 'mode=','debug=']) except: usage() url = 'www.baidu.com' mode = 'payload' for o, a in opts: if o in ('-h', '--help'): usage() elif o in ('-u', '--url'): url = a elif o in ('-m', '--mode'): mode = a if mode == 'exploit': bg_instance.exploit() # ??? exploit???????payload????? else: bg_instance.payload() elif o in ('-d', '--debug'): bg_instance.log_level = BGLogLevel.debug """ #??????? parser = OptionParser(usage=usage()) try: parser.add_option("-t", "--target", dest="target", default = '') parser.add_option("-m", "--mode", dest="mode", default = 'payload', help="Exploit vuln or attack it, \"exploit\" for verify and \"payload\" for attack") parser.add_option("-d", "--debug", dest="debug", action="store_true", help="Print debug info") (options, args) = parser.parse_args() bg_instance.option.target['default'] = options.target infos.bginfos(bg_instance) if options.debug == 'debug': bg_instance.log_level = BGLogLevel.debug if options.mode == 'exploit': bg_instance.exploit() # ??? exploit???????payload????? else: bg_instance.payload() except (OptionError, TypeError), e: parser.error(e)
def __init__(self): args = None parser = OptionParser( prog="chirribackup", description="%prog is a cheap and ugly backup tool written with love.", version="%prog 1.0", usage="%s [options] {directory} {action} [action args...]" % sys.argv[0], epilog="Use 'help' action for more information. Other valid commands: %s" \ % " ".join(ActionsManager.action_handlers.keys()) ) parser.add_option("-D", "--debug", dest="verbosity", action="store_const", const="DEBUG", help="Debug verbosity level.") parser.add_option("-l", "--log-file", dest="logfile", default=None, help="Log file") parser.add_option("-v", "--verbosity", type="str", dest="verbosity", default="INFO", help="The log verbosity level. Accepted values: CRITICAL, ERROR, WARNING, INFO (default), DEBUG, DEBUG2") try: (self.options, args) = parser.parse_args() if self.options.logfile is not None: logger.to_file(self.options.logfile) if self.options.verbosity is not None: logger.setLogLevel(self.options.verbosity) if len(args) < 1: parser.error("Target {directory} not specified.") self.options.path = args[0] self.options.args = args[1:] if len(args) == 1 and args[0] == "help": self.options.path = None self.options.args = args elif len(args) == 2 and args[0] == "help" and args[1] == "help": self.options.path = None self.options.args = args else: if not os.path.exists(self.options.path): logger.warning("Target directory '%s' does not exist." % self.options.path) elif not os.path.isdir(self.options.path): parser.error("Target directory '%s' is not a directory." % self.options.path) except (OptionError, TypeError), e: parser.error(e) # Singleton container class