summaryrefslogtreecommitdiffstats
path: root/tcllib/examples/tepam/1d_procedure_validation.demo
blob: b8710b0ae3c6d0b61729c00e0aa9df95c60bf935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
##########################################################################
# TEPAM - Tcl's Enhanced Procedure and Argument Manager
##########################################################################
#
# 1d_procedure_validation.demo: This file is part of the TEPAM demo
#
# Copyright (C) 2009, 2010 Andreas Drollinger
# 
# Id: 1d_procedure_validation.demo
##########################################################################
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
##########################################################################

#### Initialization ####

DemoControl(Initialization) 1
DemoControl(IsExecutable) {0}

# This demo shows how custom argument checks can be implemented using 
# the -validatecommand attribute.

   package require Tk
   package require tepam
   namespace import -force tepam::*; # Import tepam::procedure and tepam::argument_dialogbox

####  Custom validation with standard error text ####

DemoControl(IsExecutable) {[info commands div_entire1]=={}}

# The following procedure performs integer divisions and is refusing 
# the dividend/divisor pair if the integer division has a reminder.
# It is checked that each argument is an integer using the 
# -validatecommand argument attribute (specifying the argument types
# as integer would do the same thing).
# Furthermore, an additional procedure attribute -validatecommand
# checks that the division is not leading to a reminder.

  procedure div_entire1 {
      -short_description "Integer division"
      -description "This procedure performs divisions of integer values."
      -args {
         {a -validatecommand {string is integer %P}}
         {b -validatecommand {string is integer %P}}
      }
      -validatecommand {expr {($a % $b)==0}}
   } {
      expr {$a/$b}
   }

# The following lines show two procedure calls that are failing as well
# as a 3rd one that is successful. The default error messages are created
# in these examples:

   div_entire1 10.0 5

   div_entire1 10 3

   div_entire1 10 5


####  Custom validation with custom error text ####

DemoControl(IsExecutable) {[info commands div_entire2]=={}}

# The standard error messages that are generated if a custom
# validation fails may not be very meaningful, as the last example has
# shown.
# The following example defines custom error texts via the attribute
# -validatecommand_error_text:

  procedure div_entire2 {
      -short_description "Integer division"
      -description "This procedure performs divisions of integer values."
      -args {
         {a -validatecommand {string is integer %P} -validatecommand_error_text "Integer required for 'a'"}
         {b -validatecommand {string is integer %P} -validatecommand_error_text "Integer required for 'b'"}
      }
      -validatecommand {expr {($a % $b)==0}} -validatecommand_error_text "'a' divided by 'b' has a reminder"
   } {
      expr {$a/$b}
   }

# The next line show that the error messages may be much more meaningful
# with the text defined with -validatecommand_error_text:

   div_entire2 10.0 5

   div_entire2 10 3

   div_entire2 10 5

##########################################################################
# Id: 1d_procedure_validation.demo,v $
# Modifications:
#
# Revision 1.1  2013/10/14 droll
# * Initial checkin of this file
##########################################################################