cmdr
Check-in [3305575764]
Not logged in

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

Overview
Comment:Simplified approach to specification of custom messages. Command variants taking it as argument. Plus variants which leave out the intro containing parameter information (name, type).
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3305575764a2ae2c946a0380927127f159ad34ad
User & Date: andreask 2015-06-26 23:24:26.244
References
2015-06-26
21:58
Design mistake --- Closing --- Extended handling of "fail-unknown-thing" (fut) with hooks allowing this general code to ask for custom messaging. Directly from the parameter, indirectly from the validation type. Whose "validate" method is where "fail-unknown-thing" got called from. Which means that we have a possible alternative solution which does not go through such a convoluted circle: New convenience function, an extension of "fail-unknown-thing" which simply takes the custom message as a parameter. And we can write more functions of this type then, which take ranges, dicts, etc. and operate on these for rendering, or a possible user-choice to fix the bad value. Saving the current state before making a decision --- Trunk went forward with the alternate approach, see [3305575764]. Closed-Leaf check-in: 86ce9388c1 user: andreask tags: x-fut-acceptable
Context
2015-07-03
05:01
Merge VT test work back to main line. check-in: dd4c6bd2ac user: aku tags: trunk
2015-07-02
21:53
New validation type, time as (hour:)minute offset from midnight. Closed-Leaf check-in: a2015467df user: aku tags: vt-minute
2015-06-26
23:24
Simplified approach to specification of custom messages. Command variants taking it as argument. Plus variants which leave out the intro containing parameter information (name, type). check-in: 3305575764 user: andreask tags: trunk
2015-05-12
22:15
config, color - Bump version numbers, due to their recent changes. check-in: 4f21251865 user: aku tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to validate.tcl.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

proc ::cmdr::validate::boolean::validate {p x} {
    debug.cmdr/validate {}
    if {[string is boolean -strict $x]} {
	# Double inverse keeps value, and makes it canonical.
	return [expr {!!$x}]
    }
    fail $p BOOLEAN "a boolean" $x
}

# # ## ### ##### ######## ############# #####################
## Any integer

namespace eval ::cmdr::validate::integer {
    namespace export default validate complete release







|







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

proc ::cmdr::validate::boolean::validate {p x} {
    debug.cmdr/validate {}
    if {[string is boolean -strict $x]} {
	# Double inverse keeps value, and makes it canonical.
	return [expr {!!$x}]
    }
    fail $p BOOLEAN "a boolean (yes, no, false, true, on, off, 0, or 1)" $x
}

# # ## ### ##### ######## ############# #####################
## Any integer

namespace eval ::cmdr::validate::integer {
    namespace export default validate complete release
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    return {}
}
proc ::cmdr::validate::percent::validate {p x} {
    debug.cmdr/validate {}
    if {[string is double -strict $x] &&
	($x >= 0) &&
	($x <= 100)} { return $x }
    fail $p PERCENT "a percentage" $x
}

# # ## ### ##### ######## ############# #####################
## Any string

namespace eval ::cmdr::validate::identity {
    namespace export default validate complete release







|







151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    return {}
}
proc ::cmdr::validate::percent::validate {p x} {
    debug.cmdr/validate {}
    if {[string is double -strict $x] &&
	($x >= 0) &&
	($x <= 100)} { return $x }
    fail $p PERCENT "a percentage (\[0...100\])" $x
}

# # ## ### ##### ######## ############# #####################
## Any string

namespace eval ::cmdr::validate::identity {
    namespace export default validate complete release
535
536
537
538
539
540
541
542
543
    if {![file readable $path]} {return 0}
    if {![file writable $path]} {return 0}
    return 1
}

# # ## ### ##### ######## ############# #####################
## Ready
package provide cmdr::validate 1.3.1
return







|

535
536
537
538
539
540
541
542
543
    if {![file readable $path]} {return 0}
    if {![file writable $path]} {return 0}
    return 1
}

# # ## ### ##### ######## ############# #####################
## Ready
package provide cmdr::validate 1.3.2
return
Changes to vcommon.tcl.
35
36
37
38
39
40
41
42

43
44
45
46
47
48
49
50
    namespace export common
    namespace ensemble create
}

namespace eval ::cmdr::validate::common {
    namespace export \
	complete-enum complete-glob complete-substr \
	ok-directory lead-in fail fail-unknown-thing \

	fail-known-thing
    namespace ensemble create
}

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

debug define cmdr/validate/common
debug level  cmdr/validate/common







|
>
|







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    namespace export common
    namespace ensemble create
}

namespace eval ::cmdr::validate::common {
    namespace export \
	complete-enum complete-glob complete-substr \
	ok-directory strip-lead-in lead-in fail \
	fail-unknown-thing fail-unknown-thing-msg fail-unknown-simple fail-unknown-simple-msg \
	fail-known-thing   fail-known-thing-msg   fail-known-simple   fail-known-simple-msg
    namespace ensemble create
}

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

debug define cmdr/validate/common
debug level  cmdr/validate/common
69
70
71
72
73
74
75
76



































