switch statement 
Author Message
 switch statement

I was trying to parse the command line options as follows. But it throws me
an error for switch statement saying "bad option "-h": must
be -exact, -glob, -regexp, or -- "
If I give command line option as "-h"
##### Script ########

if { $argc > 0 } {
switch -regexp -- {"[lindex $argv 0]"} {
   {"-h"} {
        send_user "Usage: $argv0 ...sd....ffee.... \n"
        exit }
   }

Quote:
}



Mon, 21 Aug 2006 00:58:22 GMT  
 switch statement

Quote:

> I was trying to parse the command line options as follows. But it throws me
> an error for switch statement saying "bad option "-h": must
> be -exact, -glob, -regexp, or -- "
> If I give command line option as "-h"
> ##### Script ########

> if { $argc > 0 } {
> switch -regexp -- {"[lindex $argv 0]"} {
>    {"-h"} {
>         send_user "Usage: $argv0 ...sd....ffee.... \n"
>         exit }
>    }
> }

The above code does not produce the error you say it does. At least, not
for me. Also, you've got way too many quotes. Why are you doing
{"[lindex $argv 0]"} and matching against the pattern {"-h"}?

It's also not clear why you are doing regular expression matches against
user input. Are you really expecting them to give you a regular
expression on the command line? It doesn't appear so but that's what the
code assumes.

The following is, to my way of thinking, much more clear and less error
prone:

if {$argc > 0} {
        switch -exact -- [lindex $argv 0] {
                {-h} {
                        puts "-h detected"
                }
         }

Quote:
}

You might also be interested in processing args the following way. The
downside it it doesn't allow for abbreviations (eg: --file instead of
--filename) or the old unix way of lumping options together (eg: ls
-xf), but it can be done with a variation of this technique. If you need
something more fancy there are various getopt-like packages you can use.

while {[string match "-*" [lindex $argv 0]]} {
     set option [lindex $argv 0]
     set argv [lrange $argv 1 end]
     switch -exact -- $option {
        -h {
            puts "help!"
        }
        -f -
        --filename {
            # for illustrative purposes, assume this
            # option must be followed by a filename
            set filename [lindex $argv 0]
            set argv [lrange $argv 1 end]
        }

        -- {break}
     }

Quote:
}

if {[info exists filename]} {
     puts "your filename is $filename"

Quote:
}

puts "remaining args: $argv"


Mon, 21 Aug 2006 01:37:27 GMT  
 switch statement

Quote:

> I was trying to parse the command line options as follows. But it throws me
> an error for switch statement saying "bad option "-h": must

Try:
if { $argc > 0 } {
   switch -regexp -- [lindex $argv 0] {
      "-h" {send_user "Usage: $argv0 ...sd....ffee.... \n"
      exit }
   }

Quote:
}

Regards,
Christian


Mon, 21 Aug 2006 01:49:06 GMT  
 switch statement

| Try:
| if { $argc > 0 } {
|    switch -regexp -- [lindex $argv 0] {
|       "-h" {send_user "Usage: $argv0 ...sd....ffee.... \n"
|       exit }
|    }
| }

Or even
  switch -- [lindex $argv 0] {
      "-h" {
           send_user "Usage: $argv0 ...sd....ffee.... \n"
           exit
      }
   }

since the -regexp would also trigger on anything which _contains_ `-h'
(no anchors) and is unnecessary anyway.

R'



Mon, 21 Aug 2006 02:37:12 GMT  
 switch statement

Quote:


> | Try:
> | if { $argc > 0 } {
> |    switch -regexp -- [lindex $argv 0] {
> |       "-h" {send_user "Usage: $argv0 ...sd....ffee.... \n"
> |       exit }
> |    }
> | }

> Or even
>   switch -- [lindex $argv 0] {
>       "-h" {
>            send_user "Usage: $argv0 ...sd....ffee.... \n"
>            exit
>       }
>    }

> since the -regexp would also trigger on anything which _contains_ `-h'
> (no anchors) and is unnecessary anyway.

Yes, that is true -- sorry, I just c&p the original version partly.

C.



Tue, 22 Aug 2006 23:06:02 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Switch statement with nested If statement

2. Eiffel approach to switch statements

3. multi-dimensional switch statements

4. Switch statement in (A)Rexx

5. making VRML objects disappear (also SWITCH statement)

6. MIPS switch statement

7. Accessing Switch Statement Argument

8. switch statement and numeric constants

9. error in Tcl switch statement

10. switch statement problem

11. dynamic switch statement, unexpected behaviour

12. Range in Switch statement syntax

 

 
Powered by phpBB® Forum Software