Skip to content

Add extern def which allows raw arguments - #8956

Merged
amtoine merged 1 commit into
nushell:mainfrom
pingiun:extern-with-blocks
Apr 28, 2023
Merged

amtoine merged 1 commit into
nushell:mainfrom
pingiun:extern-with-blocks

Conversation

@pingiun

@pingiun pingiun commented Apr 21, 2023 •

Copy link
Copy Markdown
Contributor

Description

Extends the extern syntax to allow commands that accept raw arguments. This is mainly added to allow wrapper type scripts for external commands.

This is an example on how this can be used:

extern foo [...rest] { 
  print ($rest | str join ',' ) 
}
foo --bar baz -- -q -u -x
# => --bar,baz,--,-q,-u,-x

(It's only possible to accept a single ...varargs argument in the signature)

User-Facing Changes

No breaking changes, just extra possibilities.

Tests + Formatting

Added a test for this new behaviour and ran the toolkit pr checker

After Submitting

This is advanced functionality but it should be documented, I will open a new PR on the book for that

@pingiun

pingiun commented Apr 21, 2023

Copy link
Copy Markdown
Contributor Author

The issue gives important context for this, but basically nu needs some way for functions to accept raw arguments instead of parsed arguments. In the issue @kubouch suggested using extern with a block as syntax for this, as it hints at this only being necessary/allowed for passing to external commands

@codecov

codecov Bot commented Apr 21, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #8956 (fa36c58) into main (24b4ac6) will increase coverage by 0.36%.
The diff coverage is 90.90%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8956      +/-   ##
==========================================
+ Coverage   68.19%   68.56%   +0.36%     
==========================================
  Files         636      636              
  Lines      101752   101798      +46     
==========================================
+ Hits        69393    69795     +402     
+ Misses      32359    32003     -356     
Impacted Files Coverage Δ
crates/nu-parser/src/parse_keywords.rs 76.38% <90.74%> (+0.01%) ⬆️
crates/nu-cmd-lang/src/core_commands/extern_.rs 100.00% <100.00%> (ø)

... and 5 files with indirect coverage changes

@fdncred

fdncred commented Apr 21, 2023

Copy link
Copy Markdown
Contributor

Thanks for this. It's less code than I thought there would be. Nice job!

One question, does it really fix #7758? I'm not sure how this is going to change the OP's original problem with built-in commands.

I think it does solve the problem for calling external executables, like if you wanted to wrap rg with variable parameters, or something like that.

@sophiajt

Copy link
Copy Markdown
Contributor

also tagging @kubouch who was thinking about this, too

@kubouch

kubouch commented Apr 24, 2023 •

Copy link
Copy Markdown
Contributor

Cool, I also expected more code. I would include a bit more test cases. Can you define other parameters (like extern foo [--bar: string, ...rest]) in the signature? What happens if you don't include ...rest? Does it work if you import the extern from a module?

@pingiun

pingiun commented Apr 24, 2023 •

Copy link
Copy Markdown
Contributor Author

@fdncred
One question, does it really fix #7758? I'm not sure how this is going to change the OP's original problem with built-in commands.

You're right that it doesn't really address the original issue, I will remove that from the description.

@kubouch
Can you define other parameters (like extern foo [--bar: string, ...rest]) in the signature? What happens if you don't include ...rest?

It doesn't allow other parameters, and it must include a rest parameter.

Does it work if you import the extern from a module?

Seems to work:
image

@kubouch

kubouch commented Apr 24, 2023

Copy link
Copy Markdown
Contributor

OK, sounds good, but I think the signature should support other parameters, just like def, otherwise I think it will be too limiting (e.g., you can't take your existing block-less extern definition and extend it by adding a block).

@pingiun

pingiun commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

OK, sounds good, but I think the signature should support other parameters, just like def, otherwise I think it will be too limiting (e.g., you can't take your existing block-less extern definition and extend it by adding a block).

How do you think this should combine with raw options?

@kubouch

kubouch commented Apr 25, 2023

Copy link
Copy Markdown
Contributor

Every parameter not defined in the signature, instead of throwing an error, would be a string collected in $rest (mandatory parameter if you use extern with a block). So, a signature like this

extern foo [x:int, --bar: string, ...rest] { }

would define $x, $bar and $rest variables inside the block.

This shouldn't be very hard because commands defined with extern have "fall-through" signatures enabled by default, so the $rest contents should be in the call's argument list as a vec of Argument::Unknowns already.

@pingiun

pingiun commented Apr 25, 2023 via email •

Copy link
Copy Markdown
Contributor Author

@pingiun
pingiun force-pushed the extern-with-blocks branch from 0bfa2b4 to fa36c58 Compare April 26, 2023 08:00
@pingiun

pingiun commented Apr 26, 2023

Copy link
Copy Markdown
Contributor Author

Yea it seems everything works this way so this is even better

@pingiun

pingiun commented Apr 26, 2023

Copy link
Copy Markdown
Contributor Author

The failing check is due to problems with the github api

@kubouch

kubouch commented Apr 26, 2023 •

Copy link
Copy Markdown
Contributor

Yeah, the CI is flaky sometimes. Can you restart the job? I think there is an option for it somewhere, or you can force-push into the branch to force the reset.

EDIT: I managed to reset the job.

@pingiun

pingiun commented Apr 26, 2023 via email •

Copy link
Copy Markdown
Contributor Author

@amtoine

amtoine commented Apr 27, 2023

Copy link
Copy Markdown
Member

I don’t have permission to restart the job and I have nothing to push

you should be able to

  • commit an empty commit
  • close and reopen the PR

to force the CI to trigger 😏

@amtoine
amtoine merged commit 44493da into nushell:main Apr 28, 2023
@amtoine

amtoine commented Apr 28, 2023

Copy link
Copy Markdown
Member

thanks @pingiun, let's see how it goes 😋

@pingiun

pingiun commented Apr 28, 2023

Copy link
Copy Markdown
Contributor Author

Very cool!

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.

5 participants