Fix Command line arguments incorrectly echoed in TRACE#899
Fix Command line arguments incorrectly echoed in TRACE#899roderickvd merged 4 commits intolibrespot-org:devfrom JasonLG1979:arg_parse_fixup
Conversation
|
Any particular reason why you are parsing My 0.2c, if you are going through all this trouble to define these constants, why not make it more structured and use |
Because you can't tell the difference between options and flags in getopts. You can ofc call |
|
Another option I was looking into is making arrays of struct OptFlag<'a> {
short_name: &'a str,
long_name: &'a str,
desc: &'a str,
}
struct OptOption<'a> {
short_name: &'a str,
long_name: &'a str,
desc: &'a str,
hint: &'a str,
}
...
const OPT_FLAGS: &[OptFlag] = &[
...
];
const OPT_OPTIONS: &[OptOption] = &[
...
];
for flag in OPT_FLAGS {
opts.optflag(
flag.short_name,
flag.long_name,
flag.desc,
);
}
for option in OPT_OPTIONS {
opts.optopt(
option.short_name,
option.long_name,
option.desc,
option.hint,
);
}After that it's trivial to tell if something is an option or a flag and to get all the fields for the thing as neither getopts nor clap is capable of doing that as far as I can tell. From what I can tell once you dump the info into either you're pretty much on your own. I believe the above approach kinda goes toward the "smart data structures, dumb code" direction. |
|
@ashthespy I looked at clap and other than the let get_value = |arg: &str| -> String {
let arg = strip_arg(arg);
if matches!(
arg.as_str(),
PASSWORD | PASSWORD_SHORT | USERNAME | USERNAME_SHORT
) {
"XXXXXXXX".to_string()
} else if VALID_OPTIONS.contains(&&*arg) {
matches.opt_str(&arg).unwrap_or_else(|| "".to_string())
} else {
"".to_string()
}
};The |
|
I agree that |
So what do you want me to do then? Do you want me to rewrite with clap? Do you want to leave it as-is or do you want to try to fix it? You gotta give me a direction. |
|
If you would spare the time and effort, I'd be great if you would move it to |
I will put it on my list,lol!!! |
|
I will set this a draft and when I have something worth reviewing I will let you know. |
|
There you go @roderickvd and @ashthespy a fix up that doesn't require defining new With this PR the args are echo'd better. Out puts what you'd expect and isn't tripped up by any weird corner cases that I can tell: I'd like to see this merged in the meantime until I implement clap as that's basically going to be another rewrite of main and I just finished one not that long ago,lol!!! |
|
Yeah, this is much sweeter in the meantime. Ready to go for you? If so I’ll check it out tonight. |
There I think so. I was digging though all the |
|
It actually ended up being less lines of code than the broken version,lol!!! |
|
Ok so I added 2 commits.
|
|
The last commit guards against empty strings for args that accept strings like |
|
@ashthespy and @kingosticks What say you? |
|
I never know when you’re done 😆 |
I get antsy and see more and more as I look at the code. I'll be done if you're willing to review what I got? |
|
I was just squashing it on my side. Let know if you'd like me to push the squash before you review it or not? |
|
99% of the commit's are clean ups and minor refactoring. There's really not much there. |
|
After this I'll fix that alsa mixer bug. |
|
Apologies but I don't have time for a real code review, just a couple of drive-by comments. |
It's all good. |
|
Ok @roderickvd I'm done I swear, cross my little black heart,lol!!! I've deleted the local clone on my machine. Feel free to review at your leisure. I know you're busy with the new API and I know this looks like a lot at 1st glance but it's really mostly just a bunch of really small clean ups. I don't even think it warrants an entry in the change log. |
roderickvd
left a comment
There was a problem hiding this comment.
Phew this has become pretty complex for what seems so easy!
Sorry to keep bugging you about this, I know it's taking time.
Fix up for #886 Closes: #898 And... * Don't silently ignore non-Unicode while parsing env vars. * Iterating over `std::env::args` will panic! on invalid unicode. Let's not do that. `getopts` will catch missing args and exit if those args are required after our error message about the arg not being valid unicode. * Gaurd against empty strings. There are a few places while parsing options strings that we don't immediately evaluate their validity let's at least makes sure that they are not empty if present. * `args` is only used in `get_setup` it doesn't need to be in main. * Nicer help header. * Get rid of `use std::io::{stderr, Write};` and just use `rpassword::prompt_password_stderr`. * Get rid of `get_credentials` it was clunky, ugly and only used once. There is no need for it to be a separate function. * Handle an empty password prompt and password prompt parsing errors. * + Other random misc clean ups.
|
There you go @kingosticks that last commit DRYs up a lot of the log messaging. The magic happens with: let invalid_error_msg =
|long: &str, short: &str, invalid: &str, valid_values: &str, default_value: &str| {
error!("Invalid `--{}` / `-{}`: \"{}\"", long, short, invalid);
if !valid_values.is_empty() {
println!("Valid `--{}` / `-{}` values: {}", long, short, valid_values);
}
if !default_value.is_empty() {
println!("Default: {}", default_value);
}
}; |
|
@roderickvd I'd also like a review of the last commit on this. |
|
The last commit condenses option parsing for number type options that have a set range. |
|
After this I plan on releasing a new Raspotify version. That should be a good scream test for the last few commits since the last release. |
|
Especially since the next version of Raspotify leans pretty heavy on the env parsing feature. |
|
Alright let's get this in! |
Fix incorrect verbose logging of command line options
Fix up for #886
Closes: #898