{"id":204137,"date":"2025-05-29T13:50:53","date_gmt":"2025-05-29T05:50:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204137\/"},"modified":"2025-05-29T13:50:53","modified_gmt":"2025-05-29T05:50:53","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%86%85%e7%9c%81%e3%80%81%e5%8d%95%e5%87%bb%e5%92%8c%e4%b8%b0%e5%af%8c%e6%a0%bc%e5%bc%8f%e4%b8%ba-python-cli-%e6%9e%84%e5%bb%ba%e4%ba%a4%e4%ba%92%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204137\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u5185\u7701\u3001\u5355\u51fb\u548c\u4e30\u5bcc\u683c\u5f0f\u4e3a Python CLI \u6784\u5efa\u4ea4\u4e92\u5f0f\u804a\u5929"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u5185\u7701\u3001\u5355\u51fb\u548c\u4e30\u5bcc\u683c\u5f0f\u4e3a Python CLI \u6784\u5efa\u4ea4\u4e92\u5f0f\u804a\u5929<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528\u5185\u7701\u3001\u5355\u51fb\u548c\u4e30\u5bcc\u683c\u5f0f\u4e3a Python CLI \u6784\u5efa\u4ea4\u4e92\u5f0f\u804a\u5929\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/17301929936720a6612026b.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5982\u679c\u60a8\u66fe\u7ecf\u60f3\u8ba9\u60a8\u7684 cli \u66f4\u5177\u4ea4\u4e92\u6027\u548c\u52a8\u6001\u6027\uff0c\u6784\u5efa\u5b9e\u65f6\u547d\u4ee4\u4ea4\u4e92\u7cfb\u7edf\u53ef\u80fd\u662f\u7b54\u6848\u3002\u901a\u8fc7\u5229\u7528 python \u7684\u81ea\u7701\u529f\u80fd\u3001\u7528\u4e8e\u7ba1\u7406\u547d\u4ee4\u7684 click \u4ee5\u53ca\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51fa\u7684 rich\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5f3a\u5927\u3001\u7075\u6d3b\u7684 cli\uff0c\u4ee5\u667a\u80fd\u5730\u54cd\u5e94\u7528\u6237\u8f93\u5165\u3002\u60a8\u7684 cli \u53ef\u4ee5\u81ea\u52a8\u53d1\u73b0\u5e76\u6267\u884c\u547d\u4ee4\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u786c\u7f16\u7801\u6bcf\u4e2a\u547d\u4ee4\uff0c\u4ece\u800c\u4f7f\u7528\u6237\u4f53\u9a8c\u66f4\u6d41\u7545\u3001\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<p>\u4e30\u5bcc\u591a\u5f69\u7684\u63a7\u5236\u53f0\u6df7\u4e71\uff1a\u70b9\u51fb\u547d\u4ee4\u4e0e\u4e30\u5bcc\u7684\u8f93\u51fa\u76f8\u9047 &#8211; \u56e0\u4e3a\u5373\u4f7f\u662f\u7ec8\u7aef\u4e5f\u559c\u6b22\u70ab\u8000\u98ce\u683c\uff01<\/p>\n<p>click \u7b80\u5316\u4e86\u547d\u4ee4\u7ba1\u7406\u3001\u53c2\u6570\u89e3\u6790\u548c\u5e2e\u52a9\u751f\u6210\u3002\u5b83\u8fd8\u5141\u8bb8\u8f7b\u677e\u7684\u547d\u4ee4\u7ed3\u6784\u548c\u9009\u9879\u5904\u7406\u3002<\/p>\n<p> rich \u4f7f\u60a8\u80fd\u591f\u76f4\u63a5\u5728\u7ec8\u7aef\u4e2d\u8f93\u51fa\u683c\u5f0f\u7cbe\u7f8e\u7684 markdown\uff0c\u4f7f\u7ed3\u679c\u4e0d\u4ec5\u5b9e\u7528\uff0c\u800c\u4e14\u5177\u6709\u89c6\u89c9\u5438\u5f15\u529b\u3002<\/p>\n<p>\u901a\u8fc7\u5c06\u8fd9\u4e24\u4e2a\u5e93\u4e0e python \u81ea\u7701\u76f8\u7ed3\u5408\uff0c\u60a8\u53ef\u4ee5\u6784\u5efa\u4ea4\u4e92\u5f0f\u804a\u5929\u529f\u80fd\uff0c\u8be5\u529f\u80fd\u53ef\u4ee5\u52a8\u6001\u53d1\u73b0\u548c\u6267\u884c\u547d\u4ee4\uff0c\u540c\u65f6\u4ee5\u4e30\u5bcc\u3001\u53ef\u8bfb\u7684\u683c\u5f0f\u663e\u793a\u8f93\u51fa\u3002 <strong>\u6709\u5173\u5b9e\u9645\u793a\u4f8b\uff0c\u8bf7\u4e86\u89e3 storycraftr \u5982\u4f55\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u7b80\u5316 ai \u9a71\u52a8\u7684\u5199\u4f5c\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/strong> https:\/\/storycraftr.app\u3002<\/p>\n<h4> 1. \u8bbe\u7f6e\u57fa\u672c\u804a\u5929\u547d\u4ee4 <\/h4>\n<p>\u804a\u5929\u547d\u4ee4\u521d\u59cb\u5316\u4f1a\u8bdd\uff0c\u5141\u8bb8\u7528\u6237\u4e0e cli \u4ea4\u4e92\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u6355\u83b7\u7528\u6237\u8f93\u5165\uff0c\u8fd9\u4e9b\u8f93\u5165\u5c06\u52a8\u6001\u6620\u5c04\u5230\u9002\u5f53\u7684 click \u547d\u4ee4\u3002<\/p>\n<pre>import os\nimport click\nimport shlex\nfrom rich.console import console\nfrom rich.markdown import markdown\n\nconsole = console()\n\n@click.command()\n@click.option(\"--project-path\", type=click.path(), help=\"path to the project directory\")\ndef chat(project_path=none):\n    \"\"\"\n    start a chat session with the assistant for the given project.\n    \"\"\"\n    if not project_path:\n        project_path = os.getcwd()\n\n    console.print(\n        f\"starting chat for [bold]{project_path}[\/bold]. type [bold green]exit()[\/bold green] to quit.\"\n    )\n\n    # start the interactive session\n    while true:\n        user_input = console.input(\"[bold blue]you:[\/bold blue] \")\n\n        # handle exit\n        if user_input.lower() == \"exit()\":\n            console.print(\"[bold red]exiting chat...[\/bold red]\")\n            break\n\n        # call the function to handle command execution\n        execute_cli_command(user_input)\n<\/pre>\n<h4> 2. \u81ea\u7701\u4ee5\u53d1\u73b0\u548c\u6267\u884c\u547d\u4ee4 <\/h4>\n<p>\u4f7f\u7528python\u5185\u7701\uff0c\u6211\u4eec\u52a8\u6001\u5730\u53d1\u73b0\u53ef\u7528\u7684\u547d\u4ee4\u5e76\u6267\u884c\u5b83\u4eec\u3002\u8fd9\u91cc\u7684\u4e00\u4e2a\u5173\u952e\u90e8\u5206\u662f click \u547d\u4ee4\u662f\u4fee\u9970\u51fd\u6570\u3002\u4e3a\u4e86\u6267\u884c\u5b9e\u9645\u7684\u903b\u8f91\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u672a\u4fee\u9970\u7684\u51fd\u6570\uff08\u5373\u56de\u8c03\uff09\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5185\u7701\u52a8\u6001\u6267\u884c\u547d\u4ee4\u5e76\u5904\u7406 click \u7684\u88c5\u9970\u5668\uff1a<\/p>\n<pre>import inspect\nimport your_project_cmd  # replace with your actual module containing commands\n\ncommand_modules = {\"project\": your_project_cmd}  # list your command modules here\n\ndef execute_cli_command(user_input):\n    \"\"\"\n    function to execute cli commands dynamically based on the available modules,\n    calling the undecorated function directly.\n    \"\"\"\n    try:\n        # use shlex.split to handle quotes and separate arguments correctly\n        parts = shlex.split(user_input)\n        module_name = parts[0]\n        command_name = parts[1].replace(\"-\", \"_\")  # replace hyphens with underscores\n        command_args = parts[2:]  # keep the rest of the arguments as a list\n\n        # check if the module exists in command_modules\n        if module_name in command_modules:\n            module = command_modules[module_name]\n\n            # introspection: get the function by name\n            if hasattr(module, command_name):\n                cmd_func = getattr(module, command_name)\n\n                # check if it's a click command and strip the decorator\n                if hasattr(cmd_func, \"callback\"):\n                    # call the underlying undecorated function\n                    cmd_func = cmd_func.callback\n\n                # check if it's a callable (function)\n                if callable(cmd_func):\n                    console.print(\n                        f\"executing command from module: [bold]{module_name}[\/bold]\"\n                    )\n\n                    # directly call the function with the argument list\n                    cmd_func(*command_args)\n                else:\n                    console.print(\n                        f\"[bold red]'{command_name}' is not a valid command[\/bold red]\"\n                    )\n            else:\n                console.print(\n                    f\"[bold red]command '{command_name}' not found in {module_name}[\/bold red]\"\n                )\n        else:\n            console.print(f\"[bold red]module {module_name} not found[\/bold red]\")\n    except exception as e:\n        console.print(f\"[bold red]error executing command: {str(e)}[\/bold red]\")\n<\/pre>\n<h4> \u8fd9\u662f\u5982\u4f55\u8fd0\u4f5c\u7684\uff1f <\/h4>\n<ul>\n<li> <strong>\u8f93\u5165\u89e3\u6790<\/strong>\uff1a\u6211\u4eec\u4f7f\u7528 shlex.split \u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7b49\u8f93\u5165\u3002\u8fd9\u53ef\u786e\u4fdd\u6b63\u786e\u5904\u7406\u5e26\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u548c\u7279\u6b8a\u5b57\u7b26\u3002<\/li>\n<li> <strong>\u6a21\u5757\u548c\u547d\u4ee4\u67e5\u627e<\/strong>\uff1a\u8f93\u5165\u5206\u4e3a module_name \u548c command_name\u3002\u547d\u4ee4\u540d\u79f0\u7ecf\u8fc7\u5904\u7406\uff0c\u5c06\u8fde\u5b57\u7b26\u66ff\u6362\u4e3a\u4e0b\u5212\u7ebf\uff0c\u4ee5\u5339\u914d python \u51fd\u6570\u540d\u79f0\u3002<\/li>\n<li> <strong>\u5185\u7701<\/strong>\uff1a\u6211\u4eec\u4f7f\u7528 getattr() \u4ece\u6a21\u5757\u52a8\u6001\u83b7\u53d6\u547d\u4ee4\u51fd\u6570\u3002\u5982\u679c\u662f click \u547d\u4ee4\uff08\u5373\u5177\u6709\u56de\u8c03\u5c5e\u6027\uff09\uff0c\u6211\u4eec\u901a\u8fc7\u5265\u79bb click \u88c5\u9970\u5668\u6765\u8bbf\u95ee\u5b9e\u9645\u7684\u51fd\u6570\u903b\u8f91\u3002<\/li>\n<li> <strong>\u547d\u4ee4\u6267\u884c<\/strong>\uff1a\u4e00\u65e6\u6211\u4eec\u68c0\u7d22\u5230\u672a\u4fee\u9970\u7684\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u4f20\u9012\u53c2\u6570\u5e76\u8c03\u7528\u5b83\uff0c\u5c31\u50cf\u76f4\u63a5\u8c03\u7528 python \u51fd\u6570\u4e00\u6837\u3002<\/li>\n<\/ul>\n<h4> 3. cli \u547d\u4ee4\u793a\u4f8b <\/h4>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u9879\u76ee\u6a21\u5757\u4e2d\u7684\u4e00\u4e9b\u793a\u4f8b\u547d\u4ee4\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u804a\u5929\u4ea4\u4e92\u8c03\u7528\u8fd9\u4e9b\u547d\u4ee4\uff1a<\/p>\n<pre>@click.group()\ndef project():\n    \"\"\"project management cli.\"\"\"\n    pass\n\n@project.command()\ndef init():\n    \"\"\"initialize a new project.\"\"\"\n    console.print(\"[bold green]project initialized![\/bold green]\")\n\n@project.command()\n@click.argument(\"name\")\ndef create(name):\n    \"\"\"create a new component in the project.\"\"\"\n    console.print(f\"[bold cyan]component {name} created.[\/bold cyan]\")\n\n@project.command()\ndef status():\n    \"\"\"check the project status.\"\"\"\n    console.print(\"[bold yellow]all systems operational.[\/bold yellow]\")\n<\/pre>\n<p>\u8fd0\u884c\u4ea4\u4e92\u5f0f\u804a\u5929\u7cfb\u7edf\uff1a<\/p>\n<ol>\n<li>\u786e\u4fdd\u60a8\u7684\u6a21\u5757\uff08\u5982\u9879\u76ee\uff09\u5df2\u5728 command_modules \u4e2d\u5217\u51fa\u3002<\/li>\n<li>\u8fd0\u884c\u547d\u4ee4\uff1a <\/li>\n<\/ol>\n<pre>python your_cli.py chat --project-path \/path\/to\/project\n<\/pre>\n<p>\u4f1a\u8bdd\u5f00\u59cb\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>you: project init you: project create \"homepage\"\n<\/pre>\n<p>\u8f93\u51fa\u5c06\u4f7f\u7528 rich markdown \u4ee5\u683c\u5f0f\u826f\u597d\u7684\u65b9\u5f0f\u663e\u793a\uff1a<\/p>\n<pre>[bold green]Project initialized![\/bold green] [bold cyan]Component Homepage created.[\/bold cyan]\n<\/pre>\n<p>\u901a\u8fc7\u7ed3\u5408 click \u547d\u4ee4\u7ba1\u7406\u3001rich for markdown \u683c\u5f0f\u548c python \u81ea\u7701\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a cli \u6784\u5efa\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u804a\u5929\u7cfb\u7edf\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u60a8\u52a8\u6001\u53d1\u73b0\u548c\u6267\u884c\u547d\u4ee4\uff0c\u540c\u65f6\u4ee5\u4f18\u96c5\u3001\u53ef\u8bfb\u7684\u683c\u5f0f\u5448\u73b0\u8f93\u51fa\u3002<\/p>\n<p><strong>\u4e3b\u8981\u4eae\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u52a8\u6001\u547d\u4ee4\u6267\u884c<\/strong>\uff1a\u5185\u7701\u4f7f\u60a8\u80fd\u591f\u53d1\u73b0\u548c\u8fd0\u884c\u547d\u4ee4\uff0c\u800c\u65e0\u9700\u5bf9\u5176\u8fdb\u884c\u786c\u7f16\u7801\u3002<\/li>\n<li> <strong>\u4e30\u5bcc\u7684\u8f93\u51fa<\/strong>\uff1a\u4f7f\u7528 rich markdown \u53ef\u786e\u4fdd\u8f93\u51fa\u6613\u4e8e\u9605\u8bfb\u4e14\u5177\u6709\u89c6\u89c9\u5438\u5f15\u529b\u3002<\/li>\n<li> <strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u6b64\u8bbe\u7f6e\u5141\u8bb8\u547d\u4ee4\u7ed3\u6784\u548c\u6267\u884c\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u4f7f\u7528\u5185\u7701\u3001\u5355\u51fb\u548c\u4e30\u5bcc\u683c\u5f0f\u4e3a Python CLI \u6784\u5efa\u4ea4\u4e92\u5f0f\u804a\u5929\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u5185\u7701\u3001\u5355\u51fb\u548c\u4e30\u5bcc\u683c\u5f0f\u4e3a &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204137","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204137"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204137\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}