OsStr::starts_with API to support clap?

Hi. I was redirected here from https://github.com/rust-lang/rust/issues/new/choose since this is a feature request.

clap is a popular command line argument parsing crate. Clap requires the ability to peek into an &OsStr, for example, to see if the arg starts with - or --. It is desirable for Clap to operate on &OsStr instead of &str because native platforms do not limit command line arguments to &str.

I believe parsing arguments from the command line to be a common use case, of which determining switches and flags is part of the task. Is adding an OsStr::starts_with API something the libs team would be interested in?


I am not a team member but I have seen that suggestions like this seem to normally be encouraged to be created as a straight PR where the function is unstable behind a gate. Given that this method is already on &str it seems very reasonable that it would be accepted.


It's part of RFC 2500, which has been sadly stalled for quite some time. Perhaps it can still be added now in a way that would be forwards compatible with the RFC though.


There was a conversation recently about using a different approach for the Pattern API here: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/New.20Pattern.20API.3F/near/209492625

Unfortunately I think recent attempts to move it forward (https://github.com/rust-lang/rust/pull/76901) are stalled for concerns about the current design.

1 Like

I've dealt with the same problem in my configure_me crate which aims to be a replacement for clap in some situations. The methods for parsing &OsStr arguments and detecting if they contain -- are actually separated into a small parse_arg crate, so clap is free to use them without waiting for addition to std. See parse_arg::match_arg

LMK if you need some help using it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.