Is there a one-liner for using default values with

2020-07-02 08:46发布

问题:

I've written something like this to specify default values for prompts.

$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($defaultValue)]"
if ($prompt -eq "") {} else {
    $defaultValue = $prompt
    }

Can it be shortened further?

Here is my attempt.

$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($defaultValue)]"
if (!$prompt -eq "") {$defaultValue = $prompt}

I want a one-liner, so I'm gonna hold out accepting an answer until then.

N.b. $defaultValue should be stored independently of the one liner. Similar to the example above.

I've accepted the answer which lead me to the solution I was looking for.

$defaultValue = 'default'
if (($result = Read-Host "Press enter to accept default value $defaultValue") -eq '') {$defaultValue} else {$result}

And for those of you asking why. The reason is because it is easier on the eyes of whoever comes after me. Less is always more, when clarity is not sacrificed. IMHO.

EDIT;

Instead of a single line, perhaps I should have said a single phrase? I've added this edit clarify whilst a few answers I have seen use are using a semi-colon.

回答1:

Shortest Version I could came up with:

if (!($value = Read-Host "Value [$default]")) { $value = $default }

This version doesn't have to use else.



回答2:

$defaultValue = 'default'
$prompt = Read-Host "Press enter to accept the default [$($defaultValue)]"
$prompt = ($defaultValue,$prompt)[[bool]$prompt]

If you absolutely have to have it in one line:

$defaultValue = 'default'
($defaultValue,(Read-Host "Press enter to accept the default [$($defaultValue)]")) -match '\S' |% {$prompt = $_}


回答3:

if(($result = Read-Host "Press enter to accept default value [default]") -eq ''){"default"}else{$result}


回答4:

$DefaultValue="Foobar"

.... (Optional other code) ....

$Value=if($Value=(Read-Host "Enter value [$DefaultValue]")){$Value}else{$DefaultValue}

Just threw this together to re-use a previously entered config value while still allowing user to change it if needed... The accepted answer is missing the assignment portion and uses a hardcoded "Default" value...

There is a function (from other languages) called "Ternary Operator" or "Binary Operator"(https://en.wikipedia.org/wiki/%3F:) (the 'xx : yy ? zz' style one) and there are several people who have submitted functions to implement its behavior in Powershell.



回答5:

$prompt = ( ($defaultValue='a_default_value'), (Read-Host "Please enter something [$defaultValue]")) -match '\S' | select -last 1


回答6:

The given answers doesn't satisfy me (and don't work for me) but gave me enough input to came up with the shortest one-liner.

if (($prompt = Read-Host -Prompt "Your Text [your default]") -eq "") {$prompt= "your default"} 

Why adding an else? If user inputs nothing it does $prompt="your default" (could be a variable of course). If he adds something, its stored in %prompt already and you can leave the statement.



回答7:

I am using a function for that:

Function Read-HostDefault($Prompt, $Default) {
    if ($default) {
        $prompt = "$prompt [$default]"
    }
    $val = Read-Host $prompt
    ($default,$val)[[bool]$val]
}

Which gives:

PS C:\Windows\system32> Read-HostDefault "Enter port" 5432
Enter port [5432] : 
5432

PS C:\Windows\system32> Read-HostDefault "Enter port" 5432
Enter port [5432] : 1234
1234

PS C:\Windows\system32> Read-HostDefault "Enter port"
Enter port : 2468
2468


回答8:

You could also use a switch statement in a single line like this:

param([string]$myVariable = $($($val = $(Read-Host "Enter value")); $(Switch -regex ($val) { ".+" { $val } default { "my default value" } })))

The -regex .+ will match one or more characters, this would include any white space characters so maybe you want to strip out white space when doing the switch match i.e. \S.+.



回答9:

Assuming you already have $defaultValue:

$newValue = if ($value = Read-Host -Prompt "Please enter a value ($defaultValue)") { $value } else { $defaultValue }

This should work