Skip to content

Conversation

zdenek-crha
Copy link
Contributor

The crate provides support for detecting and ignoring double-dash ("--")
argument passed from command line, but it does not provide any
information about its presence or position to user.

For applications that may want to pass arguments after double-dash to
child process is such indication critical.

Add optional field args_end into Matches structure storing position of
first argument after double-dash (if there is any) and method to provide
user access to the data.

When checking for double-dash positin, it is important to make sure that
there actually are some arguments after it. Otherwise the position would
point to non-existent index in free list and would require additional
unnecessary checks on user side.

@zdenek-crha
Copy link
Contributor Author

I've been thinking about the change bit more and there is an alternative approach to handling args_end_pos. Instead of storing and Option<usize>, we could store just usize and say that if there are no arguments after double-dash, then the pos points after the last element of free vector.

Together with slices, it could be used for easilly extracting both lists:

let args = matches.free[0 .. matches.args_end_pos()];
let rest  = matches.free[matches.args_end_pos()..];

Not sure which approach would be better as far as crate API goes - the current one might be easier to understand on glance, second one offers consistent access to both args and rest without conditionals and matching.

@KodrAus
Copy link
Contributor

KodrAus commented Dec 21, 2020

Thanks for working on this @zdenek-crha

I like the approach of always returning a usize that may just point to the end of the free list. I think the name args_end_pos is a little non-descriptive though. Since it's an index into free I think it should be prefixed with free. Maybe we could call it free_trailing_start to indicate the position of args in the free list that are trailing after a --? That way you'd write something like:

let for_child = matches.free.get(matches.free_trailing_start()..);

if for_child.len() > 0 {
    ..
}

What do you think?

@zdenek-crha
Copy link
Contributor Author

zdenek-crha commented Dec 28, 2020

Sorry for late response @KodrAus, I've been busy with vacation :-)

I have no issue with renaming the method to free_trailing_start(). It is bit long for my liking, but it also describes its return value better than the name I have used.

I'll work on the change asap and force-push new version (I tend to use force-push instead of adding 'fixme' commits, let me know if you you prefer different way to update PR).

The crate provides support for detecting and ignoring double-dash ("--")
argument passed from command line, but it does not provide any
information about its presence or position to user.

For applications that may want to pass arguments after double-dash to
child process is such indication critical.

Add optional field `args_end` into Matches structure storing position of
first argument after double-dash (if there is any) and method to provide
user access to the data.

When checking for double-dash positin, it is important to make sure that
there actually are some arguments after it. Otherwise the position would
point to non-existent index in `free` list and would require additional
unnecessary checks on user side.
@zdenek-crha zdenek-crha force-pushed the parse_args_end_position branch from 9fdd03d to 1b52757 Compare December 28, 2020 18:13
Copy link
Contributor

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zdenek-crha. This looks good to me.

@KodrAus KodrAus merged commit c11eb65 into rust-lang:master Jan 14, 2021
@zdenek-crha zdenek-crha deleted the parse_args_end_position branch January 14, 2021 06:23
@correabuscar correabuscar mentioned this pull request Jul 6, 2024
@github-actions github-actions bot mentioned this pull request Jun 5, 2025
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

Successfully merging this pull request may close these issues.

2 participants