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

Default value that panics only if neither arg nor env provided #5091

Closed
2 tasks done
SamuelMarks opened this issue Aug 26, 2023 · 2 comments
Closed
2 tasks done

Default value that panics only if neither arg nor env provided #5091

SamuelMarks opened this issue Aug 26, 2023 · 2 comments
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@SamuelMarks
Copy link

SamuelMarks commented Aug 26, 2023

Please complete the following tasks

Clap Version

4.4.0

Describe your use case

use clap::{Parser, CommandFactory};

use const_format::concatcp;
use clap::Parser;

#[derive(Parser)]
struct Cli {
    #[arg(long, env = "ROOT", default_value_t = String::from(concatcp!(
    (
    (if cfg!(windows) {
    std::env::var("UserProfile")
    } else {
    std::env::var("HOME")
    })
    .unwrap()
    ),
    std::path::MAIN_SEPARATOR_STR, "stuff",
    std::path::MAIN_SEPARATOR_STR, "name")))]
    root: String,
}

Describe the solution you'd like

Given a system without a home directory (e.g., could not be obtained from std::env::home_dir, dirs, directories, or other crate), it should first:

  • If arg is provided, use this
  • Else if env var is provided, use this
  • Finally, panic (if no home dir is available in environment) xor use home dir (if available)

Alternatives, if applicable

No response

Additional Context

No response

@SamuelMarks SamuelMarks added the C-enhancement Category: Raise on the bar on expectations label Aug 26, 2023
@epage
Copy link
Member

epage commented Aug 27, 2023

First, I'm going to reinterpret "panic" as "error" seeing as we'd refer not to panic for user errors.

This runs a bit counter to the meaning of a default value. My first reaction is that this should be handled in the code outside of clap because its so outside of the common case. The second thought I had is you could allow for ~/ in the user input (even though most shells will expand it for you) and a value_parser could then expand it and error if there isn't a home directory. If neither works, I would lean towards #3476. Overall we have other stuff tracking that though, so I'm going to close in favor of one of these existing options.

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
@SamuelMarks
Copy link
Author

Sounds good I'll keep an eye on that issue. In the meantime I've put $HOME as placeholder and later on in my code I'll validate that if the value still startswith $HOME then look for homedir and panic if not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants