#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
########################################################################
#
# An example program and kind of a manual for ::clig.
#
# (C) 1999-2001 Harald Kirsch (kirschh@lionbioscience.com)
#
# $Revision: 1.3 $, $Date: 2001/01/21 15:06:08 $
########################################################################

package require clig
namespace import ::clig::\[A-Za-z\]*

########################################################################

##
## Declare the command line of this script. All except the `Rest'
## entry are only examples and serve no real purpose.
##

## Dictated by backwards compatibility, the declaration-functions
## below cannot take the name of their database (tcl-array) as a
## parameter. It has to be set globally, and it must be a
## namespace-absolute.
setSpec ::main

## Declare a usage-oneliner for this script.
Usage {
  Demonstrate some features of clig and show usage of functions in
  package clig. All options are just for playing with them. But you
  can also request a usage-message for the functions of clig.
}

## Declare option -s to accept between 4 and 7 args
String -s sort \
    {will print its args lsorted} \
    -c 4 7 

## Declare option -l to accept exactly 6 integer args in the range [1,49]
Int -l lotto \
    {tell me your lotto numbers} \
    -c 6 6 \
    -r 1 49

## Declare option -pi to accept exactly one float arg in the range
## [3.1,3.2] with a default of 3.14.
Float -pi pi \
    {you may specify your private version of the magic number pi} \
    -c 1 1 \
    -r 3.1 3.2 \
    -d 3.14

## Allow for at most one command line argument not being an argument
## for one of the options declared.
Rest func \
    "show usage for clig-function func, where func is one of 
Flag, String, Float, Int, Rest, Usage or parseCmdline" \
    -c 0 1

## a convenience shortcut
set Program [file tail $argv0]

## parse the command line along the specification (main). Use $Program 
## to tag error-message, if necessary.
if {[catch {parseCmdline main $Program $argc $argv} err]} {
  #puts stderr $errorInfo
  puts stderr $err
  exit 1
}

## Run little useless code sippets as requested on the command line
if {[info exist sort]} {
  puts "-s: [lsort $sort]"
}
if {[info exist fun]} {
  puts "-f: yes, we're having fun"
}
if {[info exist lotto]} {
  puts "-l: thanks for choosing $lotto"
}

if {![info exist func]} {
  puts "Ok, obviously pi=$pi"
  exit 0
}

## Obviously $func is set. Print its usage-message. Of course this
## does not work for functions not instrumented.
switch -exact -- $func {
  Flag {
    catch {::clig::usage ::clig::FlagSpec ::clig::Flag {}} err
  }
  parseCmdline {
    catch {::clig::usage ::clig::parseCmdlineSpec \
	       ::clig::parseCmdline {}} err
  }
  Usage {
    catch {::clig::usage ::clig::UsageSpec \
	       ::clig::Usage {}} err
  }
  default {
    setSpec ::dummy
    if {[info exist ::clig::${func}Spec]} {
      catch {$func d blurb adf -h} err
    } else {
      set err "don't know anything about `$func'"
    }
  }
}

if {[regexp {usage.*} $err msg]} {
  puts $msg
} else {
  puts stderr $err
}
########################################################################
## Local Variables: ##
## mode:tcl ##
## End: ##
