代码之家  ›  专栏  ›  技术社区  ›  Richard Neumann

如何重命名clap_derive中的子命令占位符?

  •  1
  • Richard Neumann  · 技术社区  · 3 年前

    如何重命名占位符 <COMMAND> 使用时子命令的 clap_derive ?

    以下是MRE: Cargo.toml

    [package]
    name = "clap_test_subcommand_placeholder"
    version = "0.1.0"
    edition = "2021"
    
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    
    [dependencies]
    clap = { version = "4.4.0", features = ["derive"] }
    

    src/main.rs

    use clap::{Parser, Subcommand};
    
    #[derive(Debug, Parser)]
    #[command(author, version, about, long_about = "Frobnicate the fizzbuzz")]
    struct Args {
        #[arg(index = 1, name = "xml_file", help = "The base XML file to operate on")]
        file: String,
        #[command(subcommand, name = "action")]
        action: Action,
    }
    
    #[derive(Clone, Debug, Subcommand)]
    pub enum Action {
        #[command(long_about = "Add a new type")]
        Add,
        #[command(long_about = "Display the selected type's properties")]
        Find,
    }
    
    fn main() {
        Args::parse();
    }
    

    旗帜 name 被编译器接受,但似乎没有效果:

    〉cargo run                                                                                                                      2023-08-27 11:35:07
       Compiling clap_test_subcommand_placeholder v0.1.0 (/home/rne/clap_test_subcommand_placeholder)
        Finished dev [unoptimized + debuginfo] target(s) in 0.65s
         Running `target/debug/clap_test_subcommand_placeholder`
    Usage: clap_test_subcommand_placeholder <xml_file> <COMMAND>
    
    Commands:
      add   Add a new type
      find  Display the selected type's properties
      help  Print this message or the help of the given subcommand(s)
    
    Arguments:
      <xml_file>  The base XML file to operate on
    
    Options:
      -h, --help     Print help (see more with '--help')
      -V, --version  Print version
    

    它仍然说 <命令> 而不是 <action>

    1 回复  |  直到 3 年前
        1
  •  2
  •   Dogbert    3 年前

    你需要设置 subcommand_value_name = "action" #[command] :

    use clap::{Parser, Subcommand};
    
    #[derive(Debug, Parser)]
    #[command(
        author,
        version,
        about,
        long_about = "Frobnicate the fizzbuzz",
        subcommand_value_name = "action"
    )]
    struct Args {
        #[arg(index = 1, name = "xml_file", help = "The base XML file to operate on")]
        file: String,
        #[command(subcommand, name = "action")]
        action: Action,
    }
    
    #[derive(Clone, Debug, Subcommand)]
    pub enum Action {
        #[command(long_about = "Add a new type")]
        Add,
        #[command(long_about = "Display the selected type's properties")]
        Find,
    }
    
    fn main() {
        Args::parse();
    }
    

    输出:

    Usage: playground <xml_file> <action>
    
    Commands:
      add   Add a new type
      find  Display the selected type's properties
      help  Print this message or the help of the given subcommand(s)
    
    Arguments:
      <xml_file>  The base XML file to operate on
    
    Options:
      -h, --help     Print help (see more with '--help')
      -V, --version  Print version
    

    Playground


    您可以更改 Commands: 在的帮助消息中 Actions: 具有 subcommand_help_heading :

    #[command(
        author,
        version,
        about,
        long_about = "Frobnicate the fizzbuzz",
        subcommand_help_heading = "Actions",
        subcommand_value_name = "action"
    )]
    

    输出:

    Usage: playground <xml_file> <action>
    
    Actions:
      add   Add a new type
      find  Display the selected type's properties
      help  Print this message or the help of the given subcommand(s)
    
    Arguments:
      <xml_file>  The base XML file to operate on
    
    Options:
      -h, --help     Print help (see more with '--help')
      -V, --version  Print version
    
    推荐文章