{"id":49654,"date":"2024-12-03T15:52:48","date_gmt":"2024-12-03T07:52:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49654\/"},"modified":"2024-12-03T15:52:48","modified_gmt":"2024-12-03T07:52:48","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a7%a3%e6%9e%90%e5%91%bd%e4%bb%a4%e6%89%8b%e5%86%8c%e4%b8%ad%e7%9a%84%e5%91%bd%e4%bb%a4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49654\/","title":{"rendered":"\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f <\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/1730294510672232eeaf140.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u89e3\u6790\u547d\u4ee4\u624b\u518c\u547d\u4ee4\u7684python\u5e93<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528python\u8bc6\u522b\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff0c\u5e76\u5c06\u5b83\u4eec\u89e3\u6790\u6210\u6613\u4e8e\u673a\u5668\u7406\u89e3\u7684\u5f62\u5f0f\uff1f<\/p>\n<p>\u547d\u4ee4\u624b\u518c\u901a\u5e38\u5305\u542b\u5305\u542b\u547d\u4ee4\u53ca\u5176\u53c2\u6570\u7684\u6587\u672c\u3002\u4eba\u7c7b\u53ef\u4ee5\u8f7b\u677e\u7406\u89e3\u8fd9\u79cd\u683c\u5f0f\uff0c\u4f46\u5bf9\u4e8e\u673a\u5668\u6765\u8bf4\u5374\u5e76\u975e\u6613\u4e8b\u3002<\/p>\n<p><strong>docopt \u5e93<\/strong><\/p>\n<p>python\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a docopt \u7684\u5e93\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u96be\u9898\u3002\u5b83\u662f\u4e00\u4e2a\u901a\u7528\u547d\u4ee4\u884c\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u5c06\u81ea\u7136\u8bed\u8a00\u7684\u547d\u4ee4\u624b\u518c\u8f6c\u6362\u4e3a\u4ee3\u7801\u3002<\/p>\n<p>docopt \u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<ol>\n<li>\u63d0\u4f9b\u5305\u542b\u547d\u4ee4\u624b\u518c\u6587\u672c\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u4f7f\u7528 docopt.docopt() \u51fd\u6570\u89e3\u6790\u6587\u672c\u3002<\/li>\n<\/ol>\n<p>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u89e3\u6790\u7ed3\u679c\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u547d\u4ee4\u53c2\u6570\uff0c\u503c\u662f\u8f93\u5165\u503c\u7684\u5217\u8868\u6216\u5176\u4ed6\u5b57\u5178\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u547d\u4ee4\uff1a<\/p>\n<pre>auto-sync {startup-config | config-register | bootvar | standard}<\/pre>\n<p>docopt \u4f1a\u8fd4\u56de\u4ee5\u4e0b\u5b57\u5178\uff1a<\/p>\n<pre>{\n    'auto-sync': True,\n    'arg': 'startup-config'\n}<\/pre>\n<p>\u8fd9\u610f\u5473\u7740\u547d\u4ee4\u662f auto-sync\uff0c\u53c2\u6570\u662f startup-config\u3002docopt \u5c06\u81ea\u52a8\u5f3a\u5236\u6267\u884c\u547d\u4ee4\u884c\u8bed\u6cd5\uff0c\u786e\u4fdd\u53c2\u6570\u6b63\u786e\u7ec4\u5408\u3002<\/p>\n<p><strong>\u5176\u4ed6\u53ef\u7528\u5e93<\/strong><\/p>\n<p>\u9664\u4e86 docopt\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b python \u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u624b\u518c\u547d\u4ee4\uff1a<\/p>\n<ul>\n<li><strong>argparse<\/strong>\uff1apython \u6807\u51c6\u5e93\u4e2d\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\u3002\u5b83\u53ef\u4ee5\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4f46\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u547d\u4ee4\u624b\u518c\u53ef\u80fd\u4e0d\u591f\u5f3a\u5927\u3002<\/li>\n<li><strong>commando<\/strong>\uff1a\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\uff0c\u63d0\u4f9b\u7c7b\u4f3c\u4e8e docopt \u7684\u529f\u80fd\uff0c\u4f46\u5177\u6709\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u3002<\/li>\n<li><strong>typer<\/strong>\uff1a\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa cli \u5e94\u7528\u7a0b\u5e8f\u548c\u547d\u4ee4\u884c\u7684\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f \u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 \u89e3\u6790\u547d\u4ee4\u624b\u518c\u547d\u4ee4\u7684python\u5e93 \u5982\u4f55\u4f7f\u7528python\u8bc6\u522b\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff0c\u5e76\u5c06\u5b83\u4eec\u89e3\u6790\u6210\u6613\u4e8e\u673a\u5668\u7406\u89e3\u7684\u5f62\u5f0f\uff1f \u547d\u4ee4\u624b\u518c\u901a\u5e38\u5305\u542b\u5305\u542b\u547d\u4ee4\u53ca\u5176\u53c2\u6570\u7684\u6587\u672c\u3002\u4eba\u7c7b\u53ef\u4ee5\u8f7b\u677e\u7406\u89e3\u8fd9\u79cd\u683c\u5f0f\uff0c\u4f46\u5bf9\u4e8e\u673a\u5668\u6765\u8bf4\u5374\u5e76\u975e\u6613\u4e8b\u3002 docopt \u5e93 python\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a docopt \u7684\u5e93\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u96be\u9898\u3002\u5b83\u662f\u4e00\u4e2a\u901a\u7528\u547d\u4ee4\u884c\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u5c06\u81ea\u7136\u8bed\u8a00\u7684\u547d\u4ee4\u624b\u518c\u8f6c\u6362\u4e3a\u4ee3\u7801\u3002 docopt \u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff1a \u63d0\u4f9b\u5305\u542b\u547d\u4ee4\u624b\u518c\u6587\u672c\u7684\u5b57\u7b26\u4e32\u3002 \u4f7f\u7528 docopt.docopt() \u51fd\u6570\u89e3\u6790\u6587\u672c\u3002 \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u89e3\u6790\u7ed3\u679c\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u547d\u4ee4\u53c2\u6570\uff0c\u503c\u662f\u8f93\u5165\u503c\u7684\u5217\u8868\u6216\u5176\u4ed6\u5b57\u5178\u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5bf9\u4e8e\u547d\u4ee4\uff1a auto-sync {startup-config | config-register | bootvar | standard} docopt \u4f1a\u8fd4\u56de\u4ee5\u4e0b\u5b57\u5178\uff1a { &#8216;auto-sync&#8217;: True, &#8216;arg&#8217;: &#8216;startup-config&#8217; } \u8fd9\u610f\u5473\u7740\u547d\u4ee4\u662f auto-sync\uff0c\u53c2\u6570\u662f startup-config\u3002docopt \u5c06\u81ea\u52a8\u5f3a\u5236\u6267\u884c\u547d\u4ee4\u884c\u8bed\u6cd5\uff0c\u786e\u4fdd\u53c2\u6570\u6b63\u786e\u7ec4\u5408\u3002 \u5176\u4ed6\u53ef\u7528\u5e93 \u9664\u4e86 docopt\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b python \u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u624b\u518c\u547d\u4ee4\uff1a argparse\uff1apython \u6807\u51c6\u5e93\u4e2d\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\u3002\u5b83\u53ef\u4ee5\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4f46\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u547d\u4ee4\u624b\u518c\u53ef\u80fd\u4e0d\u591f\u5f3a\u5927\u3002 commando\uff1a\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684\u547d\u4ee4\u884c\u89e3\u6790\u5668\uff0c\u63d0\u4f9b\u7c7b\u4f3c\u4e8e docopt \u7684\u529f\u80fd\uff0c\u4f46\u5177\u6709\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u3002 typer\uff1a\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa cli \u5e94\u7528\u7a0b\u5e8f\u548c\u547d\u4ee4\u884c\u7684\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u7684\u6a21\u5757\u3002 \u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u7528Python\u89e3\u6790\u547d\u4ee4\u624b\u518c\u4e2d\u7684\u547d\u4ee4\uff1f \u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49654","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49654"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49654\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}