[Python] YAML pipeline unable to parse input parameter with special characters to a Python task

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    How do I escape/handle parentheses/arbitrary inputs with special characters such as the one I have shown below so that my python code gets the proper string argument?

    I have a YAML pipeline and it takes an input parameter as a string from the user, and provides that string as an argument to a Python task:

    - name: Name
    displayName: Username
    type: string
    default: "-"

    - task: PythonScript@0
    displayName: 'Python task'
    inputs:
    scriptPath: $(System.DefaultWorkingDirectory)/x/y.py
    arguments: ' -Name "${{parameters.Name}}" '
    workingDirectory: '$(System.DefaultWorkingDirectory)/x'



    This input parameters works fine for alphanumeric characters, but when I input a string that has opening and closing round brackets (such as Foo(Bar)), the Python YAML task prints an error:


    /bin/sh: 1: Syntax error: "(" unexpected
    ​

    Please note that the above error appears in the pipeline logs before the Python script execution even begins

    Success input string: FooBar

    Failure input string: Foo(Bar)

    How do I handle this so that my Python script gets the correct string?

    Python script:

    import argparse

    parser = argparse.ArgumentParser(description="test")
    parser.add_argument("-Name")

    args = parser.parse_args()

    user_name= args.Name


    Edit: Added more clarity, and added Python script

    Continue reading...

Compartilhe esta Página