Skip to content

Commit

Permalink
fix(completion): fix -- handling in getopts (#235)
Browse files Browse the repository at this point in the history
Apply the same workaround to getopts that was previously added for echo.
  • Loading branch information
reubeno authored Oct 25, 2024
1 parent 594407a commit 540ed09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions brush-core/src/builtins/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ pub(crate) struct GetOptsCommand {
const VAR_GETOPTS_NEXT_CHAR_INDEX: &str = "__GETOPTS_NEXT_CHAR";

impl builtins::Command for GetOptsCommand {
/// Override the default [`builtins::Command::new`] function to handle clap's limitation related
/// to `--`. See [`builtins::parse_known`] for more information
/// TODO: we can safely remove this after the issue is resolved
fn new<I>(args: I) -> Result<Self, clap::Error>
where
I: IntoIterator<Item = String>,
{
let (mut this, rest_args) = crate::builtins::try_parse_known::<GetOptsCommand>(args)?;
if let Some(args) = rest_args {
this.args.extend(args);
}
Ok(this)
}

#[allow(clippy::too_many_lines)]
async fn execute(
&self,
Expand Down
16 changes: 16 additions & 0 deletions brush-shell/tests/cases/builtins/getopts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ cases:
echo "OPTIND: ${OPTIND}"
echo "OPTERR: ${OPTERR}"
- name: "getopts: -- as first arg"
stdin: |
while getopts "ab:" myvar -- -a; do
echo "Result: $?"
echo "myvar: ${myvar}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"
echo "OPTERR: ${OPTERR}"
echo "----------------"
done
echo "Done; result: $?"
echo "myvar: ${myvar}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"
echo "OPTERR: ${OPTERR}"
- name: "getopts: invalid option when optstr starts with colon"
stdin: |
getopts ":a:" myvar -b value
Expand Down

0 comments on commit 540ed09

Please sign in to comment.