def __init__(self, *args, **kwargs): self.parser = OptionParser(*args, **kwargs) self.add_option("-o", "--show-original", action="store_true", default=False, dest="original", help="show original polygons") self.add_option("-w", "--wireframe", action="store_true", default=False, dest="wireframe", help="draw only the border lines") self.add_option("-c", "--show-clipper", action="store_true", default=False, dest="clipper", help="show clipper polygon as wireframe, on top of clipped") self.add_option("-s", "--show-subject", action="store_true", default=False, dest="subject", help="show subject polygon as wireframe, on top of clipped") self.add_option("-d", "--debug", action="store_true", default=False, dest="debug", help="show debug information on screen") oper = OptionGroup(self.parser, "Available Operations") oper.add_option("--union", action="callback", callback=self.set_operation, dest="operation", help="perform the union of the two polygons: A|B") oper.add_option("--intersection", action="callback", callback=self.set_operation, dest="operation", help="perform the intersection of the two polygons: A&B") oper.add_option("--difference", action="callback", callback=self.set_operation, dest="operation", help="difference between the polygons: A\\B (default)") oper.add_option("--reversed-diff", action="callback", callback=self.set_operation, dest="operation", help="reversed difference between the polygons: B\\A") self.parser.set_defaults(operation="difference") self.parser.add_option_group(oper) over = OptionGroup(self.parser, "Polygon Overrides") over.add_option("--subj-poly", type="string", metavar="POLY", action="callback", callback=self.set_polygon, help="override the vertices for the subject polygon") over.add_option("--clip-poly", type="string", metavar="POLY", action="callback", callback=self.set_polygon, help="override the vertices for the clipper polygon") over.set_description( """This program is provided as a demo, but you can override the pre- defined polygons without editing the file by using these options. POLY needs to be a string with pairs of floats (representing the the x and y coordinates of the vertexes), separated by semi-colons. Example: %s --subj-poly="1.5,1.25;7.5,2.5;4,3;4.5,6.5" """ % sys.argv[0]) self.parser.add_option_group(over)
def main(): from optparse import OptionParser, OptionGroup usage = "%prog [OPTIONS]" description = u"代理IP管理. " parser = OptionParser(usage=usage, description=description) parser.add_option('-t', '--test', action='store_true', help=u"测试") parser.add_option('-i', '--init', action='store_true', help=u'初始化') parser.add_option('-a', '--add', action='store_true', help=u'添加ip或domain') tg = OptionGroup(parser, u"测试") tg.add_option('-f', dest='file', action='store', help=u"保存代理IP的文件") tg = parser.add_option_group(tg) ig = OptionGroup(parser, u"初始化") ig.set_description("-s IPS_FILE -d dm1,dm2 -f FILE OR -p ip1,ip2 -d dm1,dm2 -f FILE") ig.option_list.append(tg.get_option('-f')) ig.add_option('-s', dest='ips_file', action='store', help=u"从文件中获取初始化入库的ip") ig.add_option('-p', dest='ips', action='store', help=u"逗号分割的ip列表,如 221.176.14.72:80,211.144.81.68:18000") ig.add_option('-d', dest='domains', action='store', help=u"逗号分割的domain列表,如 che300,ganji") tg = parser.add_option_group(ig) adg = OptionGroup(parser, u"添加ip或domain") adg.set_description("-p ip1,ip2 -f FILE OR -d dm1,dm2 -f FILE") adg.option_list.append(tg.get_option('-f')) adg.option_list.append(ig.get_option('-p')) adg.option_list.append(ig.get_option('-d')) tg = parser.add_option_group(adg) options, _ = parser.parse_args() def valid_check(mandatories=[]): for m in mandatories: if not options.__dict__[m]: print u"ERROR: 必要元素 {0} 不能为空".format(m) exit(-1) if options.test: valid_check(['file', ]) test_proxy(options.file) elif options.init: valid_check(['domains', 'file']) ipm = IpsManager(options.file) if options.ips_file: with open(options.ips_file) as ipf: ipm.batch_put(ipf.read().split(), options.domains.split(',')) elif options.ips: ipm.batch_put(options.ips.split(','), options.domains.split(',')) else: print u"ERROR: 请输入IP列表" exit(-1) elif options.add: valid_check(['file', ]) ipm = IpsManager(options.file) if options.ips: ipm.put_ip(options.ips.rstrip(',').split(',')) elif options.domains: ipm.put_domain(options.domains.split(',')) else: print u"ERROR: 请输入IP列表或DOMAIN列表" exit(-1) else: print parser.print_help()