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

passing parameters/switches to ns scripts



Having grown tired of editing my simulation scripts just to change
single lines, I came up with a more generalised solution, below, to
handle a varied command line of unordered switches and ordered
parameters (which, in my case, specify sets of parameters to be loaded
in from sourced Tcl scripts inside a mass of 'eval file <blah>'
checks).

-genmap makes an awful lot of sense to me as a switch option.

I hope this might be useful to someone else - is there a better OTcl
way of doing this that I don't know about? 

Cheers,

L.

probably reinventing the wheel.

<http://www.ee.surrey.ac.uk/Personal/L.Wood/>PGP<[email protected]>


# generalised parameter and switch-passing routine.
# Lloyd Wood, September 1998, in ignorance.

# because I'd rather say simulation.tcl -genmap outputfile.tcl 
# than have to edit my simulation script _again_.

# there's probably a much better way to do this than a recursive
# procedure, and I'm looking forward to being told what it is.


# sample switches
set switchgenmap 0
set switchwarn 0

# index to list of passed parameters
set parno 0

# sanity check on number of switches passed
# which can be used against argc to check that we get all parameters
set parswitches 0

proc parameterindex { } {
   global argv argc parno parswitches switchgenmap switchwarn
 
   set thisparm [lindex $argv $parno]
   incr parno
   if {$parno <=$argc} {   
      set firstchar [eval string index $thisparm 0 ]
      if {$firstchar=="-"} {
         incr parswitches
         if {$thisparm=="-genmap"} {
            set switchgenmap 1 
            puts "-genmap: genmapping of objects enabled."
         }
         if {$thisparm=="-warn"} {
            set switchwarn 1
            puts "-warn: warn on overwriting files"
         }
         # you'd add tests for other switch settings, setting
         # global variables, in here.
         set thisparm [eval parameterindex]
      }
   }
   return $thisparm
}

# and then, wherever you get your parameters:

   set constellationscript [eval parameterindex]
   puts "loading constellation from $constellationscript"
   set trafficscript [eval parameterindex]
   puts "loading traffic from $trafficscript"
   set outputname [eval parameterindex]
   puts "output filename specified as $outputname"
 
   # catch any remaining switches tagged onto the end.
   # an amazing number of unix things don't seem to do this...
   set null [eval parameterindex]

# and near the end of your script:

   # warnings on overwriting files
   
   if {$switchgenmap==1} {
      $ns gen-map
   }