-1

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

phnsh
  • 24
  • 2
  • 5
  • ould you share with us ur script ? – Thomas Aug 04 '21 at 08:15
  • @Thomas I have updated my description with my script, but the error I am seeing appears in the pipeline logs - before the script execution even begins – phnsh Aug 04 '21 at 08:24
  • How are you passing in `Foo(Bar)`, ie `parameters.name`? Seems like you need to escape it there – Josh Friedlander Aug 04 '21 at 09:17
  • @JoshFriedlander Foo(Bar) is passed as a user input from the pipeline. It is a parameter that will take any string value. I'm accessing that param value as `' -Name "${{parameters.Name}}" '` – phnsh Aug 04 '21 at 09:41
  • I understand that, but can you give an example of the user input syntax? – Josh Friedlander Aug 04 '21 at 10:57

1 Answers1

0

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.

phnsh
  • 24
  • 2
  • 5