-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
cut: show error for multiple mode args (-b
, -c
, -f
)
#5962
Conversation
I used existing error message which was already being used when different types of mode args were given
but it is not exactly equal to the GNU's error message. Should the error message equal to GNU's? |
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.
Excellent! Could you annotate this a bit with some comments explaining the issue and how we work around it. For now, the error message is fine, we can change it later if we want to.
src/uu/cut/src/cut.rs
Outdated
let mode_args_count = [ | ||
matches.indices_of(options::BYTES), | ||
matches.indices_of(options::CHARACTERS), | ||
matches.indices_of(options::FIELDS), | ||
] | ||
.into_iter() | ||
.filter_map(|mode| mode.map(|indices| indices.len())) | ||
.sum(); |
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.
let mode_args_count = [ | |
matches.indices_of(options::BYTES), | |
matches.indices_of(options::CHARACTERS), | |
matches.indices_of(options::FIELDS), | |
] | |
.into_iter() | |
.filter_map(|mode| mode.map(|indices| indices.len())) | |
.sum(); | |
let mode_args_count = [ | |
matches.indices_of(options::BYTES), | |
matches.indices_of(options::CHARACTERS), | |
matches.indices_of(options::FIELDS), | |
] | |
.into_iter() | |
.filter_map(|mode| mode.map(|indices| indices.len())) | |
.sum(); |
I tried to find a way of expressing this without the nested map. This is what I landed on. Still the still in functionality though. I think this is a pretty good way of doing it!
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 was also wondering for more clean way to do it to remove nested map. It looks like the suggested change is identical to the original one, could you edit it to what you landed on?
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 don't know what @tertsdiepraam had in mind, here's an idea of how to get rid of the nested map:
let mode_args_count = [
matches.indices_of(options::BYTES),
matches.indices_of(options::CHARACTERS),
matches.indices_of(options::FIELDS),
]
.into_iter()
.map(|indices| indices.unwrap_or_default().count())
.sum();
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.
Thanks @cakebaker! It looks like a nice way to transform Option::None
to an empty iterator! Also, using count()
instead of len()
seems more correct way to do it, since it looks like the intended use case of len()
is to get the remaining length of the iterator.
Just updated the code!
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 must've made a mistake while copying code from my editor. I'll take another look in a bit. Sorry for the confusion 😄
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.
let mode_args_count = [
matches.indices_of(options::BYTES),
matches.indices_of(options::CHARACTERS),
matches.indices_of(options::FIELDS),
]
.into_iter()
.flatten()
.map(|indices| indices.len()))
.sum();
This is what I had in mind I think.
Thanks, added some comments about the issue and the fix. |
Thanks for your PR :) |
Overview
uutils-cut
runs without an error when there are multiple cutting mode arguments of one type, i.e. more than one--bytes
, or--characters
, or--fields
, while GNU cut shows an error as described in GNU cut manual -"Use one, and only one of -b, -c or -f."
For example:
Cause
Duplicated arguments were overriding the previous ones, therefore only the last cutting mode argument (
-b
,-c
,-f
) was handled, as you can see on the example above.Fix
Disable overriding of cutting mode arguments (
-b
,-c
,-f
) usingArgAction::APPEND
. Count the number of the cutting mode arguments and show an error when there are more than one-b
,-c
, or-f
.fixes #5925