Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split an "if" condition over multiline lines with comments

I cannot achieve to split an "if" condition over multiple lines in PowerShell WITH comments, see example:

If ( # Only do that when...
    $foo # foo
    -and $bar # AND bar
)
{
    Write-Host foobar
}

This generates the following error:

Missing closing ')' after expression in 'if' statement.

Adding the ` character does not work:

If ( ` # Only do that when...
    $foo ` # foo
    -and $bar ` # AND bar
)
{
    Write-Host foobar
}

I get a:

Unexpected token '` # foo' in expression or statement.

The only way I've found is to remove the comments:

If ( `
    $foo `
    -and $bar `
)
{
    Write-Host foobar
}

But I am sure PowerShell offers a way to do what others scripting languages can: I just seem cannot find it...

like image 278
CDuv Avatar asked Apr 18 '16 09:04

CDuv


People also ask

How do you break a long if statement?

Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)

How do you break a line in an if statement in Python?

The recommended style for multiline if statements in Python is to use parentheses to break up the if statement. The PEP8 style guide recommends the use of parentheses over backslashes and putting line breaks after the boolean and and or operators.

How do you split a string into multiple lines in Python?

You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.

How do you wrap a long statement in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.


2 Answers

PowerShell automatically wraps lines when it recognizes an incomplete statement. For comparison operations this is the case if for instance you write a line with a dangling operator:

if ( # Only do that when...
    $foo -and  # foo AND
    $bar       # bar
)

Otherwise PowerShell will parse the two lines as two different statements (because the first line is a valid expression by itself) and fail on the second one because it's invalid. Thus you need to escape the linebreak.

However, just putting an escape character somewhere in the line won't work, because that will escape the next character and leave the linebreak untouched.

$foo ` # foo

Putting it at the end of a line with a (line) comment also won't work, because the comment takes precedence, turning the escape character into a literal character.

$foo  # foo`

If you want to escape the linebreaks you need to either move the comment elsewhere:

if (
    # Only do that when foo AND bar
    $foo `
    -and $bar
)

or use block comments as @Chard suggested:

if ( # Only do that when...
    $foo       <# foo #> `
    -and $bar  <# AND bar #>
)

But frankly, my recommendation is to move the operator to the end of the previous line and avoid all the hassle of escaping linebreaks.

like image 101
Ansgar Wiechers Avatar answered Sep 20 '22 22:09

Ansgar Wiechers


You can use block comments <# Your Comment #> to do this.

If ( <# Only do that when... #> `
    $foo <# foo #> `
    -and $bar <# AND bar #> 
)
{
    Write-Host foobar
}
like image 37
Richard Avatar answered Sep 20 '22 22:09

Richard