{"id":10008247,"date":"2023-09-07T20:58:39","date_gmt":"2023-09-07T12:58:39","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008247"},"modified":"2023-09-07T20:58:39","modified_gmt":"2023-09-07T12:58:39","slug":"%e6%96%87%e7%9b%98rust-%e8%b5%b7%e6%89%8b%e5%bc%8f%ef%bc%8ccli%e7%a8%8b%e5%ba%8f-%e4%ba%ac%e4%b8%9c%e4%ba%91%e6%8a%80%e6%9c%af%e5%9b%a2%e9%98%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008247","title":{"rendered":"\u6587\u76d8Rust\u2014\u2014\u8d77\u624b\u5f0f\uff0cCLI\u7a0b\u5e8f | \u4eac\u4e1c\u4e91\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<p>\u6280\u672f\u7684\u5b66\u4e60\u4ece\u4e0d\u4f1a\u5230\u4f1a\u7684\u8fc7\u7a0b\u662f\u6700\u6709\u610f\u601d\u7684\uff0c\u4e5f\u662f\u4f53\u4f1a\u6700\u591a\u7684\u3002\u4e00\u65e6\u719f\u7ec3\u4e86\uff0c\u77e5\u8bc6\u53d8\u6210\u4e86\u5e38\u8bc6\uff0c\u53ef\u80fd\u5c31\u5931\u53bb\u4e86\u8bb0\u5f55\u5b66\u4e60\u8fc7\u7a0b\u7684\u6700\u4f73\u65f6\u673a\u3002<\/p>\n<p>\u5728\u6211\u770b\u6765\u5b66\u4e60\u4e00\u95e8\u8ba1\u7b97\u673a\u8bed\u8a00\u548c\u5b66\u4e60\u4eba\u7c7b\u8bed\u8a00\u6709\u5f88\u591a\u5171\u901a\u4e4b\u5904\u3002\u6211\u4eec\u5b66\u4e60\u4eba\u7c7b\u8bed\u8a00\u662f\u4ece\u5355\u4e2a\u7684\u8bcd\u5f00\u59cb\uff0c\u7136\u540e\u662f\u7b80\u5355\u53e5\u5b50\uff0c\u901a\u8fc7\u4e0d\u65ad\u7684\u4e0e\u4ed6\u4eba\u4ea4\u4e92\u7ec3\u4e60\u638c\u63e1\u8bed\u6cd5\u548c\u8bed\u8a00\u4e60\u60ef\u3002\u5f53\u719f\u7ec3\u5230\u4e00\u5b9a\u7a0b\u5ea6\u5c31\u53ef\u4ee5\u8868\u8fbe\u601d\u60f3\u3002\u8ba1\u7b97\u7684\u8bed\u8a00\u4e5f\u5dee\u4e0d\u591a\uff0c\u719f\u6089\u5173\u952e\u8bcd\uff0c\u57fa\u672c\u903b\u8f91\uff0c\u6807\u51c6\u5e93\uff0c\u5199\u5e94\u7528\u3002\u53ea\u662f\u6c9f\u901a\u7684\u5bf9\u8c61\u662f\u673a\u5668\u800c\u5df2\u3002<\/p>\n<p>\u65e2\u7136\u662f\u5b66\u5c31\u4e0d\u80fd\u5728\u5f00\u59cb\u641e\u7684\u592a\u96be\u3002\u5b66\u4e60\u672c\u6765\u5c31\u662f\u4e2a\u8270\u82e6\u7684\u5dee\u4e8b\u3002\u4e0a\u6765\u5c31\u5e72\u7279\u522b\u590d\u6742\u7684\u4e8b\u60c5\u5f80\u5f80\u4f1a\u575a\u6301\u4e0d\u4e0b\u53bb\u3002\u5929\u4e0b\u96be\u4e8b\u5fc5\u505a\u4e8e\u6613\uff0c\u4ece\u7b80\u5165\u7e41\uff0c\u4ece\u6613\u5230\u96be\uff0c\u65b9\u4e3a\u6b63\u9053\u3002<\/p>\n<p>\u5148\u804a\u804a\u6700\u7b80\u5355\u7684CLI(Command Line Interface)\u7a0b\u5e8f\u3002\u5176\u5b9e\u6211\u4eec\u6bcf\u5b66\u4e60\u4e00\u95e8\u8bed\u8a00\u7684 hello world \u7a0b\u5e8f\u5c31\u662fCLI\uff0c\u53ea\u662f\u6ca1\u90a3\u4e48\u591a\u4ea4\u4e92\u800c\u5df2\u3002<\/p>\n<p>\u505a\u547d\u4ee4\u884c\u7a0b\u5e8f\u6700\u7e41\u7410\u7684\u4e8b\u60c5\u662f\u5904\u7406\u4ea4\u4e92\u3002\u4ea4\u4e92\u5927\u4f53\u5206\u4e24\u79cd\u3002\u4e00\u79cd\u662f\u6211\u4eec\u6700\u719f\u6089shell\u4e0b\u7684\u4ea4\u4e92\u6a21\u5f0f\uff0c\u6bcf\u6b21\u4e00\u4e2a\u547d\u4ee4\uff0c\u914d\u5408\u53c2\u6570\u5b9e\u73b0\u4e00\u6b21\u5904\u7406\u8fd4\u56de\u4e00\u7ec4\u7ed3\u679c\u3002\u8fd9\u79cd\u6a21\u5f0f\u5904\u7406\u8d77\u6765\u6bd4\u8f83\u5bb9\u6613Rust\u4e5f\u6709\u76f8\u5f53\u4f18\u79c0\u7684\u7b2c\u4e09\u65b9lib (<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=t5xcqKL%2BsuStvsk7VVdgRw%3D%3D.K8Z1o9wgRdQw7KPrRyI1HqoOsMEcyJPoG%2FV%2Fl0MZxxQ%3D\" rel=\"noopener noreferrer\">clap<\/a>)\u3002\u7b2c\u4e8c\u79cd\u662f\u9886\u57df\u4ea4\u4e92\uff0c\u5c31\u50cf\u6211\u662f\u4f7f\u7528MySql\u6216\u8005redis\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002\u8fd9\u79cd\u7a0b\u5e8f\u53ef\u4ee5\u73a9\u513f\u7684\u4e1c\u897f\u5c31\u6bd4\u8f83\u591a\u4e86\u50cf\u5982\u4f55\u5b9e\u73b0\u4ea4\u4e92\uff0c\u5982\u4f55\u6765\u505a\u5b50\u547d\u4ee4\u7684\u63d0\u793a\u3002\u8fd9\u4e9b\u4e1c\u897f clap \u5e76\u6ca1\u6709\u63d0\u4f9b\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u6765\u5b9e\u73b0\u3002<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=J4Dd2RBtZN25I1gNBtOGJQ%3D%3D.aLjmEQxMEF08M5dk%2FOkGHj%2F1VSpGU82FgZ0xmE5aoHz9wjGc1Nnar7zz3J0zTGWt\" rel=\"noopener noreferrer\">interactcli-rs<\/a>\u662f\u6211\u5728\u5de5\u4f5c\u8fc7\u7a0b\u4e2d\u505a\u7684\u4e00\u4e2a\u4ea4\u4e92\u6a21\u5f0f\u547d\u4ee4\u884c\u811a\u624b\u67b6\u3002\u5b9e\u73b0\u4e86\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u901a\u8fc7\u51e0\u4e2a\u6b65\u9aa4\u5feb\u901f\u7684\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u76f8\u5bf9\u9f50\u5168\u7684CLI\u7a0b\u5e8f\u3002\u548c\u505a\u996d\u4e00\u6837\uff0c\u80fd\u591f\u5feb\u901f\u83b7\u5f97\u6210\u5c31\u611f\u7684\u65b9\u5f0f\u662f\u627e\u534a\u6210\u54c1\u76f4\u63a5\u4e0b\u9505\u7092\u4e00\u76d8\uff1a\uff09\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5177\u4f53\u770b\u770b\uff0c\u5982\u4f55\u901a\u8fc7interactcli-rs\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\u9f50\u5168\u7684\u547d\u4ee4\u884c\u7a0b\u5e8f<\/p>\n<h2>\u6765\u70b9\u611f\u6027\u8ba4\u8bc6<\/h2>\n<p>\u5148\u628a\u9879\u76eeclone\u4e0b\u6765\u8fd0\u884c\u4e2a\u4f8b\u5b50<\/p>\n<ul>\n<li>\n<p>clone \u9879\u76ee<\/p>\n<pre><code>git clone https:\/\/github.com\/jiashiwen\/interactcli-rs.git\ncd interactcli-rs\n\n\n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u884c\u6a21\u5f0f<\/p>\n<pre><code>cargo run requestsample baidu\n\n\n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<pre><code>cargo run -- -i\ninteract-rs&gt; requestsample baidu\n\n\n\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u547d\u4ee4\u662f\u901a\u8fc7http\u6765\u8bf7\u6c42\u767e\u5ea6<\/p>\n<h2>\u56db\u6b65\u505a\u4e2aCLI<\/h2>\n<p>\u9996\u5148\u6211\u4eec\u5148\u6765\u770b\u770b\u6846\u67b6\u7684\u76ee\u5f55\u7ed3\u6784<\/p>\n<pre><code>\n.\n\u251c\u2500\u2500 examples\n\u251c\u2500\u2500 log\n\u251c\u2500\u2500 logs\n\u2514\u2500\u2500 src\n    \u251c\u2500\u2500 cmd\n    \u251c\u2500\u2500 commons\n    \u251c\u2500\u2500 configure\n    \u251c\u2500\u2500 interact\n    \u251c\u2500\u2500 logger\n    \u2514\u2500\u2500 request\n\n\n\n<\/code><\/pre>\n<p>cmd\u76ee\u5f55\u662f\u6211\u4eec\u505a\u81ea\u5df1\u529f\u80fd\u65f6\u8981\u52a8\u7684\u4e3b\u8981\u76ee\u5f55\uff0c\u4e0b\u9762\u6211\u4eec\u4e00\u6b65\u4e00\u6b65\u7684\u5b9e\u73b0requestsample\u547d\u4ee4\u3002<\/p>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u547d\u4ee4  <br \/>cmd \u6a21\u5757\u7528\u4e8e\u5b9a\u4e49\u547d\u4ee4\u4ee5\u53ca\u76f8\u5173\u5b50\u547d\u4ee4,<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=Ho6cbEA2cjgmIInP2u7I2w%3D%3D.WyCdz1sl4m9zwxoArBwgC2nQVegoPbqsJUrN6uLUNqk%3D\" rel=\"noopener noreferrer\">requestsample.rs<\/a> \u4e2d\u5b9a\u4e49\u4e86\u8bbf\u95ee\u767e\u5ea6\u7684\u547d\u4ee4<\/p>\n<pre><code>use clap::Command;\n\npub fn new_requestsample_cmd() -&gt; Command&lt;'static&gt; {\nclap::Command::new(&quot;requestsample&quot;)\n.about(&quot;requestsample&quot;)\n.subcommand(get_baidu_cmd())\n}\n\npub fn get_baidu_cmd() -&gt; Command&lt;'static&gt; {\nclap::Command::new(&quot;baidu&quot;).about(&quot;request www.baidu.com&quot;)\n}\n\n\n\n<\/code><\/pre>\n<p>new\/_requestsample\/_cmd \u51fd\u6570\u5b9a\u4e49\u4e86\u547d\u4ee4 &#8220;requestsample&#8221;,get\/_baidu\/_cmd \u51fd\u6570\u5b9a\u4e49\u4e86 requestsample \u7684\u5b50\u547d\u4ee4 baidu<\/p>\n<\/li>\n<li>\n<p>\u6ce8\u518c\u547d\u4ee4  <br \/>src\/cmd\/rootcmd.rs \u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u547d\u4ee4\u6811\uff0c\u53ef\u4ee5\u5728\u6b64\u6ce8\u518c\u5b9a\u4e49\u597d\u7684\u5b50\u547d\u4ee4<\/p>\n<pre><code>lazy_static! {\n    static ref CLIAPP: clap::Command&lt;'static&gt; = clap::Command::new(&quot;interact-rs&quot;)\n        .version(&quot;1.0&quot;)\n        .author(&quot;Your Name. &quot;)\n        .about(&quot;command line sample&quot;)\n        .arg_required_else_help(true)\n        .arg(\n            Arg::new(&quot;config&quot;)\n                .short('c')\n                .long(&quot;config&quot;)\n                .value_name(&quot;FILE&quot;)\n                .help(&quot;Sets a custom config file&quot;)\n                .takes_value(true)\n        )\n        .arg(\n            Arg::new(&quot;daemon&quot;)\n                .short('d')\n                .long(&quot;daemon&quot;)\n                .help(&quot;run as daemon&quot;)\n        )\n        .arg(\n            Arg::new(&quot;interact&quot;)\n                .short('i')\n                .long(&quot;interact&quot;)\n                .conflicts_with(&quot;daemon&quot;)\n                .help(&quot;run as interact mod&quot;)\n        )\n        .arg(\n            Arg::new(&quot;v&quot;)\n                .short('v')\n                .multiple_occurrences(true)\n                .takes_value(true)\n                .help(&quot;Sets the level of verbosity&quot;)\n        )\n        .subcommand(new_requestsample_cmd())\n        .subcommand(new_config_cmd())\n        .subcommand(new_multi_cmd())\n        .subcommand(new_task_cmd())\n        .subcommand(new_loop_cmd())\n        .subcommand(\n            clap::Command::new(&quot;test&quot;)\n                .about(&quot;controls testing features&quot;)\n                .version(&quot;1.3&quot;)\n                .author(&quot;Someone E. &quot;)\n                .arg(\n                    Arg::new(&quot;debug&quot;)\n                        .short('d')\n                        .help(&quot;print debug information verbosely&quot;)\n                )\n        );\n    static ref SUBCMDS: Vec = subcommands();\n}\n\npub fn run_app() {\n    let matches = CLIAPP.clone().get_matches();\n    if let Some(c) = matches.value_of(&quot;config&quot;) {\n        println!(&quot;config path is:{}&quot;, c);\n        set_config_file_path(c.to_string());\n    }\n    set_config(&amp;get_config_file_path());\n    cmd_match(&amp;matches);\n}\n\npub fn run_from(args: Vec) {\n    match clap_Command::try_get_matches_from(CLIAPP.to_owned(), args.clone()) {\n        Ok(matches) =&gt; {\n            cmd_match(&amp;matches);\n        }\n        Err(err) =&gt; {\n            err.print().expect(&quot;Error writing Error&quot;);\n        }\n    };\n}\n\n\n\n\n<\/code><\/pre>\n<p>\u5b9a\u4e49\u597d\u7684\u547d\u4ee4\u4e0d\u9700\u5176\u4ed6\u5904\u7406\uff0c\u6846\u67b6\u4f1a\u5728\u7cfb\u7edf\u8fd0\u884c\u65f6\u751f\u6210\u5b50\u547d\u4ee4\u6811\uff0c\u7528\u4e8e\u5728\u9886\u57df\u4ea4\u4e92\u6a21\u5f0f\u4e0b\u547d\u4ee4\u63d0\u793a\u7684\u652f\u6301<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u89e3\u6790  <br \/>src\/cmd\/rootcmd.rs \u4e2d\u7684 cmd_match \u8d1f\u8d23\u89e3\u6790\u547d\u4ee4\uff0c\u53ef\u4ee5\u628a\u89e3\u6790\u903b\u8f91\u5199\u5728\u8be5\u51fd\u6570\u4e2d<\/p>\n<pre><code>fn cmd_match(matches: &amp;ArgMatches) {   \n  if let Some(ref matches) = matches.subcommand_matches(&quot;requestsample&quot;) {\n      if let Some(_) = matches.subcommand_matches(&quot;baidu&quot;) {\n          let rt = tokio::runtime::Runtime::new().unwrap();\n          let async_req = async {\n              let result = req::get_baidu().await;\n              println!(&quot;{:?}&quot;, result);\n          };\n          rt.block_on(async_req);\n      };\n  }\n}\n\n\n\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4fee\u6539\u4ea4\u4e92\u6a21\u5f0f\u7684\u547d\u4ee4\u63d0\u793a  <br \/>\u63d0\u793a\u7b26\u53ef\u4ee5\u5728src\/interact\/cli.rs \u4e2d\u5b9a\u4e49<\/p>\n<pre><code>pub fn run() {\n\n  ...\n\n  loop {\n      let p = format!(&quot;{}&gt; &quot;, &quot;interact-rs&quot;);\n      rl.helper_mut().expect(&quot;No helper&quot;).colored_prompt = format!(&quot;\/x1b[1;32m{}\/x1b[0m&quot;, p);\n\n      ...\n  }\n\n  ...\n}\n\n\n\n\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u5148\u5199\u5230\u8fd9\u91cc\uff0c\u4e0b\u6b21\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0binteractcli-rs\u5404\u79cd\u529f\u80fd\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002<\/p>\n<blockquote>\n<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u79d1\u6280\u00a0\u8d3e\u4e16\u95fb<\/p>\n<p>\u6765\u6e90\uff1a\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\u793e\u533a \u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u6e90<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044191010\">https:\/\/segmentfault.com\/a\/1190000044191010<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6280\u672f\u7684\u5b66\u4e60\u4ece\u4e0d\u4f1a\u5230\u4f1a\u7684\u8fc7\u7a0b\u662f\u6700\u6709\u610f\u601d\u7684\uff0c\u4e5f\u662f\u4f53\u4f1a\u6700\u591a\u7684\u3002\u4e00\u65e6\u719f\u7ec3\u4e86\uff0c\u77e5\u8bc6\u53d8\u6210\u4e86\u5e38\u8bc6\uff0c\u53ef\u80fd\u5c31\u5931\u53bb\u4e86\u8bb0\u5f55\u5b66\u4e60\u8fc7\u7a0b\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008247","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008247"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008247\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}