Structopt: Custom about message

Created on 12 Aug 2018  路  6Comments  路  Source: TeXitoi/structopt

Hello all,

Is there any way to customize the about message, I've tried:

lazy_static! {
    static ref ABOUT_MESSAGE_VALUE: &'static str = "custom about message";
}

#[derive(StructOpt)]
#[structopt(name = "custom", author="abc", about = "&ABOUT_MESSAGE_VALUE")]
pub struct Arg {
    #[structopt(name = "verbosity", short = "v")]
    verbosity: u8,
}

But this doesn't work (obviously), it prints:

test 0.1.0
abc
&ABOUT_MESSAGE_VALUE

instead of

test 0.1.0 abc custom about message

Many thanks for any response.

Most helpful comment

You need to deref the lazy_static to get its type, here &str. It might be a bit different if you use a string instead. In this case, raw(about = "ABOUT_MESSAGE_VALUE.as_str()") is the solution:

#[macro_use]
extern crate structopt;
#[macro_use]
extern crate lazy_static;

use structopt::StructOpt;

lazy_static! {
    static ref ABOUT_MESSAGE_VALUE: String = "custom about message".to_string();
}

#[derive(StructOpt, Debug)]
#[structopt(name = "custom", author="abc", raw(about = "ABOUT_MESSAGE_VALUE.as_str()"))]
pub struct Opt {
    #[structopt(name = "verbosity", short = "v")]
    verbosity: u8,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

All 6 comments

about = "texte" or raw(about = "&ABOUT_MESSAGE_VALUE")

Hi @TeXitoi ,

Thanks a lot for your response. I've tried raw(about = "&ABOUT_MESSAGE_VALUE") but the program does not compile, the compiler complains:

error[E0277]: the trait bound `&str: std::convert::From<&ABOUT_MESSAGE_VALUE>` is not satisfied
  --> src/main.rs:94:10
   |
94 | #[derive(StructOpt)]
   |          ^^^^^^^^^ the trait `std::convert::From<&ABOUT_MESSAGE_VALUE>` is not implemented for `&str`
   |
   = note: required because of the requirements on the impl of `std::convert::Into<&str>` for `&ABOUT_MESSAGE_VALUE`

(I cannot use about = "texte" since texte needs to be generated in runtime (this the reason why I've used lazy_static)

#[macro_use]
extern crate structopt;
#[macro_use]
extern crate lazy_static;

use structopt::StructOpt;

lazy_static! {
    static ref ABOUT_MESSAGE_VALUE: &'static str = "custom about message";
}

#[derive(StructOpt, Debug)]
#[structopt(name = "custom", author="abc", raw(about = "*ABOUT_MESSAGE_VALUE"))]
pub struct Opt {
    #[structopt(name = "verbosity", short = "v")]
    verbosity: u8,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

You need to deref the lazy_static to get its type, here &str. It might be a bit different if you use a string instead. In this case, raw(about = "ABOUT_MESSAGE_VALUE.as_str()") is the solution:

#[macro_use]
extern crate structopt;
#[macro_use]
extern crate lazy_static;

use structopt::StructOpt;

lazy_static! {
    static ref ABOUT_MESSAGE_VALUE: String = "custom about message".to_string();
}

#[derive(StructOpt, Debug)]
#[structopt(name = "custom", author="abc", raw(about = "ABOUT_MESSAGE_VALUE.as_str()"))]
pub struct Opt {
    #[structopt(name = "verbosity", short = "v")]
    verbosity: u8,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

@TeXitoi many many thanks, it works now.

Great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liamdawson picture liamdawson  路  5Comments

TedDriggs picture TedDriggs  路  6Comments

rask picture rask  路  8Comments

bb010g picture bb010g  路  9Comments

yoshuawuyts picture yoshuawuyts  路  7Comments