user3565150
user3565150

Reputation: 914

Parsing string arguments in tcl

I am passing parameters to a function StcPortConfig as below:

 STC::StcPortConfig m:$port_b fc:1 pause_frame_rate:30 pause_val:"FFFF" mode:"gen";

In StcPortconfig function I need to parse different parameter values which I am doing as follows:

 proc ::STC::StcPortConfig {args} {

  foreach arg $args {
    regexp {([A-Za-z0-9_-]+):([a-z0-9,-_ ]*)} $arg match cmd value

 if {$cmd == "pause_frame_rate"} {
    set pause_frame_rate $value
    set pause_frame 1
  }
 if {$cmd == "mode"} {
    set mode $value
    puts "\nport is configured as $mode\n"
  }
}

I am able to read the value of pause_frame_rate (set to 30). But there is a problem in parsing string when I am reading "mode". The print statement is giving "port is configured as ".

When I am reading the args, its printing like below: args are m:1 fc:1 pause_frame_rate:30 pause_val:\"FFFF\" mode:\"gen\"

I am missing something while trying to parse the parameters which are string and not integers/numerals.

Upvotes: 1

Views: 701

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

You can use string trim to remove the quotes. An alternative implementation:

proc ::STC::StcPortConfig {args} {
    foreach arg $args {
        regexp {([\w-]+):(.*)} $arg -> cmd value
        set value [string trim $value {"}]   ; # remove double quotes
        switch -exact -- $cmd {
            pause_frame_rate {
                set pause_frame_rate $value
                set pause_frame 1
            }
            mode {
                set mode $value
                puts "\nport is configured as $mode\n"
            }
        }
    }
}

Upvotes: 1

user3565150
user3565150

Reputation: 914

Instead of doing mode:"gen", I changed it to mode:gen and it works.

Upvotes: 0

Related Questions