0
function ed {
    Invoke-Expression "$env:EDITOR $args"
}
ed --option '1 2'  # Any number of parameters here.

I expect for ed to visit file 1 2, but actually it visited 1 and 2.

Any way to implement what I want?


FWIW, the value of my EDITOR is

echo $env:EDITOR
d:/Progs/Emacs/bin/emacsclientw --server-file=d:/Documents/Apps/Emacs/server-auth-dir/server-name.txt --alternate-editor= --create-frame
shynur
  • 334
  • 10
  • I'd recommend to read the help [Get-Help about_Functions_Advanced_Parameters](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3) – Olaf Aug 27 '23 at 08:44
  • it seems like what you wanted was simply `function ed { & $env:EDITOR $args }` – Santiago Squarzon Aug 27 '23 at 14:52

2 Answers2

0

Can you try this?

function EchoStringParameter {
    param (
        [string]$InputString
    )

    Write-Host "$InputString"
}

EchoStringParameter -InputString "1 2"

enter image description here

new update as request

function EchoStringParameter {
    echo $args
}

EchoStringParameter --option "1 2"

enter image description here

Thái Lê
  • 23
  • 6
  • I want to pass the arguments without writing `-InputString`. So in your example, the output should be `-InputString` and `1 2`. – shynur Aug 27 '23 at 08:58
  • ```function EchoStringParameter { param ( [string]$args ) echo "$args" } EchoStringParameter --option "1 2" ``` i used your input, sorry this cannot render well in the comment section. – Thái Lê Aug 27 '23 at 09:00
  • Emmm, it outputs only `1 2` in my pwsh. The expected output is `--option` and `1 2` – shynur Aug 27 '23 at 09:03
  • sorry it was my misunderstanding. i corrected it. please look at the answer above or in this comment ```function EchoStringParameter { echo $args } EchoStringParameter --option "1 2"``` – Thái Lê Aug 27 '23 at 09:12
  • thx. But this only works for `echo`. In fact, my original code is `Invoke-Expression "$env:EDITOR $args"`. I tried `function EchoStringParameter {$env:EDITOR $args}` and pwsh throw an error. – shynur Aug 27 '23 at 09:14
  • can you try this `function EditWithEditor { $arguments = $args -join ' ' Invoke-Expression "$env:EDITOR ""$arguments""" } EditWithEditor --option "1 2"` – Thái Lê Aug 27 '23 at 09:22
  • `EditWithEditor --option "1 2"` opens file `--option 1 2`. – shynur Aug 27 '23 at 09:26
0

When you do:

function f1 {
   Invoke-Expression "echo $args"
   $args
}

you will get:

PS D:\TEMP> f1 --option a 'b c'
--option
a
b
c
--option
a
b c
PS D:\TEMP>

So the use of Invoke-Expression "echo $args", does make you get unwanted output....


EDIT: (Because of more info that was given in the question)

When your environment variable also contains parameters which are needed to start your editor, and when you want to pass an unknown number of parameters, you can build the complete statement which is needed to start your editor like this:

function ed {
    param(
       [Parameter(ValueFromRemainingArguments=$true)][String[]]$Args
       )
    
    $cmd = $env:EDITOR + ' ' + [string]::Join(' ',$Args)
    & Invoke-Expression $cmd
}

The variable cmd should contain the complete statement that works when you type it at the prompt.

NOTE: Please take a note of this warning: "Take reasonable precautions when using the Invoke-Expression cmdlet in scripts. When using Invoke-Expression to run a command that the user enters, verify that the command is safe to run before running it. In general, it is best to design your script with predefined input options, rather than allowing freeform input." (see: Invoke-Expression)

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • I just want to pass strings to function separately. It would be nice if `func --option '1 2' "3 4"` behaves as same as what I write in interactive shell: `some-command --option '1 2' "3 4"` – shynur Aug 27 '23 at 09:29