{"id":1170045,"date":"2025-01-15T16:17:47","date_gmt":"2025-01-15T08:17:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170045.html"},"modified":"2025-01-15T16:17:50","modified_gmt":"2025-01-15T08:17:50","slug":"%e5%a6%82%e4%bd%95%e7%bb%99python%e8%84%9a%e6%9c%ac%e4%bc%a0%e5%8f%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170045.html","title":{"rendered":"\u5982\u4f55\u7ed9python\u811a\u672c\u4f20\u53c2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070844\/e30de113-ba08-47de-820d-f69d06ac0d80.webp\" alt=\"\u5982\u4f55\u7ed9python\u811a\u672c\u4f20\u53c2\" \/><\/p>\n<p><p> \u7ed9Python\u811a\u672c\u4f20\u53c2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709<strong>\u4f7f\u7528sys.argv\u3001\u4f7f\u7528argparse\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982click<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528argparse\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5730\u5b9a\u4e49\u548c\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528argparse\u6a21\u5757\u7ed9Python\u811a\u672c\u4f20\u53c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sys.argv<\/h3>\n<\/p>\n<p><p>sys.argv\u662fPython\u5185\u7f6e\u7684sys\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u5165\u7684\u53c2\u6570\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>    script_name = sys.argv[0]<\/p>\n<p>    arg1 = sys.argv[1]<\/p>\n<p>    arg2 = sys.argv[2]<\/p>\n<p>    print(f&quot;Script Name: {script_name}&quot;)<\/p>\n<p>    print(f&quot;Argument 1: {arg1}&quot;)<\/p>\n<p>    print(f&quot;Argument 2: {arg2}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u65f6\u4f20\u53c2\uff1a<\/p>\n<\/p>\n<p><pre><code>python script.py arg1 arg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528argparse\u6a21\u5757<\/h3>\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\u548c\u9009\u9879\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u53cb\u597d\u7684\u7528\u6237\u754c\u9762\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=&quot;Example script with arguments&quot;)<\/p>\n<p>    parser.add_argument(&quot;arg1&quot;, type=int, help=&quot;First argument&quot;)<\/p>\n<p>    parser.add_argument(&quot;arg2&quot;, type=str, help=&quot;Second argument&quot;)<\/p>\n<p>    parser.add_argument(&quot;--optional&quot;, type=str, default=&quot;default_value&quot;, help=&quot;Optional argument&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    print(f&quot;Argument 1: {args.arg1}&quot;)<\/p>\n<p>    print(f&quot;Argument 2: {args.arg2}&quot;)<\/p>\n<p>    print(f&quot;Optional Argument: {args.optional}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u65f6\u4f20\u53c2\uff1a<\/p>\n<\/p>\n<p><pre><code>python script.py 10 hello --optional optional_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93click<\/h3>\n<\/p>\n<p><p>click\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u547d\u4ee4\u884c\u63a5\u53e3\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u4fbf\u6377\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import click<\/p>\n<p>@click.command()<\/p>\n<p>@click.argument(&#39;arg1&#39;)<\/p>\n<p>@click.argument(&#39;arg2&#39;)<\/p>\n<p>@click.option(&#39;--optional&#39;, default=&#39;default_value&#39;, help=&#39;Optional argument&#39;)<\/p>\n<p>def main(arg1, arg2, optional):<\/p>\n<p>    print(f&quot;Argument 1: {arg1}&quot;)<\/p>\n<p>    print(f&quot;Argument 2: {arg2}&quot;)<\/p>\n<p>    print(f&quot;Optional Argument: {optional}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u65f6\u4f20\u53c2\uff1a<\/p>\n<\/p>\n<p><pre><code>python script.py 10 hello --optional optional_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u4ecb\u7ecdargparse\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efaArgumentParser\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aArgumentParser\u5bf9\u8c61\uff0c\u901a\u5e38\u4f1a\u5728\u811a\u672c\u7684main\u51fd\u6570\u4e2d\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser = argparse.ArgumentParser(description=&quot;Description of your script&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6dfb\u52a0\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528add_argument\u65b9\u6cd5\u4e3a\u811a\u672c\u5b9a\u4e49\u53c2\u6570\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u4f4d\u7f6e\u53c2\u6570\u6216\u53ef\u9009\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f4d\u7f6e\u53c2\u6570\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser.add_argument(&quot;arg1&quot;, type=int, help=&quot;First argument&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u53ef\u9009\u53c2\u6570\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parser.add_argument(&quot;--optional&quot;, type=str, default=&quot;default_value&quot;, help=&quot;Optional argument&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u89e3\u6790\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u8c03\u7528parse_args\u65b9\u6cd5\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u53c2\u6570\u7684Namespace\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">args = parser.parse_args()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Namespace\u5bf9\u8c61\u7684\u5c5e\u6027\u8bbf\u95ee\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(f&quot;Argument 1: {args.arg1}&quot;)<\/p>\n<p>print(f&quot;Optional Argument: {args.optional}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\uff1a\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528argparse\u5b9e\u73b0\u7684\u7b80\u5355\u8ba1\u7b97\u5668\u811a\u672c\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u64cd\u4f5c\u7b26\u6267\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u8fd0\u7b97\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=&quot;Simple calculator&quot;)<\/p>\n<p>    parser.add_argument(&quot;num1&quot;, type=float, help=&quot;First number&quot;)<\/p>\n<p>    parser.add_argument(&quot;num2&quot;, type=float, help=&quot;Second number&quot;)<\/p>\n<p>    parser.add_argument(&quot;operator&quot;, type=str, choices=[&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;\/&quot;], help=&quot;Operator&quot;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    if args.operator == &quot;+&quot;:<\/p>\n<p>        result = args.num1 + args.num2<\/p>\n<p>    elif args.operator == &quot;-&quot;:<\/p>\n<p>        result = args.num1 - args.num2<\/p>\n<p>    elif args.operator == &quot;*&quot;:<\/p>\n<p>        result = args.num1 * args.num2<\/p>\n<p>    elif args.operator == &quot;\/&quot;:<\/p>\n<p>        result = args.num1 \/ args.num2<\/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>\u8fd0\u884c\u65f6\u4f20\u53c2\uff1a<\/p>\n<\/p>\n<p><pre><code>python calculator.py 10 5 +<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7sys.argv\u3001argparse\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93click\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed9Python\u811a\u672c\u4f20\u53c2\u3002<strong>argparse\u6a21\u5757<\/strong>\u7531\u4e8e\u5176\u529f\u80fd\u5f3a\u5927\u4e14\u4f7f\u7528\u7b80\u5355\uff0c\u662f\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u9996\u9009\u65b9\u6cd5\u3002\u4f7f\u7528argparse\u6a21\u5757\u65f6\uff0c\u9996\u5148\u521b\u5efaArgumentParser\u5bf9\u8c61\uff0c\u7136\u540e\u6dfb\u52a0\u53c2\u6570\uff0c\u89e3\u6790\u53c2\u6570\uff0c\u6700\u540e\u4f7f\u7528\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3aPython\u811a\u672c\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570\u652f\u6301\uff0c\u63d0\u5347\u811a\u672c\u7684\u7075\u6d3b\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u547d\u4ee4\u884c\u4e2d\u7ed9Python\u811a\u672c\u4f20\u9012\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u811a\u672c\u540d\u79f0\u540e\u9762\u6dfb\u52a0\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u547d\u4ee4 <code>python script.py arg1 arg2<\/code> \u4f20\u9012\u4e24\u4e2a\u53c2\u6570 <code>arg1<\/code> \u548c <code>arg2<\/code>\u3002\u5728\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>sys.argv<\/code> \u5217\u8868\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\uff0c\u5176\u4e2d <code>sys.argv[0]<\/code> \u662f\u811a\u672c\u540d\u79f0\uff0c\u540e\u7eed\u7684\u7d22\u5f15\u5bf9\u5e94\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n<p><strong>Python\u811a\u672c\u4e2d\u5982\u4f55\u5904\u7406\u4f20\u9012\u7684\u53c2\u6570\uff1f<\/strong><br \/>\u5904\u7406\u4f20\u9012\u7684\u53c2\u6570\u53ef\u4ee5\u4f7f\u7528 <code>sys<\/code> \u6a21\u5757\u6216 <code>argparse<\/code> \u6a21\u5757\u3002<code>sys.argv<\/code> \u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u53c2\u6570\u83b7\u53d6\u529f\u80fd\uff0c\u800c <code>argparse<\/code> \u6a21\u5757\u5219\u5141\u8bb8\u5b9a\u4e49\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u548c\u5e2e\u52a9\u4fe1\u606f\uff0c\u4f7f\u811a\u672c\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u6613\u7528\u6027\u3002\u4f7f\u7528 <code>argparse<\/code> \u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u53c2\u6570\u89e3\u6790\u3002<\/p>\n<p><strong>\u4f20\u9012\u53c2\u6570\u65f6\u5982\u4f55\u786e\u4fdd\u53c2\u6570\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u4f20\u9012\u53c2\u6570\u7684\u6709\u6548\u6027\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u6761\u4ef6\u68c0\u67e5\uff0c\u4f8b\u5982\u9a8c\u8bc1\u53c2\u6570\u7684\u6570\u91cf\u3001\u7c7b\u578b\u548c\u8303\u56f4\u3002\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u5b9a\u4e49\u53c2\u6570\u7c7b\u578b\u548c\u5fc5\u8981\u6027\uff0c\u8fd9\u6837\u5728\u547d\u4ee4\u884c\u8fd0\u884c\u65f6\uff0c\u7528\u6237\u4f1a\u6536\u5230\u81ea\u52a8\u751f\u6210\u7684\u5e2e\u52a9\u4fe1\u606f\uff0c\u63d0\u793a\u4ed6\u4eec\u5982\u4f55\u6b63\u786e\u8f93\u5165\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7ed9Python\u811a\u672c\u4f20\u53c2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528sys.argv\u3001\u4f7f\u7528argparse\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982 [&hellip;]","protected":false},"author":3,"featured_media":1170054,"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\/1170045"}],"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=1170045"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170045\/revisions"}],"predecessor-version":[{"id":1170058,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170045\/revisions\/1170058"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170054"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}