0

how can I get an input from user with special characters and treat it like a string without using the "/" character?
For example:
Suppose there is a procedure proc a {input} {...} that gets the following string "classA.arr2[0].classB".
If I don't add the character "/" next to "[0]" it will think that 0 is a command/procedure.

I will make it more clearly:
I added a code that is part of a Class A method that evaluating the args.

public method config {args} {
    if {[llength $args] > 1} {
        foreach {option value} $args {
            if {[string length $option] == 0 || [string length $value] == 0} {
                puts "Runtime error::Bad Input: option flag or value is missing"
                return ""
            }
            switch -- $option {
                -scope { 
                    if { [regexp {[A-Za-z]+} $value] } {
                        set _scope $value 
                    } else { 
                        puts "Runtime error::Bad Input: BAD SCOPE FORMAT" 
                        return ""
                    }
                } ...

Now I want to run the following code:

A a
a config -scope "string1.string2[0].string3"
Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
ms_stud
  • 361
  • 4
  • 18
  • Not enough information: how are you using the $input variable? – glenn jackman Aug 09 '18 at 12:47
  • We need to see where your problem is. At what point is your input string getting evaluated such that the `[0]` becomes a problem? Tcl never does double substitutions, so there has to be something in your code. – Brad Lanam Aug 09 '18 at 14:53

1 Answers1

1

I assume, you want to use your procedure manually from tclsh. If so, just put the input within braces. Example:

% proc a {input} {puts "Doing stuff with $input"}
% a "classA.arr2[0].classB" ;# Zero is evaluated
invalid command name "0"
% a "classA.arr2\[0\].classB" ;# You have to escape square brackets
Doing stuff with classA.arr2[0].classB
% a {classA.arr2[0].classB} ;# Or you can take the input in braces instead
Doing stuff with classA.arr2[0].classB

In this context, using braces prevents evaluation.

I also believe you mean \ (backslash), not / (slash), am I right?

  • Yes you are absolutely right. But I don't want to add backslash or braces when I am entering the input I want it to do it automatically inside the method. Meaning I want to be able to write the string as it is with the necessary flags – ms_stud Aug 09 '18 at 13:47
  • So, in a nutshell, you want to disable command expansion (as `0` is treated here as a sub command) in certain cases? Problem is, "[command substitution is not performed on words enclosed in braces](http://tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M11)" and only in these cases. I apologize, but no solution comes to my mind - at least other than a custom Tcl interpreter. – Maciej Stanek Aug 09 '18 at 14:35
  • @MaciejStanek is right. Command substitution occurs on everything enclosed in double quotes. And this substitution occurs BEFORE the control is passed to your proc. In other words, there is nothing you can do INSIDE the proc as a workaround because all the substitutions would have occurred before. Depending on where you get your input, you should try and sanitise the string there. E.g. converting "classA.arr2[0].classB" into {classA.arr2[0].classB} – Marijan Aug 09 '18 at 17:05