python之optparse模块
测试例子
#!/usr/bin/env python2.7 import sys import os from optparse import OptionParser def parse_options(): parser = OptionParser() parser.add_option("-u", "--user", dest="user", default="", help="MySQL user") parser.add_option("-H", "--host", dest="host", default="localhost", help="MySQL host (default: localhost)") parser.add_option("-p", "--password", dest="password", default="", help="MySQL password") parser.add_option("--ask-pass", action="store_true", dest="prompt_password", help="Prompt for password") parser.add_option("-P", "--port", dest="port", type="int", default="3306", help="TCP/IP port (default: 3306)") parser.add_option("-S", "--socket", dest="socket", default="/var/run/mysqld/mysql.sock", help="MySQL socket file. Only applies when host is localhost") parser.add_option("", "--defaults-file", dest="defaults_file", default="", help="Read from MySQL configuration file. Overrides all other options") parser.add_option("-d", "--database", dest="database", help="Database name (required unless table is fully qualified)") parser.add_option("-t", "--table", dest="table", help="Table to alter (optionally fully qualified)") parser.add_option("-g", "--ghost", dest="ghost", help="Table name to serve as ghost. This table will be created and synchronized with the original table") parser.add_option("-a", "--alter", dest="alter_statement", help="Comma delimited ALTER statement details, excluding the 'ALTER TABLE t' itself") parser.add_option("-c", "--chunk-size", dest="chunk_size", type="int", default=1000, help="Number of rows to act on in chunks. Default: 1000") parser.add_option("-l", "--lock-chunks", action="store_true", dest="lock_chunks", default=False, help="Use LOCK TABLES for each chunk") parser.add_option("-N", "--skip-binlog", dest="skip_binlog", action="store_true", default=False, help="Disable binary logging") parser.add_option("-r", "--max-lock-retries", type="int", dest="max_lock_retries", default="10", help="Maximum times to retry on deadlock or lock_wait_timeout. (default: 10; 0 is unlimited)") parser.add_option("--skip-delete-pass", dest="skip_delete_pass", action="store_true", default=False, help="Do not execute the DELETE data pass") parser.add_option("--sleep", dest="sleep_millis", type="int", default=0, help="Number of milliseconds to sleep between chunks. Default: 0") parser.add_option("", "--sleep-ratio", dest="sleep_ratio", type="float", default=0, help="Ratio of sleep time to execution time. Default: 0") parser.add_option("--cleanup", dest="cleanup", action="store_true", default=False, help="Remove custom triggers, ghost table from possible previous runs") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=True, help="Print user friendly messages") parser.add_option("-q", "--quiet", dest="verbose", action="store_false", help="Quiet mode, do not verbose") return parser.parse_args() (options,args) = parse_options() print options,args
Help的截图:
参数值的获取:
print options,args