1

How can I get a single string variable with spaces in it in TCL to be interpreted as multiple arguments? I can't change the proc definition.

Here is an example of what I mean:

set my_options ""
if { "$some_condition" == 1 } {
    append my_options " -optionA"
}
if { "$some_other_condition" == 1 } {
    append my_options " -optionB"
}
set my_options [string trim $my_options]
not_my_proc ${my_options} ;# my_options gets interpreted as a single arg here and causes a problem:
# Flag '-optionA -optionB' is not supported by this command.

1 Answer 1

1

This is where you use the argument expansion syntax:

not_my_proc {*}$my_options
# ..........^^^

Although I'd recommend using a list instead of a string:

  • if for some reason the my_options string is not a well-formed list, you'll see an error thrown
  • if any of the options takes a space, a list is the proper data structure:
set my_options [list]
lappend my_options {-option1}
lappend my_options {-option2 "with a parameter"}
not_my_proc {*}$my_options
Sign up to request clarification or add additional context in comments.

1 Comment

Ohh fabulous - I knew there had to be a way! Thanks Glenn!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.