简体   繁体   中英

Combination of 2 commands in TCL

Here are the results of some TCL commands.

get_props -type assert
{"a", "b", "c", "d"}

Now all these 4 objects have certain attributes associated with them. But I am interested in the "enabled" attribute only.

get_attribute [get_props a] enabled
true

get_attribute [get_props b] enabled
false

get_attribute [get_props c] enabled
true

get_attribute [get_props d] enabled
false

Now I want to convert only "enabled" objects (enabled = true) out of these 4 "assert" type objects into "cover" type objects (So only "a" & "c" should be converted) and for converting "assert" into "cover", the command is fvcover.

I have tried the following command:

fvcover [get_props -type assert]

Now the problem is that, this fvcover command converts all 4 "assert" type objects into "cover" type objects, instead of just "a" & "c".

So I guess, I need to combine both get_props & get_attributes command, but I don't know how to do it.

So how to solve this problem?

Note :- "a", "b", "c", "d" are just for explanation. In reality, get_props command may return any number of results with any name. But out of that list, I need to convert, only those objects, whose "enabled" attribute is true.

The lists are not in Tcl format. Here's some test code you can use to convert from your format to Tcl.

#### PROCS FOR TESTING ####
proc get_props {type {assert no}} {
    if {$type == "-type" && $assert == "assert"} {
        return {"a", "b", "c", "d"}
    }
    if {$type == "a" || $type == "c"} {
        return [list enabled true]
    } elseif {$type == "b" || $type == "d"} {
        return [list enabled false]
    }
    return [list NOT FOUND]
}

proc get_attribute {a k} {
    foreach {key value} $a {
        if {$key == $k} {
            return $value
        }
    }
    return NOT_FOUND
}


# get props. props is in a list format that is not native tcl list
set props [get_props -type assert]

# convert props to tcl list
set props_list [string map {, ""} $props]

# make a list to catch enabled props
set enabled_props [list]

# add enabled props to new list
foreach {prop_name} $props_list {
    if {[get_attribute [get_props $prop_name] enabled] == "true"} {
        lappend enabled_props "\"$prop_name\""
    }
}
# convert enabled_props to your format
set enabled_props "{[join $enabled_props ", "]}"

# run your program on $enabled_props

puts $enabled_props

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM