2013-04-30 1 views
1

나는 기대 스크립트를 가지고 내가tcl에서 명령 줄에 스위치를 어떻게 지정합니까?

proc ABLOCK { } { 

} 

proc BBLOCK { } { 

} 

proc CBLOCK { } { 

} 

다음 스크립트를 실행하는 것은 내가 몇 가지를 사용할 수 있는지 동안 아래처럼 내 코드에서 몇 가지 절차가있는 경우 requirement.suppose에 따라 코드의 특정 부분을 실행하려면 스위치처럼.

./script -A ABLOCK #executes only ABLOCK 
./script -A ABLOCK -B BBLOCK #executes ABLOCK and BBLOCK 
./script -V # just an option for say verbose output 

어디 ABLOCK, BBLOCK는 CBLOCK는 인수 argv의 목록

+0

: 당신이 스위치 인수를해야하는 경우

proc -V {} { set ::verbose 1 # Enable some other output } 

을 다음을 수행 할 수 명령 줄 인수를 구문 분석하는 가장 일반적인 방법은 TCLLIB의 일부인 [cmdline] (http://tcllib.sourceforge.net/doc/cmdline.html)을 사용하는 것입니다. 더 많은 것들이 [http://wiki.tcl.tk/1730](http://wiki.tcl.tk/1730)]에 나와 있습니다. – potrzebie

답변

2

왜 수 :

foreach arg $argv { 
    $arg 
} 

하고있는 경우,

사람도 exit를 전달할 수 ./script ABLOCK BLOCK CBLOCK로 실행 원하지 않으면 유효한지 확인하십시오 :

(그들은 매개 변수 필요하지 않은 경우) 동일로 전환 10
foreach arg $argv { 
    if {$arg in {ABLOCK BLOCK CBLOCK}} { 
     $arg 
    } else { 
     # What else? 
    } 
} 

, 당신은 할 수 :

set myargs $argv 
while {[llength $myargs]} { 
    set myargs [lassign $myargs arg] 
    if {[string index $arg 0] eq {-}} { 
     # Option 
     if {[string index $arg 1] eq {-}} { 
      # Long options 
      switch -exact -- [string range $arg 2 end] 
       verbose {set ::verbose 1} 
       logfile {set myargs [lassign $myargs ::logfile]} 
      } 
     } else { 
      foreach opt [split [string range $arg 1 end] {}] { 
       switch -exact $opt { 
        V {set ::verbose 1} 
        l {set myargs [lassign $myargs ::logfile]} 
       } 
      } 
     } 
    } else { 
     $arg 
    } 
} 
관련 문제