Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command-line and config file, both use-cases become possible with structopt? #3124

Closed
epage opened this issue Dec 9, 2021 · 3 comments
Closed

Comments

@epage
Copy link
Member

epage commented Dec 9, 2021

Issue by SamuelMarks
Wednesday Jun 05, 2019 at 04:46 GMT
Originally opened as TeXitoi/structopt#197


A few common use-cases:

  • Accept input from config file
  • Accept input from environment variables
  • Accept input from command-line

Then, overriding config file attributes with environment variable or command line.

I'm not sure exactly how to represent this in Rust, but something like:

#[derive(StructOpt, Debug)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
    /// The config file
    #[structopt(short = "c", long = "config", help = "Config file")]
    config_file: Option<Config>,

    #[structopt(flatten, required_unless = "config_file")
    options: Option<Config>,
}

#[derive(StructOpt, Debug)]
struct Config {
    /// A flag, true if used in the command line.
    #[structopt(short = "d", long = "debug", help = "Activate debug mode")]
    debug: bool,

    /// An argument of type float, with a default value.
    #[structopt(short = "s", long = "speed", help = "Set speed", default_value = "42")]
    speed: f64,

    /// Needed parameter, the first on the command line.
    #[structopt(help = "Input file")]
    input: String,

    /// An optional parameter, will be `None` if not present on the
    /// command line.
    #[structopt(help = "Output file, stdout if not present")]
    output: Option<String>,

    /// An optional parameter with optional value, will be `None` if
    /// not present on the command line, will be `Some(None)` if no
    /// argument is provided (i.e. `--log`) and will be
    /// `Some(Some(String))` if argument is provided (e.g. `--log
    /// log.txt`).
    #[structopt(
        long = "log",
        help = "Log file, stdout if no file, no logging if not present"
    )]
    log: Option<Option<String>>,

    /// An optional list of values, will be `None` if not present on
    /// the command line, will be `Some(vec![])` if no argument is
    /// provided (i.e. `--optv`) and will be `Some(Some(String))` if
    /// argument list is provided (e.g. `--optv a b c`).
    #[structopt(long = "optv")]
    optv: Option<Vec<String>>,
}
@epage
Copy link
Member Author

epage commented Dec 9, 2021

Comment by TeXitoi
Wednesday Jun 05, 2019 at 09:15 GMT


See also #72

@epage
Copy link
Member Author

epage commented Dec 9, 2021

Comment by TeXitoi
Wednesday Jun 05, 2019 at 09:41 GMT


Note that clap support environment variable. You can use that:

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(short, long, env = "FILE", default_value = "foo")]
    file: String,
}

if -f or --file is given on the command line, it is used, else if the environment variable FILE is setted, it is used, else the default value "foo" is used.

@epage
Copy link
Member Author

epage commented Dec 9, 2021

I'm closing this in favor of #3113 to track config layering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant