Skip to content

Passing parameters to $env:PAGER is broken in 6.2.0-preview.4 #8912

Closed

Description

I have $env:PAGER set to less "-PPowerShell help page line %lt (press h for help or q to quit)" and on PS Core 6.1.0 on Windows 10 1809, PS Core uses less and gives me this nice footer during paging:

image

But in PS Core 6.2.0-preview.4 that same env var setting, causes more.com to be invoked. That is rather rude - ignoring my less preference.

Steps to reproduce

Set the following env var value.

$env:PAGER = 'less "-PPowerShell help page line %lt (press h for help or q to quit)"'

Expected behavior

I get paging via less and with my desired footer.

Actual behavior

more.com is invoked instead.

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.4
PSEdition                      Core
GitCommitId                    6.2.0-preview.4
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 Feb 19, 2019
  2. rkeithhill commented on Feb 19, 2019

    @rkeithhill
    CollaboratorAuthor

    Ah, I see this warning when I scroll up - it goes by pretty fast and winds up off-screen:

    WARNING: Ignoring invalid custom-paging utility command line specified in $env:PAGER: less "-PPowerShell help page line %lt (press h for help or q to quit)"
    

    OK, so this is a (lesser) breaking change. Is there a work-around?

  3. rkeithhill commented on Feb 19, 2019

    @rkeithhill
    CollaboratorAuthor

    I'm not sure this script works as expected:

                # Split the command line into tokens, respecting quoting. 
                $customPagerCommand, $customPagerCommandArgs = & { Write-Output -- $customPagerCommandLine }
    

    e.g.

    $customPagerCommandLine = 'less "-PPowerShell help page line %lt (press h for help or q to quit)"'
    $customPagerCommand, $customPagerCommandArgs = & { Write-Output -- $customPagerCommandLine }
    

    Outputs the entire value to $customPagerCommand because $customPagerCommandLine is always interpreted as a single string. Something like this works but I wouldn't recommend iex:

     $customPagerCommand, $customPagerCommandArgs = iex "Write-Output -- $customPagerCommandLine"
    
  4. rkeithhill commented on Feb 19, 2019

    @rkeithhill
    CollaboratorAuthor

    Another possible approach:

    $errs = $null
    $t = [System.Management.Automation.PSParser]::Tokenize($customPagerCommandLine, [ref]$errs)
    $customPagerCommand = $t[0]
    $customPagerArgs = $t[1]
    
  5. rjmholt commented on Feb 19, 2019

    @rjmholt
    Collaborator

    Looks like this is defined here:

    return @"
    <#
    .FORWARDHELPTARGETNAME Get-Help
    .FORWARDHELPCATEGORY Cmdlet
    #>
    [CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='https://go.microsoft.com/fwlink/?LinkID=113316')]
    param(
    [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [string]
    ${Name},
    [string]
    ${Path},
    [ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','Filter','ExternalScript','All','DefaultHelp','Workflow','DscResource','Class','Configuration')]
    [string[]]
    ${Category},
    [Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
    [switch]
    ${Detailed},
    [Parameter(ParameterSetName='AllUsersView')]
    [switch]
    ${Full},
    [Parameter(ParameterSetName='Examples', Mandatory=$true)]
    [switch]
    ${Examples},
    [Parameter(ParameterSetName='Parameters', Mandatory=$true)]
    [string[]]
    ${Parameter},
    [string[]]
    ${Component},
    [string[]]
    ${Functionality},
    [string[]]
    ${Role},
    [Parameter(ParameterSetName='Online', Mandatory=$true)]
    [switch]
    ${Online},
    [Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
    [switch]
    ${ShowWindow})
    # Display the full help topic by default but only for the AllUsersView parameter set.
    if (($psCmdlet.ParameterSetName -eq 'AllUsersView') -and !$Full) {
    $PSBoundParameters['Full'] = $true
    }
    # Nano needs to use Unicode, but Windows and Linux need the default
    $OutputEncoding = if ([System.Management.Automation.Platform]::IsNanoServer -or [System.Management.Automation.Platform]::IsIoT) {
    [System.Text.Encoding]::Unicode
    } else {
    [System.Console]::OutputEncoding
    }
    $help = Get-Help @PSBoundParameters
    # If a list of help is returned or AliasHelpInfo (because it is small), don't pipe to more
    $psTypeNames = ($help | Select-Object -First 1).PSTypeNames
    if ($psTypeNames -Contains 'HelpInfoShort' -Or $psTypeNames -Contains 'AliasHelpInfo')
    {
    $help
    }
    elseif ($help -ne $null)
    {
    $customPagerCommand = """"
    $customPagerCommandArgs = """"
    $customPagerCommandLine = """"
    if ($env:PAGER)
    {
    $customPagerCommandLine = $env:PAGER
    # Split the command line into tokens, respecting quoting.
    $customPagerCommand, $customPagerCommandArgs = & { Write-Output -- $customPagerCommandLine }
    # See if the first token refers to a known command (executable),
    # and if not, see if the command line as a whole is an executable path.
    $cmds = Get-Command $customPagerCommand, $customPagerCommandLine -ErrorAction Ignore
    if (-not $cmds) {
    # Custom command is invalid; ignore it, but issue a warning.
    Write-Warning ""Ignoring invalid custom-paging utility command line specified in `$env:PAGER: $customPagerCommandLine""
    $customPagerCommand = $null # use default command
    }
    elseif ($cmds.Count -eq 1 -and $cmds[0].Source -eq $customPagerCommandLine) {
    # The full command line is an unquoted path to an existing executable
    # with embedded spaces.
    $customPagerCommand = $customPagerCommandLine
    $customPagerCommandArgs = $null
    }
    }
    # Respect PAGER, use more on Windows, and use less on Linux
    if ($customPagerCommand) {
    $help | & $customPagerCommand $customPagerCommandArgs
    } elseif ($IsWindows) {
    $help | more.com
    } else {
    $help | less -Ps""Page %db?B of %D:.\. Press h for help or q to quit\.$""
    }
    }
    ";

    Possibly last changed in this commit: 4858afb.

  6. pougetat commented on Feb 19, 2019

    @pougetat

    Just saw this issue. I'll look into it asap since I committed that code. :)

  7. pougetat commented on Feb 19, 2019

    @pougetat

    Keith Hill (@rkeithhill) Rob Holt (@rjmholt)
    First of all thanks for your investigative work. :)
    I've looked into this issue and it does seem that my code is not tokenizing the custom env:PAGER that is being set. I am about to open a PR that tokenizes properly and once it is merged this env:PAGER variable will work :

    $env:PAGER = '"C:\Program Files\Git\usr\bin\less.exe" "-PPowerShell
    help page line %lt (press h for help or q to quit)"'

    You should replace the path specified with whichever path the less executable is located at. However since you said your PSCore uses less you might not even need the path.

  8. added a commit that references this issue on Apr 10, 2019
    d05737a
  9. added a commit that references this issue on Aug 6, 2026
    77dd4a3
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-QuestionResolution-Fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions