Skip to content

Add "fall-through" signatures - #7527

Merged
kubouch merged 7 commits into
nushell:mainfrom
kubouch:extern-exec
Dec 21, 2022
Merged

kubouch merged 7 commits into
nushell:mainfrom
kubouch:extern-exec

Conversation

@kubouch

@kubouch kubouch commented Dec 19, 2022 •

Copy link
Copy Markdown
Contributor

Description

Adds a "fall-through" type of signature which does not throw errors if is called with unknown flags/positionals. This type of signatures is enabled for known externals defined with extern and the exec built-in command.

The PR is a continuation of #6567 made by @merelymyself and #7461 made by @WindSoilder.

Fixes #4659. Fixes #5294. Fixes #6124.

Does not fix: #5103

Problematic flags

These flags remain somewhat tricky:

  • -0: gets parsed as 0 integer and re-printed as 0 instead of -0
  • -h, --help: Prints the help message of exec and doesn't pass the flag to the external. If defined as extern the command will pass the -h flag to the external but won't print the help message.

User-Facing Changes

  • Signatures defined with extern now allow unknown arguments
  • exec now allows unknown arguments same as known external
  • tweaks to execargument handling so their behavior is consistent with running other externals (e.g., exec foo $x [ a b c ] -90 will now work correctly)
  • The Signature struct changed which means you need to delete $nu.plugin-file and re-register all of your plugins.

Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@kubouch kubouch added the notes:breaking-changes Noted in Breaking Changes section label Dec 19, 2022
@fdncred

fdncred commented Dec 19, 2022

Copy link
Copy Markdown
Contributor

unknown flags, yay! excited to see this work

}
spans_idx += 1;
} else {
error = error.or_else(|| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, I think we need to check if !signature.allows_unknown_flag here, we may only need to make the error if it's not allows_unknown_flag, to address #5103

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Interesting, yeah git push --force-with-lease doesn't work. I think we should still report missing flag parameter if the flag requires it. extern spam [--foo: string] should report error if you call just spam --foo. But git push --force-with-lease doesn't require a parameter, so there is something wrong here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually #5103 is also a problem of how the completion is defined, like #6020. It is currently not possible for Nushell call parsing to support both --flag and --flag value at the same time because it is not clear whether value is a flag's argument or a positional.

@WindSoilder

Copy link
Copy Markdown
Contributor

To fix the issue: #6020, I think we need to check if !signature.allows_unknown_flag too:

if spans[..end].is_empty() || spans_idx == end {
error = error.or_else(|| {
Some(ParseError::MissingPositional(
positional.name.clone(),
Span::new(spans[spans_idx].end, spans[spans_idx].end),
signature.call_signature(),
))
});

I don't think we need to return MissingPositional for known extern

let (arg, arg_err) = parse_value(
working_set,
arg_span,
&SyntaxShape::Any,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, I don't really understand why we need to use SyntaxShape::Any rather than SyntaxShape::String?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because we want to pass also other expressions that evaluate to a string, such as variables ($foo), subexpressions (([a b c] | str collect ' ')), etc. So what I did is I allowed all expressions and left the type checking up to the runtime.

@kubouch

kubouch commented Dec 19, 2022

Copy link
Copy Markdown
Contributor Author

I don't think we need to return MissingPositional for known extern

@WindSoilder We want to report a situation where you define that the command requires a positional but you don't supply it. Example: extern spam [a: string], then calling spam without a should throw an error. The problem with #6020 is actually that the npm external completion defines a required positional instead of optional. Could be fixed with extern npm [command?: string].

@kubouch

kubouch commented Dec 19, 2022

Copy link
Copy Markdown
Contributor Author

@WindSoilder I see where you're going with adding the !signature.allows_unknown_flag checks to allow more errors to pass. To me, however, it seems like we'd be subtly changing how call parsing works and it doesn't feel 100% right. It might be a good idea, I'm not sure. I'd rather merge this PR first and we can discuss additional changes afterwards.

@WindSoilder

Copy link
Copy Markdown
Contributor

@kubouch Yeah, that's fine :-) Thanks for your implementation, I never thought about adding Argument::Unknown to make the solution simpler, learned a lot from it

@kubouch
kubouch marked this pull request as ready for review December 20, 2022 10:58
@kubouch
kubouch merged commit 757d747 into nushell:main Dec 21, 2022
@kubouch
kubouch deleted the extern-exec branch December 21, 2022 22:33
@jiripospisil

Copy link
Copy Markdown

Hopefully fixes #2675 and #6381 as well.

@dead10ck

Copy link
Copy Markdown
Contributor

Awesome, glad to see this fix land. Thank you all for your work. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants