-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Unify the docs of std::env::{args_os, args} more #84551
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit late to the table here, happening to stumble upon this just after it's merged, but I must state that I find both original and updated changes here to be flawed.
Filenames and paths, which of course are sometimes provided as arguments to a program, are not guaranteed to be valid UTF-8 on some platforms/filesystems. Consequently there is a possibility of arguments representing real filenames/paths genuinely being non-UTF-8 strings. Use of
args
to collect arguments that take filenames/paths would obviously result in a crash for such inputs, blocking users from potentially using real files/paths with that program.In theory any argument (or env-var), as external input to the program, could potentially be invalid UTF-8, and simply crashing in the face of this, rather than gracefully exiting with a clear error where appropriate, is really not good behaviour.
The benefit of
args
overargs_os
is simplicity for cases where for instance your app does not take any filename/path arguments, and you don't care if invalid UTF-8 argument inputs cause a crash. It is great for beginners getting to grips with the basics of playing with argument handling.The benefit of
args_os
overargs
is that it allows you to "correctly" handle all possible filenames/paths for such arguments, and allows you to more gracefully handle situations of otherwise unexpected invalid UTF-8 input, at the expense of the minor complication of having to handleOsString
/OsStr
types. It could be viewed as the better/right choice for those with more knowledge and experience.Thus the choice is nothing to do with safety exactly, as already pointed out, but also is not really about "ensuring UTF-8 validity" either. It's about (a) simplicity - avoiding you having to check for and handle non-UTF-8 situations as above, (b) whether or not you care about properly supporting possibly non-UTF-8 filename/path arguments, (c) whether or not you care about ungraceful crashes in the face of non-UTF-8 input otherwise.
This is not what is conveyed by this modified documentation. It might be best, really, for some form of what I have just written to be placed into the documentation to properly inform those trying to make a choice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to keep it short so maybe it can be changed to something simple like
If you want to panic on invalid UTF-8, use the [`args`] function instead.
? This sentence plainly states what happens and doesn't mention "safety" or "validity".But maybe it could still be nice to have that additional information you provided, so if you want to add that please open a PR. But if you think the sentence I just suggested suffices, I would be happy to make another PR changing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that that sentence is certainly much better, but I feel that there is significant value in actually explaining the situation regarding what I wrote above, such that people reading it can make a properly informed choice. I'll try to take a look at further improving the text later. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we have the exact same case for
vars
,vars_os
andvar
,var_os
too, don't we? I think you are making a really good point and it doesn't just apply toargs
andargs_os
so because this is more general I think what you are saying would be a good fit for maybe the docs ofstd::env
? Maybe you could mention those 6 methods at the bottom there and discuss them, rather than copy-paste to every of those 6 methods.