You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Out-String cmdlet should support -NoNewline too #3684
PSv5 brought the -NoNewline switch to the Set-Content (Add-Content) and Out-File cmdlets to allow direct concatenation of the input objects without newlines (strings among the input objects retain their embedded newlines however).
By default, these cmdlets blindly append an additional newline, whereas Out-String currently invariably does that, because it lacks -NoNewline.
For symmetry, Out-String should support -NoNewline too.
Current behavior
> (Out-String-InputObject 'a') -match'^a\r?\n$'$True# A trailing newline was added.
Desired behavior
> (Out-String-NoNewline -InputObject 'a') -match'^a$'$True# No trailing newline was added.
Environment data
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar 316:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64
@raghav710 Typically, beginners receive most style comments. Please read out Coding Guidelines. Mainly you should follow the style of the surrounding code.
Michael Klement (@mklement0) I notice that when the -Stream switch is used, no newline is appended. Should I make the behaviour consistent and add in a newline when -NoNewLine is not set?
Yes, -Stream and -NoNewline should be mutually exclusive and should therefore be in different parameter sets.
All Out-String output goes through the formatting system to generate multi-line string output; by default, the result is output as a single, multi-line string; -Stream, by contrast, outputs the lines as a string array, and the individual array elements - the lines - should never have a trailing newline.
In other words: No change should be made to the existing -Stream behavior.
@raghav710: Yes, PowerShell Core uses platform-appropriate newlines (CRLF on Windows, LF-only on Unix), so getting 6 chars. on Windows vs. 4 on Unix is to be expected.
This behavior is unrelated to -NoNewline (newline is used as an abstract term for line break, irrespective of what specific character [sequence] is used to represent it).
As Steve Lee (@SteveL-MSFT) mentions, the natural assumption about what -NoNewline does is to suppress a trailing newline only, but - unfortunately - that is never how it has worked: all implementations of -NoNewline - in Write-Host (v2+), Set-Content/Add-Content (v5+), Out-File (v5+) directly concatenate the (representations of) all input objects without newlines:
>1,2|Write-Host-NoNewline; Write-Host more
12more
(You won't notice with Write-Host 1, 2 or the equivalent Write-Host 1 2, because the array is processed as a whole and stringified to 1 2.)
The usefulness of this behavior is questionable, but changing it would certainly be a breaking change.
Currently, -NoNewline only work as one would expect with a single input object.
The only conceivable benefit of the current -NoNewline behavior with multiple input objects is a way to suppress all newlines at the source, as opposed to having to remove them after the fact.
As such, it is as special case of a generic -Delimiter parameter, that was suggested in #3855: -Delimiter ''.
-Delimiter would work as a terminator - i.e., placed after every object, including the last; arguably, it would also be helpful to place the string only between elements, not also after the last, i.e, act as a separator (with an empty delimiter that distinction doesn't matter).
If -NoNewline gets fixed to work the way users probably expect - suppress the last newline only - -NoTerminator could be introduced as an alias of -NoNewline to generically suppress a trailing delimiter - be it a newline by default or one specified via -Delimiter, which effectively applies the delimiter string as a separator.
Michael Klement (@mklement0) if -NoNewline works as No Newline Between Objects we would add new parameter -NoLastNewLine which will suppress only last new line.
The parameter name -NoNewline does not explicitly imply that new lines is suppressed all or only the last. So user expectiotions is based on docs and/or actual behavior. Considering it's a long-standing parameter I beleive that -NoLastNewLine is right way if we really want fix something here.
User expectations may be based on examining actual behavior (personally, I only very recently discovered the true behavior), and it's certainly not based on the docs, as I've tried to point out.
That said, introducing -NoLastNewline (and possibly alias -NoLastDelimiter) sounds like a good compromise for the sake of backward compatibility.
Activity
iamraghavs commentedon Oct 5, 2017
Michael Klement (@mklement0) how complex is this for a beginner? If its fairly straightforward I'd like to give it a try!
mklement0 commentedon Oct 5, 2017
@raghav710:
Shouldn't be too hard:
I'm not sure how the documentation part is usually handled; perhaps create an issue in the docs repo; Ilya (@iSazonov), can you advise?
Thanks for taking it on.
SteveL-MSFT commentedon Oct 5, 2017
For the docs, the best thing is to create a corresponding doc PR in the docs repo and link it to the PR in this repo.
iSazonov commentedon Oct 6, 2017
@raghav710 Typically, beginners receive most style comments. Please read out Coding Guidelines. Mainly you should follow the style of the surrounding code.
iamraghavs commentedon Oct 6, 2017
Ilya (@iSazonov) Steve Lee (@SteveL-MSFT) Michael Klement (@mklement0) thanks for all the guidance :) That was quite kind of you all :)
Should the issue be assigned before i start working?
iSazonov commentedon Oct 6, 2017
We cannot formally assign you today because your GitHub status is still not
Contributor.SteveL-MSFT commentedon Oct 6, 2017
@raghav710 consider it implicitly assigned to you :)
I'll assign it to myself so no one else ends up duplicating your work. Thanks!
iamraghavs commentedon Oct 7, 2017
Michael Klement (@mklement0) I notice that when the -Stream switch is used, no newline is appended. Should I make the behaviour consistent and add in a newline when -NoNewLine is not set?
iSazonov commentedon Oct 7, 2017
-Streamturn out internal formatting in Out-String. I think-Streamand-NoNewLineshould be in different parameter sets.mklement0 commentedon Oct 7, 2017
Yes,
-Streamand-NoNewlineshould be mutually exclusive and should therefore be in different parameter sets.All
Out-Stringoutput goes through the formatting system to generate multi-line string output; by default, the result is output as a single, multi-line string;-Stream, by contrast, outputs the lines as a string array, and the individual array elements - the lines - should never have a trailing newline.In other words: No change should be made to the existing
-Streambehavior.15 remaining items
mklement0 commentedon Oct 11, 2017
@raghav710: Yes, PowerShell Core uses platform-appropriate newlines (CRLF on Windows, LF-only on Unix), so getting 6 chars. on Windows vs. 4 on Unix is to be expected.
This behavior is unrelated to
-NoNewline(newline is used as an abstract term for line break, irrespective of what specific character [sequence] is used to represent it).As Steve Lee (@SteveL-MSFT) mentions, the natural assumption about what
-NoNewlinedoes is to suppress a trailing newline only, but - unfortunately - that is never how it has worked: all implementations of-NoNewline- inWrite-Host(v2+),Set-Content/Add-Content(v5+),Out-File(v5+) directly concatenate the (representations of) all input objects without newlines:(You won't notice with
Write-Host 1, 2or the equivalentWrite-Host 1 2, because the array is processed as a whole and stringified to1 2.)The usefulness of this behavior is questionable, but changing it would certainly be a breaking change.
Currently,
-NoNewlineonly work as one would expect with a single input object.The only conceivable benefit of the current
-NoNewlinebehavior with multiple input objects is a way to suppress all newlines at the source, as opposed to having to remove them after the fact.As such, it is as special case of a generic
-Delimiterparameter, that was suggested in #3855:-Delimiter ''.-Delimiterwould work as a terminator - i.e., placed after every object, including the last; arguably, it would also be helpful to place the string only between elements, not also after the last, i.e, act as a separator (with an empty delimiter that distinction doesn't matter).If
-NoNewlinegets fixed to work the way users probably expect - suppress the last newline only --NoTerminatorcould be introduced as an alias of-NoNewlineto generically suppress a trailing delimiter - be it a newline by default or one specified via-Delimiter, which effectively applies the delimiter string as a separator.iSazonov commentedon Oct 12, 2017
Michael Klement (@mklement0) if
-NoNewlineworks as No Newline Between Objects we would add new parameter -NoLastNewLine which will suppress only last new line.The parameter name
-NoNewlinedoes not explicitly imply that new lines is suppressed all or only the last. So user expectiotions is based on docs and/or actual behavior. Considering it's a long-standing parameter I beleive that-NoLastNewLineis right way if we really want fix something here.mklement0 commentedon Oct 12, 2017
User expectations may be based on examining actual behavior (personally, I only very recently discovered the true behavior), and it's certainly not based on the docs, as I've tried to point out.
That said, introducing
-NoLastNewline(and possibly alias-NoLastDelimiter) sounds like a good compromise for the sake of backward compatibility.iamraghavs commentedon Oct 12, 2017
Should the docs be updated with the details of the current implementation of
-NoNewLine?SteveL-MSFT commentedon Oct 12, 2017
@raghav710 yes! is this something you can contribute? I think the
-NoLast*discussion should continue in a separate issueiSazonov commentedon Oct 13, 2017
Michael Klement (@mklement0) Could you please open new Issue for
-NoLast*?mklement0 commentedon Oct 13, 2017
Ilya (@iSazonov): Please see #5108 and #5107.
iamraghavs commentedon Oct 13, 2017
Steve Lee (@SteveL-MSFT) I'd be happy to :) I should first create an issue and then work on it right?
SteveL-MSFT commentedon Oct 13, 2017
@raghav710 for the doc update, you don't need to create an issue, you can just submit PR to https://github.com/PowerShell/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Utility/Out-String.md
iamraghavs commentedon Oct 15, 2017
Michael Klement (@mklement0) Steve Lee (@SteveL-MSFT) Ilya (@iSazonov) I've added the docs and raised a PR here. Please let me know if I need to add more clarity anywhere