{"id":472,"date":"2025-01-02T23:10:40","date_gmt":"2025-01-02T15:10:40","guid":{"rendered":"https:\/\/cococat.top\/?p=472"},"modified":"2025-01-02T23:10:40","modified_gmt":"2025-01-02T15:10:40","slug":"clap-note","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2025\/01\/02\/clap-note\/","title":{"rendered":"clap \u7b14\u8bb0"},"content":{"rendered":"<h2>1. ValueEnum<\/h2>\n<p>\u5f53\u6211\u4eec\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>clap::clap_derive::ValueEnum<\/code>\u3002<\/p>\n<h3>1.1 derive \u65b9\u5f0f<\/h3>\n<p>\u76f4\u63a5\u4e0a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-rust\">use clap::{clap_derive::ValueEnum, Parser};\n\n#[derive(Debug, Clone, ValueEnum)]\nenum Gender {\n    Male,\n    Female,\n}\n\n#[derive(Parser, Debug)]\n#[command(version, about, long_about = None)]\nstruct Args {\n    \/\/\/ Name of the person to greet\n    #[arg(short, long)]\n    name: String,\n\n    \/\/\/ Gender of the person to greet\n    #[arg(short, long)]\n    gender: Gender,\n}\n\nfn main() {\n    let args = Args::parse();\n    println!(&quot;Hello {}!&quot;, args.name)\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u8fd0\u884c --help \u7684\u7ed3\u679c\u662f\uff1a<\/p>\n<pre><code>Usage: draft --name &lt;NAME&gt; --gender &lt;GENDER&gt;\n\nOptions:\n  -n, --name &lt;NAME&gt;      Name of the person to greet\n  -g, --gender &lt;GENDER&gt;  Gender of the person to greet [possible values: male, female]\n  -h, --help             Print help\n  -V, --version          Print version<\/code><\/pre>\n<p>\u53ef\u89c1\uff0cclap \u81ea\u52a8\u5730\u4e3a\u6211\u4eec\u6807\u6ce8\u51fa\u4e86\u63a5\u53d7\u7684\u53d6\u503c\uff0c\u4e5f\u6b63\u597d\u662f enum \u91cc\u7684\u547d\u540d\u3002<\/p>\n<h3>1.2 builder \u65b9\u5f0f<\/h3>\n<p>\u4f7f\u7528 builder \u65b9\u5f0f\u521b\u5efa\u53c2\u6570\u89e3\u6790\u65f6\uff0c\u8fd8\u9700\u8981\u7528\u5230 <code>clap::value_parser<\/code> \u5b8f\u3002<\/p>\n<pre><code class=\"language-rust\">use clap::{clap_derive::ValueEnum, Arg, Command};\n\n#[derive(Debug, Clone, ValueEnum)]\nenum Gender {\n    Male,\n    Female,\n}\n\nfn main() {\n    let matches = Command::new(&quot;whatever&quot;)\n        .arg(\n            Arg::new(&quot;name&quot;)\n                .short(&#039;n&#039;)\n                .long(&quot;name&quot;)\n                .value_name(&quot;NAME&quot;)\n                .required(true),\n        )\n        .arg(\n            Arg::new(&quot;gender&quot;)\n                .short(&#039;g&#039;)\n                .long(&quot;gender&quot;)\n                .value_name(&quot;GENDER&quot;)\n                .required(true)\n                .value_parser(clap::value_parser!(Gender)),\n        )\n        .get_matches();\n    println!(&quot;Hello {}!&quot;, matches.get_one::&lt;String&gt;(&quot;name&quot;).unwrap())\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\uff1a<\/p>\n<pre><code class=\"language-rust\">Usage: draft --name &lt;NAME&gt; --gender &lt;GENDER&gt;\n\nOptions:\n  -n, --name &lt;NAME&gt;      \n  -g, --gender &lt;GENDER&gt;  [possible values: male, female]\n  -h, --help             Print help<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. ValueEnum \u5f53\u6211\u4eec\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u679a\u4e3e\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 clap::clap_der [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[24],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-rust","tag-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/472\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}