{"id":1089868,"date":"2025-01-08T13:57:17","date_gmt":"2025-01-08T05:57:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089868.html"},"modified":"2025-01-08T13:57:19","modified_gmt":"2025-01-08T05:57:19","slug":"python%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e5%91%bd%e4%bb%a4%e8%a1%8c%e8%be%93%e5%85%a5%e5%8f%82%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089868.html","title":{"rendered":"python\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202607\/bb06b71d-ac59-4265-ac61-3cd8718755c0.webp\" alt=\"python\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570\" \/><\/p>\n<p><p> <strong>Python\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528sys.argv\u3001argparse\u6a21\u5757\u3001click\u5e93\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/strong> \u5176\u4e2d\uff0cargparse\u6a21\u5757\u662f\u5b98\u65b9\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528sys.argv<\/p>\n<\/p>\n<p><p>sys.argv\u662f\u4e00\u4e2a\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5217\u8868\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u7684\u540d\u79f0\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\uff0c\u4f46\u9700\u8981\u624b\u52a8\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    if len(sys.argv) &lt; 2:<\/p>\n<p>        print(&quot;Usage: script.py &lt;param1&gt; &lt;param2&gt; ...&quot;)<\/p>\n<p>        sys.exit(1)<\/p>\n<p>    # \u83b7\u53d6\u53c2\u6570<\/p>\n<p>    param1 = sys.argv[1]<\/p>\n<p>    param2 = sys.argv[2]<\/p>\n<p>    # \u6253\u5370\u53c2\u6570<\/p>\n<p>    print(f&quot;Param1: {param1}&quot;)<\/p>\n<p>    print(f&quot;Param2: {param2}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528sys.argv\u867d\u7136\u7b80\u5355\uff0c\u4f46\u7f3a\u4e4f\u7075\u6d3b\u6027\uff0c\u4e14\u9700\u8981\u624b\u52a8\u5904\u7406\u53c2\u6570\u7684\u9a8c\u8bc1\u548c\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528argparse\u6a21\u5757<\/p>\n<\/p>\n<p><p>argparse\u662fPython\u6807\u51c6\u5e93\u4e2d\u4e13\u95e8\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u3001\u5e2e\u52a9\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def main():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&#39;A simple argparse example&#39;)<\/p>\n<p>    # \u6dfb\u52a0\u53c2\u6570<\/p>\n<p>    parser.add_argument(&#39;param1&#39;, type=str, help=&#39;The first parameter&#39;)<\/p>\n<p>    parser.add_argument(&#39;param2&#39;, type=int, help=&#39;The second parameter&#39;)<\/p>\n<p>    # \u89e3\u6790\u53c2\u6570<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    # \u6253\u5370\u53c2\u6570<\/p>\n<p>    print(f&quot;Param1: {args.param1}&quot;)<\/p>\n<p>    print(f&quot;Param2: {args.param2}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528argparse\u6a21\u5757\u80fd\u591f\u81ea\u52a8\u751f\u6210\u5e2e\u52a9\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u53c2\u6570\u9a8c\u8bc1\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f\uff0cargparse\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528click\u5e93<\/p>\n<\/p>\n<p><p>click\u5e93\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e13\u6ce8\u4e8e\u521b\u5efa\u7f8e\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u547d\u4ee4\u884c\u754c\u9762\u3002\u76f8\u6bd4\u4e8eargparse\uff0c\u5b83\u66f4\u7b80\u5355\u3001\u66f4\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p>@click.command()<\/p>\n<p>@click.argument(&#39;param1&#39;)<\/p>\n<p>@click.argument(&#39;param2&#39;, type=int)<\/p>\n<p>def main(param1, param2):<\/p>\n<p>    # \u6253\u5370\u53c2\u6570<\/p>\n<p>    print(f&quot;Param1: {param1}&quot;)<\/p>\n<p>    print(f&quot;Param2: {param2}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>click\u5e93\u63d0\u4f9b\u4e86\u88c5\u9970\u5668\u8bed\u6cd5\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002\u5bf9\u4e8e\u559c\u6b22\u7b80\u6d01\u4ee3\u7801\u7684\u5f00\u53d1\u8005\uff0cclick\u5e93\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9002\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u811a\u672c<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528sys.argv\u3002\u5b83\u4e0d\u9700\u8981\u989d\u5916\u7684\u4f9d\u8d56\uff0c\u4e14\u80fd\u591f\u5feb\u901f\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/li>\n<li><strong>\u590d\u6742\u811a\u672c<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u811a\u672c\u6216\u9700\u8981\u8be6\u7ec6\u5e2e\u52a9\u4fe1\u606f\u7684\u7a0b\u5e8f\uff0cargparse\u662f\u6700\u4f73\u9009\u62e9\u3002\u5b83\u529f\u80fd\u4e30\u5bcc\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002<\/li>\n<li><strong>\u7f8e\u89c2\u754c\u9762<\/strong>\uff1a\u5982\u679c\u4f60\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u7f8e\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u547d\u4ee4\u884c\u754c\u9762\uff0cclick\u5e93\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u793a\u4f8b\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528argparse\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u8be5\u5de5\u5177\u80fd\u591f\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u6839\u636e\u53c2\u6570\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p>def main():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&#39;A simple calculator&#39;)<\/p>\n<p>    parser.add_argument(&#39;operation&#39;, choices=[&#39;add&#39;, &#39;subtract&#39;], help=&#39;The operation to perform&#39;)<\/p>\n<p>    parser.add_argument(&#39;a&#39;, type=int, help=&#39;The first number&#39;)<\/p>\n<p>    parser.add_argument(&#39;b&#39;, type=int, help=&#39;The second number&#39;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    if args.operation == &#39;add&#39;:<\/p>\n<p>        result = add(args.a, args.b)<\/p>\n<p>    elif args.operation == &#39;subtract&#39;:<\/p>\n<p>        result = subtract(args.a, args.b)<\/p>\n<p>    print(f&quot;Result: {result}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6307\u5b9a\u64cd\u4f5c\u7c7b\u578b\u548c\u4e24\u4e2a\u64cd\u4f5c\u6570\u3002argparse\u6a21\u5757\u4f1a\u81ea\u52a8\u751f\u6210\u5e2e\u52a9\u4fe1\u606f\uff0c\u5e76\u9a8c\u8bc1\u53c2\u6570\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>Python\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528sys.argv\u3001argparse\u6a21\u5757\u3001click\u5e93<\/strong>\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528sys.argv\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u811a\u672c\uff0cargparse\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u7f8e\u89c2\u7684\u547d\u4ee4\u884c\u754c\u9762\uff0cclick\u5e93\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002\u901a\u8fc7<code>sys.argv<\/code>\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u8bbf\u95ee\u4f20\u9012\u7ed9\u811a\u672c\u7684\u6240\u6709\u53c2\u6570\u3002<code>sys.argv[0]<\/code>\u662f\u811a\u672c\u7684\u540d\u79f0\uff0c\u800c\u540e\u9762\u7684\u5143\u7d20\u5219\u662f\u60a8\u8f93\u5165\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u6267\u884c\u547d\u4ee4<code>python script.py arg1 arg2<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>sys.argv[1]<\/code>\u8bbf\u95ee<code>arg1<\/code>\uff0c<code>sys.argv[2]<\/code>\u8bbf\u95ee<code>arg2<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u7c7b\u578b\u8f6c\u6362\uff1f<\/strong><br \/>\u547d\u4ee4\u884c\u53c2\u6570\u901a\u5e38\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u4f20\u9012\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5982\u679c\u671f\u671b\u63a5\u6536\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u6700\u597d\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u8f93\u5165\u9519\u8bef\u800c\u5d29\u6e83\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7b80\u5316\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5904\u7406\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u5e93\uff0c\u6bd4\u5982<code>argparse<\/code>\u548c<code>click<\/code>\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>argparse<\/code>\u5141\u8bb8\u60a8\u5b9a\u4e49\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u548c\u5e2e\u52a9\u4fe1\u606f\uff0c\u800c<code>click<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7b80\u6d01\u7684\u88c5\u9970\u5668\u98ce\u683c\u7684API\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u547d\u4ee4\u884c\u5de5\u5177\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528sys.argv\u3001argparse\u6a21\u5757\u3001click\u5e93\uff0c\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1089874,"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\/1089868"}],"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=1089868"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089868\/revisions"}],"predecessor-version":[{"id":1089877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089868\/revisions\/1089877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089874"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}