Skip to content

Out-String cmdlet should support -NoNewline too #3684

Closed

Description

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  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64

Activity

  1. added
    Issue-Enhancement
    on May 3, 2017
  2. added
    Hacktoberfest
    on Oct 1, 2017
  3. iamraghavs commented on Oct 5, 2017

    @iamraghavs
    Contributor

    Michael Klement (@mklement0) how complex is this for a beginner? If its fairly straightforward I'd like to give it a try!

  4. mklement0 commented on Oct 5, 2017

    @mklement0
    ContributorAuthor

    @raghav710:

    Shouldn't be too hard:

    • Add the new parameter here.
    • Act on it accordingly here.
    • Add tests here.

    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.

  5. SteveL-MSFT commented on Oct 5, 2017

    @SteveL-MSFT
    Member

    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.

  6. iSazonov commented on Oct 6, 2017

    @iSazonov
    Collaborator

    @raghav710 Typically, beginners receive most style comments. Please read out Coding Guidelines. Mainly you should follow the style of the surrounding code.

  7. iamraghavs commented on Oct 6, 2017

    @iamraghavs
    Contributor

    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?

  8. iSazonov commented on Oct 6, 2017

    @iSazonov
    Collaborator

    We cannot formally assign you today because your GitHub status is still not Contributor.

  9. SteveL-MSFT commented on Oct 6, 2017

    @SteveL-MSFT
    Member

    @raghav710 consider it implicitly assigned to you :)

    I'll assign it to myself so no one else ends up duplicating your work. Thanks!

  10. iamraghavs commented on Oct 7, 2017

    @iamraghavs
    Contributor

    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?

  11. iSazonov commented on Oct 7, 2017

    @iSazonov
    Collaborator

    -Stream turn out internal formatting in Out-String. I think -Stream and -NoNewLine should be in different parameter sets.

  12. mklement0 commented on Oct 7, 2017

    @mklement0
    ContributorAuthor

    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.

  13. 15 remaining items

  14. mklement0 commented on Oct 11, 2017

    @mklement0
    ContributorAuthor

    @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.

  15. iSazonov commented on Oct 12, 2017

    @iSazonov
    Collaborator

    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.

  16. mklement0 commented on Oct 12, 2017

    @mklement0
    ContributorAuthor

    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.

  17. iamraghavs commented on Oct 12, 2017

    @iamraghavs
    Contributor

    Should the docs be updated with the details of the current implementation of -NoNewLine ?

  18. SteveL-MSFT commented on Oct 12, 2017

    @SteveL-MSFT
    Member

    @raghav710 yes! is this something you can contribute? I think the -NoLast* discussion should continue in a separate issue

  19. removed
    Review - Committee
    on Oct 12, 2017
  20. iSazonov commented on Oct 13, 2017

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Could you please open new Issue for -NoLast*?

  21. mklement0 commented on Oct 13, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov): Please see #5108 and #5107.

  22. iamraghavs commented on Oct 13, 2017

    @iamraghavs
    Contributor

    Steve Lee (@SteveL-MSFT) I'd be happy to :) I should first create an issue and then work on it right?

  23. SteveL-MSFT commented on Oct 13, 2017

    @SteveL-MSFT
    Member

    @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

  24. iamraghavs commented on Oct 15, 2017

    @iamraghavs
    Contributor

    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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestIssue-EnhancementResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions