args question

  • Follow


Hi,

I have a question. I can't figure out an optimal way to properly
design my proc.

The input_cap proc below will get the input args in the form:

            input_cap [-pts <pt_name_list>|all] [-config <name>
<ingr_name_list>]

Below is my proc itself but I am not sure if passing args in this way
for the pattern above is correct.

proc input_cap { args } {
     if { [llength $args] == 0 } {
        print_usage
        exit 1

    } else {
        print_usage
        exit 0

    }
}

Should I use { {arg1 ""} {arg2 ""}} instead of [lindex $args 0],
[lindex $args 1], etc.?

It's just that -pt and -config flags control the output generation and
the rest of the list items  tell how this output should be generated.

Please advise.

Thank you.
Hrachya
0
Reply Hrachya.Aghajanyan (6) 4/20/2010 7:02:16 PM

Have a look at this answer on Stack Overflow:

http://stackoverflow.com/questions/2341441/how-can-i-safely-deal-with-optional-parameters/2343802#2343802

At 2010-04-20 03:02PM, "Hrachya.Aghajanyan@gmail.com" wrote:
>  Hi,
>  
>  I have a question. I can't figure out an optimal way to properly
>  design my proc.
>  
>  The input_cap proc below will get the input args in the form:
>  
>              input_cap [-pts <pt_name_list>|all] [-config <name>
> <ingr_name_list>]

Is that right?  the -config option takes 2 arguments?

Do you expect to see either -pts or -config but not both?

If the answer is yes, then do this (untested):

    proc lshift {listVar {num 1}} {
        upvar 1 $listVar lst
        set shifted [lrange $lst 0 [expr {$num - 1}]]
        set lst [lrange $lst $num end]
        return $shifted
    }

    proc input_cap args {
        switch --exact -- [lshift args] {
            -pts {
                do your pts stuff with $args
            }
            -config {
                do your config stuff with $args
            }
            default {
                puts "'$option' is an invalid option"
                print_usage
                return 1
            }
        }
        return 0
    }

>  Below is my proc itself but I am not sure if passing args in this way
>  for the pattern above is correct.
>  
>  proc input_cap { args } {
>       if { [llength $args] == 0 } {
>          print_usage
>          exit 1
>  
>      } else {
>          print_usage
>          exit 0
>  
>      }
>  }
>  
>  Should I use { {arg1 ""} {arg2 ""}} instead of [lindex $args 0],
>  [lindex $args 1], etc.?
>  
>  It's just that -pt and -config flags control the output generation and
>  the rest of the list items  tell how this output should be generated.

>  
>  Please advise.
>  
>  Thank you.
>  Hrachya


-- 
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous
0
Reply Glenn 4/20/2010 8:03:51 PM


