From: Hrachya.Aghajanyan on
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
From: Glenn Jackman on
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(a)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
From: hrachyag on
On Apr 21, 1:03 am, Glenn Jackman <gle...(a)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...(a)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

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:

–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.

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
From: Glenn Jackman on
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
From: hrachyag on
On Apr 21, 7:33 pm, Glenn Jackman <gle...(a)ncf.ca> wrote:
> 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_capargs{
>         while {[llength $args] > 0} {
>             setargs[lassign $argsoption]
>             switch -exact -- $option {
>                 -pts {
>                     setargs[lassign $argspts]
>                 }
>                 -config {
>                     setargs[lassign $argscolname 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

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 == "-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 == "-configs" }
.....
....
....

Thank you for pointers again!
Hrachya