77
78
79
80
81
82
83

































84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101










102
103
104
105
106
107
108

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] \"$x\" does not exist$context."
    append msg " Please use a different value."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}




































proc ::cmdr::validate::common::fail-known-thing {p code type x {context {}}} {
    # Specific failure for a named thing: Expected non-existence, found a definition.
    debug.cmdr/validate/common {}

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] named \"$x\" already exists$context."
    append msg " Please use a different name."


































    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

# # ## ### ##### ######## ############# #####################
## Support commands for construction of messages.

proc ::cmdr::validate::common::lead-in {type} {
    if {[string match {A *}  $type] ||
	[string match {An *} $type]} {
	set lead {}
    } elseif {[string match {[aeiouAEIOU]*} $type]} {
	set lead {An }
    } else {
	set lead {A }
    }
    return $lead$type
}











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

proc ::cmdr::validate::common::complete-enum {choices nocase buffer} {
    # As a helper function for command completion printing anything
    # here would mix with the output of linenoise. Do that only on
    # explicit request (level 10).








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















>
>
>
>
>
>
>
>
>
>







70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] \"$x\" does not exist$context."
    append msg " Please use a different value."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-unknown-thing-msg {usermsg p code type x {context {}}} {
    # Specific failure for a named thing: Expected existence, found it missing.
    # Takes a custom message to place into the error.
    debug.cmdr/validate/common {}

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] \"$x\" does not exist$context."
    append msg " " $usermsg "."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-unknown-simple {p code type x {context {}}} {
    # Specific failure for a named thing: Expected existence, found it missing.
    # Simplified intro, leaving out the parameter information (input|option, name)
    debug.cmdr/validate/common {}

    append msg "[string to-title [strip-lead-in $type]] \"$x\" does not exist$context."
    append msg " Please use a different value."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-unknown-simple-msg {usermsg p code type x {context {}}} {
    # Specific failure for a named thing: Expected existence, found it missing.
    # Takes a custom message to place into the error.
    # Simplified intro, leaving out the parameter information (input|option, name)
    debug.cmdr/validate/common {}

    append msg "[string totitle [strip-lead-in $type]] \"$x\" does not exist$context."
    append msg " " $usermsg "."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-known-thing {p code type x {context {}}} {
    # Specific failure for a named thing: Expected non-existence, found a definition.
    debug.cmdr/validate/common {}

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] named \"$x\" already exists$context."
    append msg " Please use a different name."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-known-thing-msg {usermsg p code type x {context {}}} {
    # Specific failure for a named thing: Expected non-existence, found a definition.
    debug.cmdr/validate/common {}

    append msg "Found a problem with [$p type] \"[$p the-name]\":"
    append msg " [lead-in $type] named \"$x\" already exists$context."
    append msg " " $usermsg "."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-known-simple {p code type x {context {}}} {
    # Specific failure for a named thing: Expected non-existence, found a definition.
    # Simplified intro, leaving out the parameter information (input|option, name)
    debug.cmdr/validate/common {}

    append msg " [string totitle [strip-lead-in $type]] named \"$x\" already exists$context."
    append msg " Please use a different name."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

proc ::cmdr::validate::common::fail-known-simple-msg {usermsg p code type x {context {}}} {
    # Specific failure for a named thing: Expected non-existence, found a definition.
    # Simplified intro, leaving out the parameter information (input|option, name)
    debug.cmdr/validate/common {}

    append msg " [string totitle [strip-lead-in $type]] named \"$x\" already exists$context."
    append msg " " $usermsg "."

    return -code error -errorcode [list CMDR VALIDATE {*}$code] $msg
}

# # ## ### ##### ######## ############# #####################
## Support commands for construction of messages.

proc ::cmdr::validate::common::lead-in {type} {
    if {[string match {A *}  $type] ||
	[string match {An *} $type]} {
	set lead {}
    } elseif {[string match {[aeiouAEIOU]*} $type]} {
	set lead {An }
    } else {
	set lead {A }
    }
    return $lead$type
}

proc ::cmdr::validate::common::strip-lead-in {type} {
    if {[string match {A *} $type]} {
	return [string range $type 2 end]
    } elseif {[string match {An *} $type]} {
	return [string range $type 3 end]
    } else {
	return $type
    }
}

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

proc ::cmdr::validate::common::complete-enum {choices nocase buffer} {
    # As a helper function for command completion printing anything
    # here would mix with the output of linenoise. Do that only on
    # explicit request (level 10).
188
189
190
191
192
193
194
195
196
    if {![file isdirectory $path]} {return 0}
    if {![file writable    $path]} {return 0}
    return 1
}

# # ## ### ##### ######## ############# #####################
## Ready
package provide cmdr::validate::common 1.2
return







|

267
268
269
270
271
272
273
274
275
    if {![file isdirectory $path]} {return 0}
    if {![file writable    $path]} {return 0}
    return 1
}

# # ## ### ##### ######## ############# #####################
## Ready
package provide cmdr::validate::common 1.3
return