What is wrong with my code?

My lib.rs
pub const DRAGONSAY: &str = r#"

\ / \ //
\ |___/| / // \
\ /0 0 __ / // | \
\ / / // // | \
@
^@'/ / // | \
//^/ /_ // | \
( //) | /// | \
( / /) | / ) // | \
( // /) '/,
_ / ( ; -. | _ .-~ .-~~~^-.
(( / / )) ,-{ _ -.|.-~-. .~ .
(( // / )) '/\ / ~-. _ .-~ .-~^-.
(( /// )) . { } / \ \ (( / )) .----~-.\ \-' .~ \ . ^-.
///.----..> \ _ -~ . ^- ^-

///-.
_ _ _ _ _ _}^ - - - - ~ ~-- ,.-~
/.-~
"#;

pub const TUXSAY: &str = r#"
\ .--.
\ |o_o |
|:_/ |
// \
(| | )
/'_ /`
_)=(
/
"#;

pub const COWSAY: &str = r#"
\ ^^
\ (oo)_
____
(__)\ )/
||----w |
|| ||
"#;

And my main.rs: use clap::Parser;
use application::{COWSAY, DRAGONSAY, TUXSAY};

#[derive(Parser)]
/// A simple cowsay clone
struct Opts {
/// Sets the animal for the cowsay
#[clap(short, long, default_value = "cow")]
animal: String,

/// Sets the message for the cowsay
#[clap(short, long, default_value = "Moo!")]
message: String,

}

fn prepend_spaces(s: &str, n: usize) -> String {
let mut s = s.to_string();
s.insert_str(0, &" ".repeat(n));
s
}

fn main() {
let opts = Opts::parse();
let Opts { animal, message } = opts;

let art = match animal.to_lowercase().as_str() {
    "dragon" => DRAGONSAY,
    "tux" => TUXSAY,
    "cow" => COWSAY,
    _ => panic!("Unknown animal: {}", message),
};

println!("< {} >", message);
let indent = |s| prepend_spaces(s, message.len() + 4);
let art = art
    .trim()
    .split('\n')
    .map(indent)
    .collect::<Vec<String>>()
    .join("\n");
println!("{art}");

}

And the test gives: thread 'test_program_help_text' panicked at 'Unexpected stdout, failed var.contains(A simple cowsay clone

Usage: application [OPTIONS]

Options:
-a, --animal [default: cow]
-m, --message [default: Moo!]
-h, --help Print help)
├── var: A simple cowsay clone

│ Usage: application [OPTIONS]

│ Options:
│ -a, --animal Sets the animal for the cowsay [default: cow]
│ -m, --message Sets the message for the cowsay [default: Moo!]
│ -h, --help Print help
└── var as str: A simple cowsay clone

Usage: application [OPTIONS]

Options:
  -a, --animal <ANIMAL>    Sets the animal for the cowsay [default: cow]
  -m, --message <MESSAGE>  Sets the message for the cowsay [default: Moo!]
  -h, --help               Print help

command="/app/target/debug/application" "--help"
code=0
stdout=```
A simple cowsay clone

Usage: application [OPTIONS]

Options:
-a, --animal Sets the animal for the cowsay [default: cow]
-m, --message Sets the message for the cowsay [default: Moo!]
-h, --help Print help


stderr=""
', /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/ops/function.rs:251:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace. I have been again stuck for very long :(

I've created a playground from your code and it runs just fine. I don't understand what error you receive from your question, could you please fix the formatting?

If you want help, the least you can do is properly format your post. In it's current state, you are making it difficult to help you. Please see:

2 Likes

This was the excercise: Clap also provides a derive macro Parser that can be used with helper attributes to define the required command line arguments with a struct. The available attributes are listed here. Note that the derive macro does not come with Clap by default so it needs to be explicitly specified as a desired feature in the project Cargo.toml: clap = { version = "4.1.8", features = ["derive"] } .

The starter code contains a struct Opts and code for a cowsay clone program (similar to the ones in the command line argument exercises) that expects the struct to have a parse method that parses command line arguments from the struct.

Derive the trait clap::Parser for the struct Opts in the starter code so that when run with cargo run -- --help , the program prints out

A simple cowsay clone Usage: application [OPTIONS] Options: -a, --animal [default: cow] -m, --message [default: Moo!] -h, --help Print help

In other words, the program should have an about text "A simple cowsay clone", and the options animal and message should have both short and long names and default values as shown in the above help message.

This should be link to working playground: Rust Playground

This should be link to working playground: Rust Playground

This was the excercise: Clap also provides a derive macro Parser that can be used with helper attributes to define the required command line arguments with a struct. The available attributes are listed here. Note that the derive macro does not come with Clap by default so it needs to be explicitly specified as a desired feature in the project Cargo.toml: clap = { version = "4.1.8", features = ["derive"] } .

The starter code contains a struct Opts and code for a cowsay clone program (similar to the ones in the command line argument exercises) that expects the struct to have a parse method that parses command line arguments from the struct.

Derive the trait clap::Parser for the struct Opts in the starter code so that when run with cargo run -- --help , the program prints out

A simple cowsay clone Usage: application [OPTIONS] Options: -a, --animal [default: cow] -m, --message [default: Moo!] -h, --help Print help

In other words, the program should have an about text "A simple cowsay clone", and the options animal and message should have both short and long names and default values as shown in the above help message.