On Apr 21, 1:03=A0am, Glenn Jackman <gle...@ncf.ca> wrote:
> Have a look at this answer on Stack Overflow:
>
> http://stackoverflow.com/questions/2341441/how-can-i-safely-deal-with...
>
> At 2010-04-20 03:02PM, "Hrachya.Aghajan...@gmail.com" wrote:
>
> > =A0Hi,
>
> > =A0I have a question. I can't figure out an optimal way to properly
> > =A0design my proc.
>
> > =A0The input_cap proc below will get the input args in the form:
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0input_cap [-pts <pt_name_list>|all] [-config=
 <name>
> > <ingr_name_list>]
>
> Is that right? =A0the -config option takes 2 arguments?
>
> Do you expect to see either -pts or -config but not both?
>
> If the answer is yes, then do this (untested):
>
> =A0 =A0 proc lshift {listVar {num 1}} {
> =A0 =A0 =A0 =A0 upvar 1 $listVar lst
> =A0 =A0 =A0 =A0 set shifted [lrange $lst 0 [expr {$num - 1}]]
> =A0 =A0 =A0 =A0 set lst [lrange $lst $num end]
> =A0 =A0 =A0 =A0 return $shifted
> =A0 =A0 }
>
> =A0 =A0 proc input_cap args {
> =A0 =A0 =A0 =A0 switch --exact -- [lshift args] {
> =A0 =A0 =A0 =A0 =A0 =A0 -pts {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 do your pts stuff with $args
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 -config {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 do your config stuff with $args
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 default {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts "'$option' is an invalid option"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print_usage
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return 1
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 return 0
> =A0 =A0 }
>
>
>
> > =A0Below is my proc itself but I am not sure if passing args in this wa=
y
> > =A0for the pattern above is correct.
>
> > =A0proc input_cap { args } {
> > =A0 =A0 =A0 if { [llength $args] =3D=3D 0 } {
> > =A0 =A0 =A0 =A0 =A0print_usage
> > =A0 =A0 =A0 =A0 =A0exit 1
>
> > =A0 =A0 =A0} else {
> > =A0 =A0 =A0 =A0 =A0print_usage
> > =A0 =A0 =A0 =A0 =A0exit 0
>
> > =A0 =A0 =A0}
> > =A0}
>
> > =A0Should I use { {arg1 ""} {arg2 ""}} instead of [lindex $args 0],
> > =A0[lindex $args 1], etc.?
>
> > =A0It's just that -pt and -config flags control the output generation a=
nd
> > =A0the rest of the list items =A0tell how this output should be generat=
ed.
>
> > =A0Please advise.
>
> > =A0Thank you.
> > =A0Hrachya
>
> --
> Glenn Jackman
> =A0 =A0 Write a wise saying and your name will live forever. -- Anonymous

Thank you for the pointers and the code, Glen! I appreciate your help.

What I am trying to achieve is the following:

1) The input_cap proc will receive input of the form:

=96pts =91fn40c tt25c sd25c sp8c ff25c=92  -config PiDoST =91i1 i2=92 =96co=
nfig
PiDoIT =91i3 i4=92 -config PiDoST =91i5 i6 i7 i8=92

The PiDoST/PiDoIT/PiDoST values in the -config option will be used to
create three columns of a table with column headings PiDoST/PiDoIT/
PiDoST.

Each such PiDoST/PiDoIT/PiDoST column will have four nested columns
whose names will be fn40c tt25c sd25c sp8c ff25c taken from the value
list of the -pts option.

So, both -pts and -config will be provided as input at the same time.

I guess your suggested code using switch can still be useful to handle
both options sequentially, i.e. I will use the -pts values to create
the general columns and the -config to create the nested columns
fn40c tt25c sd25c sp8c ff25c.

Does this sound reasonable?

Thank you.
Hrachya
0
Reply hrachyag 4/21/2010 2:11:54 PM

At 2010-04-21 10:11AM, "hrachyag" wrote:
[...]
>  What I am trying to achieve is the following:
>  
>  1) The input_cap proc will receive input of the form:
>  
>  -pts 'fn40c tt25c sd25c sp8c ff25c'  -config PiDoST 'i1 i2' -config
>  PiDoIT 'i3 i4' -config PiDoST 'i5 i6 i7 i8'
>  
>  The PiDoST/PiDoIT/PiDoST values in the -config option will be used to
>  create three columns of a table with column headings PiDoST/PiDoIT/
>  PiDoST.
>  
>  Each such PiDoST/PiDoIT/PiDoST column will have four nested columns
>  whose names will be fn40c tt25c sd25c sp8c ff25c taken from the value
>  list of the -pts option.
>  
>  So, both -pts and -config will be provided as input at the same time.

Well, then I'd do something like:

    proc input_cap args {
        while {[llength $args] > 0} {
            set args [lassign $args option]
            switch -exact -- $option {
                -pts {
                    set args [lassign $args pts]
                }
                -config {
                    set args [lassign $args colname coltype]
                    lappend configs [list $colname $coltype]
                }
                default {
                    error "oops, found invalid option: $option"
                }
            }
        }
        puts "pts = $pts"
        puts "configs = $configs"
    }

Then

    input_cap  \
        -pts "fn40c tt25c sd25c sp8c ff25c" \
        -config PiDoST "i1 i2" \
        -config PiDoIT "i3 i4" \
        -config PiDoST "i5 i6 i7 i8"


outputs:

    pts = fn40c tt25c sd25c sp8c ff25c
    configs = {PiDoST {i1 i2}} {PiDoIT {i3 i4}} {PiDoST {i5 i6 i7 i8}}


-- 
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous
0
Reply Glenn 4/21/2010 2:33:16 PM

On Apr 21, 7:33=A0pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2010-04-21 10:11AM, "hrachyag" wrote:
> [...]
>
>
>
> > =A0What I am trying to achieve is the following:
>
> > =A01) The input_cap proc will receive input of the form:
>
> > =A0-pts 'fn40c tt25c sd25c sp8c ff25c' =A0-config PiDoST 'i1 i2' -confi=
g
> > =A0PiDoIT 'i3 i4' -config PiDoST 'i5 i6 i7 i8'
>
> > =A0The PiDoST/PiDoIT/PiDoST values in the -config option will be used t=
o
> > =A0create three columns of a table with column headings PiDoST/PiDoIT/
> > =A0PiDoST.
>
> > =A0Each such PiDoST/PiDoIT/PiDoST column will have four nested columns
> > =A0whose names will be fn40c tt25c sd25c sp8c ff25c taken from the valu=
e
> > =A0list of the -pts option.
>
> > =A0So, both -pts and -config will be provided as input at the same time=
..
>
> Well, then I'd do something like:
>
> =A0 =A0 proc input_capargs{
> =A0 =A0 =A0 =A0 while {[llength $args] > 0} {
> =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argsoption]
> =A0 =A0 =A0 =A0 =A0 =A0 switch -exact -- $option {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -pts {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argspts]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -config {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argscolname colt=
ype]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 lappend configs [list $colname $c=
oltype]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 error "oops, found invalid option=
: $option"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 puts "pts =3D $pts"
> =A0 =A0 =A0 =A0 puts "configs =3D $configs"
> =A0 =A0 }
>
> Then
>
> =A0 =A0 input_cap =A0\
> =A0 =A0 =A0 =A0 -pts "fn40c tt25c sd25c sp8c ff25c" \
> =A0 =A0 =A0 =A0 -config PiDoST "i1 i2" \
> =A0 =A0 =A0 =A0 -config PiDoIT "i3 i4" \
> =A0 =A0 =A0 =A0 -config PiDoST "i5 i6 i7 i8"
>
> outputs:
>
> =A0 =A0 pts =3D fn40c tt25c sd25c sp8c ff25c
> =A0 =A0 configs =3D {PiDoST {i1 i2}} {PiDoIT {i3 i4}} {PiDoST {i5 i6 i7 i=
8}}
>
> --
> Glenn Jackman
> =A0 =A0 Write a wise saying and your name will live forever. -- Anonymous

Thank you for explanations, Glenn! My implementation is pretty much
similar to yours but I chose to do it a bit differently.

        set argi 0
	set configs {}
	set pvts {}
        while { $argi < $argc } {
            set option [lindex $args $argi]
            if { $option =3D=3D "-pts" } {
                if { $argi < $argc - 1 } {
                    incr argi
                    set pts [lindex $args $argi]
                } else {
                    print_usage
                    puts "Error: Missing argument \$pts for option
$option"
                    exit 1
                }
            } elseif { $option =3D=3D "-configs" }
.....
....
....

Thank you for pointers again!
Hrachya
0
Reply hrachyag 5/4/2010 10:18:24 AM

On Apr 21, 7:33=A0pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2010-04-21 10:11AM, "hrachyag" wrote:
> [...]
>
>
>
> > =A0What I am trying to achieve is the following:
>
> > =A01) The input_cap proc will receive input of the form:
>
> > =A0-pts 'fn40c tt25c sd25c sp8c ff25c' =A0-config PiDoST 'i1 i2' -confi=
g
> > =A0PiDoIT 'i3 i4' -config PiDoST 'i5 i6 i7 i8'
>
> > =A0The PiDoST/PiDoIT/PiDoST values in the -config option will be used t=
o
> > =A0create three columns of a table with column headings PiDoST/PiDoIT/
> > =A0PiDoST.
>
> > =A0Each such PiDoST/PiDoIT/PiDoST column will have four nested columns
> > =A0whose names will be fn40c tt25c sd25c sp8c ff25c taken from the valu=
e
> > =A0list of the -pts option.
>
> > =A0So, both -pts and -config will be provided as input at the same time=
..
>
> Well, then I'd do something like:
>
> =A0 =A0 proc input_capargs{
> =A0 =A0 =A0 =A0 while {[llength $args] > 0} {
> =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argsoption]
> =A0 =A0 =A0 =A0 =A0 =A0 switch -exact -- $option {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -pts {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argspts]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -config {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 setargs[lassign $argscolname colt=
ype]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 lappend configs [list $colname $c=
oltype]
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 default {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 error "oops, found invalid option=
: $option"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 puts "pts =3D $pts"
> =A0 =A0 =A0 =A0 puts "configs =3D $configs"
> =A0 =A0 }
>
> Then
>
> =A0 =A0 input_cap =A0\
> =A0 =A0 =A0 =A0 -pts "fn40c tt25c sd25c sp8c ff25c" \
> =A0 =A0 =A0 =A0 -config PiDoST "i1 i2" \
> =A0 =A0 =A0 =A0 -config PiDoIT "i3 i4" \
> =A0 =A0 =A0 =A0 -config PiDoST "i5 i6 i7 i8"
>
> outputs:
>
> =A0 =A0 pts =3D fn40c tt25c sd25c sp8c ff25c
> =A0 =A0 configs =3D {PiDoST {i1 i2}} {PiDoIT {i3 i4}} {PiDoST {i5 i6 i7 i=
8}}
>
> --
> Glenn Jackman
> =A0 =A0 Write a wise saying and your name will live forever. -- Anonymous

Thank you for explanations, Glenn! My implementation is pretty much
similar to yours but I chose to do it a bit differently.

        set argi 0
        set configs {}
        set pts {}
        while { $argi < $argc } {
            set option [lindex $args $argi]
            if { $option =3D=3D "-pts" } {
                if { $argi < $argc - 1 } {
                    incr argi
                    set pts [lindex $args $argi]
                } else {
                    print_usage
                    puts "Error: Missing argument \$pts for option
$option"
                    exit 1
                }
            } elseif { $option =3D=3D "-configs" }
.....
....
....

Thank you for pointers again!
Hrachya
0
Reply hrachyag 5/4/2010 10:20:49 AM

5 Replies
376 Views

(page loaded in 0.212 seconds)

Similiar Articles:













7/30/2012 2:30:26 AM


Reply: