{"id":1080525,"date":"2025-01-08T12:30:58","date_gmt":"2025-01-08T04:30:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080525.html"},"modified":"2025-01-08T12:31:01","modified_gmt":"2025-01-08T04:31:01","slug":"python%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080525.html","title":{"rendered":"python\u5982\u4f55\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183023\/9a709153-0064-4b43-88ae-65dd2c1c39a9.webp\" alt=\"python\u5982\u4f55\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\" \/><\/p>\n<p><p> <strong>Python\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u6709argparse\u3001sys.argv\u3001click\u3001optparse<\/strong>\uff0c\u5176\u4e2d<strong>argparse<\/strong>\u6700\u4e3a\u5e38\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd<strong>argparse<\/strong>\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001ARGPARSE\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>argparse\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u7f16\u5199\u7528\u6237\u53cb\u597d\u7684\u547d\u4ee4\u884c\u63a5\u53e3\uff0c\u5e76\u81ea\u52a8\u751f\u6210\u5e2e\u52a9\u548c\u4f7f\u7528\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5bfc\u5165argparse\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165argparse\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u521b\u5efaArgumentParser\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aArgumentParser\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5e76\u8d1f\u8d23\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser = argparse.ArgumentParser(description=&quot;\u8fd9\u662f\u4e00\u4e2a\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u793a\u4f8b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u6dfb\u52a0\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86ArgumentParser\u5bf9\u8c61\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5411\u5176\u4e2d\u6dfb\u52a0\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528add_argument\u65b9\u6cd5\u6765\u6dfb\u52a0\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser.add_argument(&quot;name&quot;, type=str, help=&quot;\u7528\u6237\u7684\u540d\u5b57&quot;)<\/p>\n<p>parser.add_argument(&quot;--age&quot;, type=int, help=&quot;\u7528\u6237\u7684\u5e74\u9f84&quot;, required=False)<\/p>\n<p>parser.add_argument(&quot;--gender&quot;, type=str, choices=[&quot;male&quot;, &quot;female&quot;], help=&quot;\u7528\u6237\u7684\u6027\u522b&quot;, required=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e09\u4e2a\u53c2\u6570\uff1aname\u3001age\u548cgender\u3002\u5176\u4e2d\uff0cname\u662f\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u5fc5\u987b\u63d0\u4f9b\uff1bage\u548cgender\u662f\u53ef\u9009\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>4\u3001\u89e3\u6790\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6dfb\u52a0\u5b8c\u53c2\u6570\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528parse_args\u65b9\u6cd5\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">args = parser.parse_args()<\/p>\n<p>print(args)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5\u3001\u4f7f\u7528\u89e3\u6790\u540e\u7684\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u89e3\u6790\u540e\u7684\u53c2\u6570\u4f1a\u4ee5\u5c5e\u6027\u7684\u5f62\u5f0f\u5b58\u50a8\u5728args\u5bf9\u8c61\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u8fd9\u4e9b\u5c5e\u6027\u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(f&quot;\u7528\u6237\u7684\u540d\u5b57\u662f\uff1a{args.name}&quot;)<\/p>\n<p>if args.age:<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u5e74\u9f84\u662f\uff1a{args.age}&quot;)<\/p>\n<p>if args.gender:<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u6027\u522b\u662f\uff1a{args.gender}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001SYS.ARGV\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>sys.argv\u662fPython\u4e2d\u6700\u7b80\u5355\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5bfc\u5165sys\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165sys\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>sys.argv\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u7684\u540d\u79f0\uff0c\u540e\u9762\u7684\u5143\u7d20\u662f\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">args = sys.argv<\/p>\n<p>print(args)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eesys.argv\u5217\u8868\u7684\u5143\u7d20\u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if len(args) &gt; 1:<\/p>\n<p>    name = args[1]<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u540d\u5b57\u662f\uff1a{name}&quot;)<\/p>\n<p>if len(args) &gt; 2:<\/p>\n<p>    age = args[2]<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u5e74\u9f84\u662f\uff1a{age}&quot;)<\/p>\n<p>if len(args) &gt; 3:<\/p>\n<p>    gender = args[3]<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u6027\u522b\u662f\uff1a{gender}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001CLICK\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>click\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u547d\u4ee4\u884c\u63a5\u53e3\u3002\u5b83\u7684\u8bed\u6cd5\u6bd4argparse\u66f4\u52a0\u7b80\u5355\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5click\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5click\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install click<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u5bfc\u5165click\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165click\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u521b\u5efa\u547d\u4ee4\u884c\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528@click.command\u88c5\u9970\u5668\u6765\u521b\u5efa\u4e00\u4e2a\u547d\u4ee4\u884c\u63a5\u53e3\uff0c\u5e76\u4f7f\u7528@click.argument\u548c@click.option\u88c5\u9970\u5668\u6765\u6dfb\u52a0\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@click.command()<\/p>\n<p>@click.argument(&#39;name&#39;)<\/p>\n<p>@click.option(&#39;--age&#39;, type=int, help=&#39;\u7528\u6237\u7684\u5e74\u9f84&#39;)<\/p>\n<p>@click.option(&#39;--gender&#39;, type=click.Choice([&#39;male&#39;, &#39;female&#39;]), help=&#39;\u7528\u6237\u7684\u6027\u522b&#39;)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(name, age, gender):<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u540d\u5b57\u662f\uff1a{name}&quot;)<\/p>\n<p>    if age:<\/p>\n<p>        print(f&quot;\u7528\u6237\u7684\u5e74\u9f84\u662f\uff1a{age}&quot;)<\/p>\n<p>    if gender:<\/p>\n<p>        print(f&quot;\u7528\u6237\u7684\u6027\u522b\u662f\uff1a{gender}&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u8fd0\u884c\u547d\u4ee4\u884c\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u811a\u672c\u6765\u4f7f\u7528\u547d\u4ee4\u884c\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py name --age 25 --gender male<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001OPTPARSE\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>optparse\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u867d\u7136optparse\u5728\u529f\u80fd\u4e0a\u4e0eargparse\u7c7b\u4f3c\uff0c\u4f46\u5b83\u7684\u4f7f\u7528\u65b9\u5f0f\u66f4\u52a0\u590d\u6742\uff0c\u56e0\u6b64\u5df2\u7ecf\u88ab\u5f03\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u5bfc\u5165optparse\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165optparse\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from optparse import OptionParser<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u521b\u5efaOptionParser\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aOptionParser\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u547d\u4ee4\u884c\u53c2\u6570\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5e76\u8d1f\u8d23\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser = OptionParser()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u6dfb\u52a0\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86OptionParser\u5bf9\u8c61\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5411\u5176\u4e2d\u6dfb\u52a0\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528add_option\u65b9\u6cd5\u6765\u6dfb\u52a0\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser.add_option(&quot;-n&quot;, &quot;--name&quot;, dest=&quot;name&quot;, help=&quot;\u7528\u6237\u7684\u540d\u5b57&quot;)<\/p>\n<p>parser.add_option(&quot;-a&quot;, &quot;--age&quot;, dest=&quot;age&quot;, type=&quot;int&quot;, help=&quot;\u7528\u6237\u7684\u5e74\u9f84&quot;)<\/p>\n<p>parser.add_option(&quot;-g&quot;, &quot;--gender&quot;, dest=&quot;gender&quot;, type=&quot;choice&quot;, choices=[&quot;male&quot;, &quot;female&quot;], help=&quot;\u7528\u6237\u7684\u6027\u522b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u89e3\u6790\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6dfb\u52a0\u5b8c\u53c2\u6570\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528parse_args\u65b9\u6cd5\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">(options, args) = parser.parse_args()<\/p>\n<p>print(options)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5\u3001\u4f7f\u7528\u89e3\u6790\u540e\u7684\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u89e3\u6790\u540e\u7684\u53c2\u6570\u4f1a\u4ee5\u5c5e\u6027\u7684\u5f62\u5f0f\u5b58\u50a8\u5728options\u5bf9\u8c61\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u8fd9\u4e9b\u5c5e\u6027\u6765\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(f&quot;\u7528\u6237\u7684\u540d\u5b57\u662f\uff1a{options.name}&quot;)<\/p>\n<p>if options.age:<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u5e74\u9f84\u662f\uff1a{options.age}&quot;)<\/p>\n<p>if options.gender:<\/p>\n<p>    print(f&quot;\u7528\u6237\u7684\u6027\u522b\u662f\uff1a{options.gender}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528argparse\u3001sys.argv\u3001click\u548coptparse\u7b49\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u6211\u4eec\u7684\u7a0b\u5e8f\u4e2d\u3002\u867d\u7136sys.argv\u662f\u6700\u7b80\u5355\u7684\u89e3\u6790\u65b9\u6cd5\uff0c\u4f46\u5b83\u7684\u529f\u80fd\u8f83\u4e3a\u6709\u9650\uff1bargparse\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff1bclick\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u5f00\u53d1\u547d\u4ee4\u884c\u63a5\u53e3\uff1boptparse\u867d\u7136\u5728\u529f\u80fd\u4e0a\u4e0eargparse\u7c7b\u4f3c\uff0c\u4f46\u5df2\u7ecf\u88ab\u5f03\u7528\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u89e3\u6790\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528argparse\u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>argparse\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5de5\u5177\u3002\u4f7f\u7528argparse\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9a\u4e49\u6240\u9700\u7684\u53c2\u6570\u3001\u9009\u9879\u548c\u5e2e\u52a9\u4fe1\u606f\u3002\u9996\u5148\uff0c\u5bfc\u5165argparse\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aArgumentParser\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528add_argument()\u65b9\u6cd5\u6dfb\u52a0\u53c2\u6570\uff0c\u6700\u540e\u8c03\u7528parse_args()\u65b9\u6cd5\u89e3\u6790\u547d\u4ee4\u884c\u8f93\u5165\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import argparse\n\nparser = argparse.ArgumentParser(description=&#39;\u793a\u4f8b\u7a0b\u5e8f&#39;)\nparser.add_argument(&#39;--name&#39;, type=str, help=&#39;\u60a8\u7684\u540d\u5b57&#39;)\nargs = parser.parse_args()\n\nprint(f&#39;\u4f60\u597d, {args.name}!&#39;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u5165<code>--name<\/code>\u53c2\u6570\u3002<\/p>\n<p><strong>\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u7684\u5e38\u89c1\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u65f6\uff0c\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u672a\u63d0\u4f9b\u5fc5\u9700\u7684\u53c2\u6570\u3001\u53c2\u6570\u7c7b\u578b\u4e0d\u5339\u914d\u4ee5\u53ca\u62fc\u5199\u9519\u8bef\u3002\u4f7f\u7528argparse\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6erequired=True\u6765\u6307\u5b9a\u5fc5\u9700\u53c2\u6570\uff0c\u8fd9\u6837\u5982\u679c\u7528\u6237\u672a\u63d0\u4f9b\u53c2\u6570\uff0c\u7a0b\u5e8f\u4f1a\u663e\u793a\u9519\u8bef\u4fe1\u606f\u5e76\u9000\u51fa\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u907f\u514d\u53c2\u6570\u7c7b\u578b\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528type\u53c2\u6570\u5f3a\u5236\u8981\u6c42\u8f93\u5165\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5e2e\u52a9\u4fe1\u606f\u4ee5\u4fbf\u66f4\u597d\u5730\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>argparse\u6a21\u5757\u81ea\u52a8\u751f\u6210\u5e2e\u52a9\u4fe1\u606f\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u6dfb\u52a0<code>-h<\/code>\u6216<code>--help<\/code>\u53c2\u6570\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u663e\u793a\u6240\u6709\u53ef\u7528\u9009\u9879\u53ca\u5176\u8bf4\u660e\u3002\u901a\u8fc7\u5728ArgumentParser\u5bf9\u8c61\u4e2d\u6dfb\u52a0description\u548cepilog\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u4e0a\u4e0b\u6587\u548c\u4f7f\u7528\u6307\u5357\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u60a8\u7684\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u6709argparse\u3001sys.argv\u3001click\u3001optpars [&hellip;]","protected":false},"author":3,"featured_media":1080539,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080525"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1080525"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080525\/revisions"}],"predecessor-version":[{"id":1080540,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080525\/revisions\/1080540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080539"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}