<p>传统的方法看起来更像如下:</p>
<pre><code>def create_parser():
parser = ArgumentParser(description='Parser for Backup / Consistency Check')
parser.add_argument(' database', '-d', dest='db', help='Database name', choices=get_active_database_list())
parser.add_argument(' timeout', '-t', dest='timeout', help='Timeout limit (in minutes)')
subparsers = parser.add_subparsers()
parser_backup = subparsers.add_parser('backup', help='Run a backup')
parser_backup.set_defaults(action='backup') # or even pass the backup function itself, vs a string
parser_backup.add_argument('dest', help='Where to backup to') # where to backup to
parser_check = subparsers.add_parser('consistency_check', help='Run a consistency check')
parser_check.set_defaults(action='consistency_check')
parser_check.add_argument('source', help='What file to check for consistency')
return parser
</code></pre>
<p>…用法为:</p>
^{pr2}$
<p>……或者。。。在</p>
<pre><code># here, action='consistency_check' and source='/path/to/content/to/check'
yourtool -d db -t 15 consistency_check /path/to/content/to/check
</code></pre>