GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add implicit line continuance for named parameters and splatted collections (experimental feature) by KirkMunro · Pull Request #9614 · PowerShell/PowerShell

Conversation

@KirkMunro

@KirkMunro Kirk Munro (KirkMunro) commented May 16, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR Summary

Continuing the work that was started in PR #8938, this PR adds more implicit line continuance support to simplify scripting while further reducing the need for explicit line continuance using backtick. With these changes in place, users can span commands across multiple lines as long as the subsequent lines begin with named parameters or splatted collections.

Breaking Change Details

It is marked as experimental at the moment because it introduces a breaking change and there wasn't a better way to make it optional at this time. Before these changes, users could invoke a command whose name begins with a dash (-), or a named unary operator in a statement, even on a line immediately following another command.

For example, here is what can be done before these changes:

function -minus {
   'The -minus command was invoked.'
}
Get-Process -Id $PID
-minus # Invokes the -minus command

Get-Process -Id $PID
-split 'a b c d'

With this PR in place, and with the experimental feature enabled, here is what happens instead:

function -minus {
   'The -minus command was invoked.'
}
Get-Process -Id $PID
-minus # Identifies the '-minus' text as a named parameter for Get-Process (implicit line continuance)
& -minus # Invokes the -minus command using the required workaround

-minus # The workaround isn't necessary when you don't have a command on the contiguous, non-whitespace lines before this command

Get-Process -Id $PID
-split 'a b c d' # Identifies the '-split' text as a named parameter for Get-Process (implicit line continuance)

-split 'a b c d' # You can invoke named unary operators after commands by ensuring the parser recognizes them as new statements (which is done by placing a blank line before them)

While command names beginning with dash are virtually non-existent, to the point where I don't know if any exist in Windows or Linux (searches seem to indicate that dashes as the first character are problematic when it comes to file names on Linux, so maybe that's why), named unary operators are part of PowerShell and are used frequently. With that in mind, I've opened an RFC for discussion on possibilities because I believe this feature adds significant value that is worth considering, even if it remains only optional.

PR Context

This PR helps because PowerShell has many long commands (especially since some command names are over 60 characters long!), with many, many parameters, and users often want to span those commands across multiple lines in their scripts, but the only way to do that in PS 6.x and earlier is to use backtick or splatting. Neither of those should be necessary. Splatting has a purpose (passing in a common set of parameters to multiple commands), but splatting should not be the solution to more intelligent line wrapping within scripts. Instead, PowerShell can simply identify implicit line continuance automatically by looking at the subsequent lines to determine if they start with a named parameter or a splatted collection, and if so, tread them as part of the same command.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: or [ WIP ] to the beginning of the title (the WIP bot will keep its status check at Pending while the prefix is present) and remove the prefix when the PR is ready.
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed: #4303
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed
  • Experimental
    • The use of and testing of this feature requires enabling the following experimental features: PSImplicitLineContinuanceForNamedParameters

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

RamblingCookieMonster reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@KirkMunro
add experimental feat.:implicit param continuance
2822e7f
@KirkMunro
Kirk Munro (KirkMunro) requested a review from Dongbo Wang (daxian-dbw) as a code owner May 16, 2019 00:56
@KirkMunro Kirk Munro (KirkMunro) mentioned this pull request May 16, 2019
Update docs to explain how commands can be written to span multiple lines MicrosoftDocs/PowerShell-Docs#4303
Closed
2 of 8 tasks
@KirkMunro
adding file I missed in last commit
018c2ad
@KirkMunro
Kirk Munro (KirkMunro) requested review from Paul Higinbotham (PaulHigin), Travis Plunk (TravisEz13), Aditya Patwardhan (adityapatwardhan) and Andrew (anmenaga) as code owners May 16, 2019 01:43
@doctordns

Thomas Lee (doctordns) commented May 16, 2019

Copy link
Copy Markdown
Collaborator

I must be missing something - but the text:

function '-minus' {
   'The -minus command was invoked.'
}

Generates an error:

PS [C:\foo> ]> function '-minus' {
>>    'The -minus command was invoked.'
>> }
At line:1 char:9
+ function '-minus' {
+         ~
Missing name after function keyword.
At line:1 char:19
+ function '-minus' {
+                   ~
Unexpected token '{' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingNameAfterKeyword
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KirkMunro

Kirk Munro (KirkMunro) commented May 16, 2019

Copy link
Copy Markdown
Contributor Author

Sorry Thomas Lee (@doctordns), that was my mistake. I typed in those sample scripts since they were short, rather than copy/paste them, and in doing so I made a mistake wrapping the function name in quotes. This has been corrected in my original post.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TravisEz13 Travis Plunk (TravisEz13) added Breaking-Change breaking change that may affect users CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log labels May 16, 2019
@TravisEz13

Travis Plunk (TravisEz13) commented May 16, 2019

Copy link
Copy Markdown
Member

Kirk Munro (@KirkMunro) Could you write an RFC where users that try the feature can give feedback?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KirkMunro

Kirk Munro (KirkMunro) commented May 17, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Travis Plunk (@TravisEz13): RFC. Also, I added the link to the RFC PR in the description to increase visibility.

RamblingCookieMonster and fMichaleczek reacted with hooray emoji
All reactions
  • 🎉 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KirkMunro
added verbatim argument implicit continuance
60caea6
@GABeech George Beech (GABeech) mentioned this pull request May 18, 2019
RFC for expanded implicit line continuance feature PowerShell/PowerShell-RFC#176
Closed
@KirkMunro

Kirk Munro (KirkMunro) commented May 23, 2019

Copy link
Copy Markdown
Contributor Author

Pulling back on this PR for now as the RFC has changed significantly. The new proposal has eliminated any breaking changes. I'll be experimenting with the logic separately to confirm it's not very complex to do and let me try it out in a number of scenarios.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@KirkMunro Kirk Munro (KirkMunro) closed this May 23, 2019
@KirkMunro
Kirk Munro (KirkMunro) deleted the line-continuance-with-named-parameters-and-splatting branch July 29, 2019 21:13
@thomasrea0113 thomasrea0113 mentioned this pull request Sep 18, 2019
Automatically split pipes and ForEach-Object commands to a new line #10572
Closed
@jimbobmcgee jimbobmcgee mentioned this pull request May 28, 2020
Feature Request: Enforcement of non-newline command terminator (e.g. semicolon) within scripts, via opt-in directive #12803
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@daxian-dbw Dongbo Wang (daxian-dbw) Awaiting requested review from daxian-dbw daxian-dbw is a code owner
@adityapatwardhan Aditya Patwardhan (adityapatwardhan) Awaiting requested review from adityapatwardhan
@anmenaga Andrew (anmenaga) Awaiting requested review from anmenaga
@PaulHigin Paul Higinbotham (PaulHigin) Awaiting requested review from PaulHigin
@TravisEz13 Travis Plunk (TravisEz13) Awaiting requested review from TravisEz13

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Breaking-Change breaking change that may affect users CL-Experimental Indicates that a PR should be marked as an Experimental Feature in the Change Log

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@KirkMunro @doctordns @TravisEz13
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.