espressomd-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ESPResSo-devel] TCL procedures with variable calling parameters


From: Olaf Lenz
Subject: Re: [ESPResSo-devel] TCL procedures with variable calling parameters
Date: Fri, 26 Oct 2007 14:23:34 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.6) Gecko/20070728 Thunderbird/2.0.0.6 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Limbach, Hans Joerg, LAUSANNE, NRC-FS wrote:
> I find it often very anoying that you one always has to look for the
> right order of arguments and in addition that you have to give all of
> them all the time.

Indeed, this is annoying. In general, whenever a Tcl proc has optional
arguments, a comparable construct is required.

Attached, you'll find a modified version of Hanjos script demonstarting
the technique that I have been using in the pbctools plugin for VMD and
also in ESPResSo in scripts/vtf.tcl (e.g. for the proc writevcf).

It basically does the same as Hanjos script, but has a number of
additional features:
1. It is more readable (I think).
2. It can handle optional arguments with no arguments ("switches") or
with more than one argument.
3. It passes through the mandatory arguments, i.e. after the option
processing, $args contains the remaining arguments that can be handled
classically.

Although this is not necessary, I have changed the optional arguments to
require a "-" in front of it, as is common e.g. in Unix commands. This
makes it easier to read the proc call, as one can distinguish between
the optional arguments and the mandatory arguments.

> Have a look at it and let me know what you think.
> I am almost sure that this either might exist already or that somebody
> of you has a better idea how to handle this. In my opinion something
> like this would make Espresso much more user friendly.

You are absolutely right. However, this would require to change the UI
of most of ESPResSos functions and would render all already written
scripts invalid! Therefore I think doing this change would be something
for ES++.

In general, this shows, how important it is to think thoroughly about
the UI that you give to a new function, because it is very hard to
change it afterwards, when it is already used by many people.

Olaf
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHIdxGtQ3riQ3oo/oRAlwIAJ4lfefzkr/C1/m3KJn3DL8aicdMpACgoYle
mc5gMmAZ0QBpVie9LLW1mss=
=KTPG
-----END PGP SIGNATURE-----
#!/usr/local/tcl/bin/tclsh8.4

proc test_params { args } {
    # set the defaults fr optional arguments
    set p1 "This"
    set p2 "is"
    set p3 "a"
    set p4 "stupid"
    set p5 "routine"
    set p6 0
    set p7_0 "two"
    set p7_1 "arguments"

    # process the optional arguments
    for { set argnum 0 } { $argnum < [llength $args] } { incr argnum } {
        set arg [ lindex $args $argnum ]
        set val [ lindex $args [expr $argnum + 1]]
        switch -glob -- $arg {
            "-p1" { set p1 $val; incr argnum; }
            "-p2" { set p2 $val; incr argnum; }
            "-p3" { set p3 $val; incr argnum; }
            "-p4" { set p4 $val; incr argnum; }
            "-p5" { set p5 $val; incr argnum; }
            "-p6" { set p6 1; }
            "-p7" { # option with two arguments
                set p7_1 $val
                set p7_2 [lindex $args [expr $argnum + 2]]
            }
            "--" { # stop processing of optional arguments 
                break
            }
            "-*" { # unknown option
                error "Option $arg not known"
            }
            default { break; }
        }
    }

    # remove the optional arguments
    set args [lrange $args $argnum end]

    puts "Optional arguments:"
    puts "  p1=$p1"
    puts "  p2=$p2"
    puts "  p3=$p3"
    puts "  p4=$p4"
    puts "  p5=$p5"
    puts "  p6=$p6"
    puts "  p7_0=$p7_0"
    puts "  p7_1=$p7_1"
    puts "Remaining arguments:"
    puts $args
}

test_params
test_params -p4 "clever"
test_params -p5 "Routine" -p2 "ist"  -p4 "dumme" -p1 "Das" -p3 "eine" 
test_params -p6 -p7 "with two" "arguments" "and some more afterwards"
test_params -h4 "test"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]