cmdr
Check-in [8e18e110df]
Not logged in
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:cmdr::validate::common - Added commands to generate more specific error messages, while still general - From stackato client. Bumped version to 1.2.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8e18e110df650dfec0f8643dab8862859a6169e5
User & Date: andreask 2014-04-16 19:46:22
Context
2014-04-22
06:56
Merged branch back, making the channel v-types official. check-in: c7a27f1422 user: aku tags: trunk
06:55
Merged trunk extensions. Closed-Leaf check-in: 6163942da7 user: aku tags: more-vtypes
2014-04-16
19:46
cmdr::validate::common - Added commands to generate more specific error messages, while still general - From stackato client. Bumped version to 1.2. check-in: 8e18e110df user: andreask tags: trunk
2014-04-15
21:04
Fixed multi-section help setup of the limit command. check-in: abb1eb8115 user: andreask tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to vcommon.tcl.

    33     33   
    34     34   namespace eval ::cmdr::validate {
    35     35       namespace export common
    36     36       namespace ensemble create
    37     37   }
    38     38   
    39     39   namespace eval ::cmdr::validate::common {
    40         -    namespace export fail complete-enum complete-glob
           40  +    namespace export \
           41  +	complete-enum complete-glob \
           42  +	fail fail-unknown-thing fail-known-thing \
           43  +	p-name lead-in
    41     44       namespace ensemble create
    42     45   }
    43     46   
    44     47   # # ## ### ##### ######## ############# #####################
    45     48   
    46     49   debug define cmdr/validate/common
    47     50   debug level  cmdr/validate/common
    48     51   debug prefix cmdr/validate/common {[debug caller] | }
    49     52   
    50     53   # # ## ### ##### ######## ############# #####################
           54  +## Different forms of validation failure messages
    51     55   
    52         -proc ::cmdr::validate::common::fail {p code type x} {
           56  +proc ::cmdr::validate::common::fail {p code type x {context {}}} {
           57  +    # Generic failure: "Expected foo, got x".
           58  +    debug.cmdr/validate/common {}
           59  +
           60  +    append msg "Expected $type for [$p type] \"[p-name $p]\"$context,"
           61  +    append msg " got \"$x\""
           62  +
           63  +    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
           64  +}
           65  +
           66  +proc ::cmdr::validate::common::fail-unknown-thing {p code type x {context {}}} {
           67  +    # Specific failure for a named thing: Expected existence, found it missing.
           68  +    debug.cmdr/validate/common {}
           69  +
           70  +    append msg "Found a problem with [$p type] \"[p-name $p]\":"
           71  +    append msg " [lead-in $type] \"$x\" does not exist$context."
           72  +    append msg " Please use a different value."
           73  +
           74  +    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
           75  +}
           76  +
           77  +proc ::cmdr::validate::common::fail-known-thing {p code type x {context {}}} {
           78  +    # Specific failure for a named thing: Expected non-existence, found a definition.
    53     79       debug.cmdr/validate/common {}
    54     80   
    55         -    # Determine type of p: state, option, or input.  Use this to
    56         -    # choose a proper identifying string in the generated message.
           81  +    append msg "Found a problem with [$p type] \"[p-name $p]\":"
           82  +    append msg " [lead-in $type] named \"$x\" already exists$context."
           83  +    append msg " Please use a different name."
    57     84   
    58         -    set ptype [$p type]
           85  +    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
           86  +}
           87  +
           88  +# # ## ### ##### ######## ############# #####################
           89  +## Support commands for construction of messages.
           90  +
           91  +proc ::cmdr::validate::common::lead-in {type} {
           92  +    if {[string match {A *}  $type] ||
           93  +	[string match {An *} $type]} {
           94  +	set lead {}
           95  +    } elseif {[string match {[aeiouAEIOU]*} $type]} {
           96  +	set lead {An }
           97  +    } else {
           98  +	set lead {A }
           99  +    }
          100  +    return $lead$type
          101  +}
    59    102   
    60         -    if {$ptype eq "option"} {
    61         -	set name [$p flag]
          103  +proc ::cmdr::validate::common::p-name {p} {
          104  +    if {[$p type] eq "option"} {
          105  +	return [$p flag]
    62    106       } else {
    63         -	set name [$p label]
          107  +	return [$p label]
    64    108       }
    65         -    return -code error -errorcode [list CMDR VALIDATE {*}$code] \
    66         -	"Expected $type for $ptype \"$name\", got \"$x\""
    67    109   }
          110  +
          111  +# # ## ### ##### ######## ############# #####################
    68    112   
    69    113   proc ::cmdr::validate::common::complete-enum {choices nocase buffer} {
    70    114       # As a helper function for command completion printing anything
    71    115       # here would mix with the output of linenoise. Do that only on
    72    116       # explicit request (level 10).
    73    117       debug.cmdr/validate/common {} 10
    74    118   
................................................................................
   105    149   
   106    150       debug.cmdr/validate/common {= [join $candidates "\n= "]} 10
   107    151       return $candidates
   108    152   }
   109    153   
   110    154   # # ## ### ##### ######## ############# #####################
   111    155   ## Ready
   112         -package provide cmdr::validate::common 1.1
          156  +package provide cmdr::validate::common 1.2
   113    157   return