pythonazure-devopsazure-pipelinesazure-pipelines-yaml

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


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


Solution

  • I have fixed the issue. I wrapped my input parameter with double quotes and now I am able to take input strings with special characters. I output pipeline variable from my Python script for the next task, but due to the special character, I used to get this error.