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

Why does bin_name() return str rather than OsString? #6

Closed
Minoru opened this issue Jul 19, 2021 · 2 comments
Closed

Why does bin_name() return str rather than OsString? #6

Minoru opened this issue Jul 19, 2021 · 2 comments

Comments

@Minoru
Copy link

Minoru commented Jul 19, 2021

lexopt tries not to convert strings unless absolutely necessary, but bin_name() deviates from that. The source code doesn't explain why. If bin_name() returned OsString, that'd be more consistent with other methods.

@blyxxyz
Copy link
Owner

blyxxyz commented Jul 19, 2021

I wrote up some considerations here but they fell through the gaps the first time I scrapped the method.

The main use case I envisioned for it was to show in help messages and error messages. Try running /bin/cat --help and /bin/cat doesnotexist: if you have GNU cat then you'll see

Usage: /bin/cat [OPTION]... [FILE]...
/bin/cat: doesnotexist: No such file or directory

It does this based on argv[0].

It's easy to format a &str but hard to format an &OsStr. With the current API you can do println!("Usage: {} blabla", parser.bin_name().unwrap_or("myprog"));, but if it returned an &OsStr you'd have to do more work.

Unlike a normal argument, which might be an actual file you have to open, I don't think there are any common cases where you'd absolutely have to process your own command name even if it's invalid unicode. (It's not even a good way of finding the actual executable.) But then a lossy decode might make more sense than ignoring it.

(I want to remove/overhaul the whole method, as discussed in #5 (comment))

@Minoru
Copy link
Author

Minoru commented Jul 19, 2021

Thanks! This answers my question completely, so I'll close the issue.

@Minoru Minoru closed this as completed Jul 19, 2021
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

2 participants