Skip to content

Interactive input should works with -File parameter #9494

Closed

Description

@iSazonov

Come form #9362
The comment in code says that interactive input should works

The comments differ in "no prompts" vs "with prompts" - it is not clear is it comments and behaviors right.

Also using dash is documented only for -Command (not for -File)
https://docs.microsoft.com/en-us/powershell/scripting/components/console/powershell.exe-command-line-help?view=powershell-6#-command

Steps to reproduce

pwsh -file C:\temp\q1.ps1 -

Expected behavior

pwsh -file C:\1\q1.ps1 -
<wait interactive inout>

Actual behavior

pwsh -file C:\1\q1.ps1 -
-

Expected behavior

pwsh -command -
<wait interactive inout>

Actual behavior

'-' was specified as the argument to -Command but standard input has not been redirected for this process.

Usage: pwsh[.exe] [[-File] <filePath> [args]]
                  [-Command { - | <script-block> [-args <arg-array>]
                                | <string> [<CommandParameters>] } ]
                  [-ConfigurationName <string>] [-CustomPipeName <string>]
                  [-EncodedCommand <Base64EncodedCommand>]
                  [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}]
                  [-Interactive] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
                  [-OutputFormat {Text | XML}] [-Version] [-WindowStyle <style>]
                  [-WorkingDirectory <directoryPath>]

       pwsh[.exe] -h | -Help | -? | /?

PowerShell Online Help https://aka.ms/pscore6-docs

All parameters are case-insensitive.
-

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-rc.1
PSEdition                      Core
GitCommitId                    6.2.0-rc.1-204-g262d86c691519ff7d5adacbae6424e2c87accce6
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Apr 29, 2019
  2. iSazonov commented on Apr 29, 2019

    @iSazonov
    CollaboratorAuthor

    Perhaps we could discuss this in #3223.

  3. mklement0 commented on Apr 29, 2019

    @mklement0
    Contributor

    The - in pwsh -file C:\temp\q1.ps1 - is just a positional argument; - is only special as the first argument after -file or -command; that is, if it is specified instead of a file name / code snippet.

    Do note that the primary use of these idioms is to provide input via stdin / the pipeline, not interactive use.

    Given that, when called from the outside, - should represent stdin, the following commands should all be equivalent on Unix:

    pwsh -noprofile -file -
    pwsh -noprofile -file /dev/stdin

    pwsh -noprofile -file - is broken (as is pwsh -noprofile -command -, as demonstrated in your OP).

    pwsh -noprofile -file /dev/stdin works in principle with interactive input, but, once the input is submitted with ^D, it then displays pseudo-interactive output behavior, as discussed in #3223.

    (As an aside, bash actually prompts line by line in this scenario, because, in the Unix world, non-redirected, i.e., interactive stdin input is processed line by line, whereas redirected stdin is typically processed as a whole. Again, however: interactive input is not the typical or useful use case - if you want to submit commands interactively, one by one, just start a new session with pwsh.)

    Returning to the typical use case:

    The following two commands should be equivalent (with the exception of what the script reports in $PSCommandPath, $PSScriptRoot, ...):

    • pwsh -noprofile -file foo.ps1
    • cat foo.ps1 | pwsh -noprofile -file -

    It is what enables the powerful curl ... | sh install-from-the-web idiom in POSIX-like shells - that is, feeding the content of a script to the shell rather than needing to provide a file['s path].

    If -file - worked that way, there would really be no need for -command - at all.
    Given that we cannot remove support for it, it should be made to work the same, but [soft]-deprecated.

    There's also no reason to disallow arguments to follow -file - - it's an unnecessary restriction that violates the equivalence of the two commands above. It prevents passing the script content via stdin / the pipeline while also accepting arguments.

    (Given how -command currently works, there is a good reason to disallow following -command - with arguments (the way that -command currently works - stitching together a script from all arguments following -command - is highly unfortunate, but cannot be changed), but if we fix -file -, that restriction can then easily be bypassed by using -file - instead).

    Note that it still makes sense to combine -command - not with - - with stdin input: The -command argument then specifies the command to run, as usual, and stdin input becomes its pipeline input.

    Here's a bash example:

    $ printf 'hi\nthere\n' | bash -c 'cat -n'
         1	hi
         2	there

    Regrettably, stdin input passed to pwsh currently does not become pipeline input - see #9497 - so that the following does not work analogously:

    $ printf 'hi\nthere\n' | pwsh -noprofile -command '% { $i=0 } { "{0}`t{1}" -f ++$i, $_ }'
    1   # !! stdin input did NOT become pipeline input

    As a doleful aside: if you tried to run this from PowerShell, the broken quoting for external programs (PowerShell itself, in this case), would make the command fail.

  4. iSazonov commented on Apr 29, 2019

    @iSazonov
    CollaboratorAuthor

    Do note that the primary use of these idioms is to provide input via stdin / the pipeline, not interactive use.

    Documentation is silent about it that confuses me. Make sense to open new issue in Docs repo?

    The question remains about the input from the console. Although it could also be documented that it is not supported.

  5. mklement0 commented on Apr 29, 2019

    @mklement0
    Contributor

    The docs definitely need improvement: The CLI help doesn't mention -File - at all, and doesn't mention the requirement of redirected stdin input for -Command -

    The current behavior is conceptually backwards and fundamentally broken, as shown in #3223:

    • It is designed around line-by-line processing, so clearly focused on interactive use.

      • By contrast, the real value comes from providing commands via redirected stdin / piping.
    • It is -Command - that is implemented with redirected stdin input in mind - yet line-by-line process is still applied (but at least the prompt string isn't printed the way it is with -File -), unlike when you pass a multi-line command string as an argument.

      • Conceptually, -File - makes more sense for redirected stdin input - and it is the only form needed.

    The question remains about the input from the console.

    pwsh -File - without redirected stdin input simply starts a regular session - the same as not specifying -File - at all.
    While technically somewhat defensible, it's certainly surprising.

    There is a way in which -File - could be useful with interactive input:
    If it allowed you to type an entire script ad hoc, and then submit it at once - it is what pwsh -File /dev/stin already does on Unix, albeit accidentally:

    $ pwsh -File -  # type the ad hoc script on the following lines, then submit with ^D (+ Enter on Windows)
    get-date
    get-childItem -Directory
    ^D

    This is what ruby supports, for instance.

    The caveat is that this form of supplying multi-line stdin input is probably more familiar to Unix users than Windows users, and it would be a first in the context of PowerShell.
    (I suppose submitting an empty extra line could be taken as the end-of-input signal, the same way as with array-valued mandatory parameter prompts, but that preclude use of blank lines for readability.)

  6. changed the title [-]Iinteractive input should works with -File parameter[/-] [+]Interactive input should works with -File parameter[/+] on Apr 29, 2019
  7. added
    WG-Engine
    Issue-Enhancement
    and removed
    Issue-Question
    on Sep 29, 2019
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

    Issue-EnhancementWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions