diff options
author | hobbs <hobbs> | 2000-06-01 00:33:27 (GMT) |
---|---|---|
committer | hobbs <hobbs> | 2000-06-01 00:33:27 (GMT) |
commit | 58476f96744cb7143b4129af3d1978f949d8b474 (patch) | |
tree | 27af8e10e0015c6de9e8f5eafcd39254d32ca5b9 | |
parent | fb987e05c7e3613f4a27e0f9262e29cbda36e51a (diff) | |
download | tcl-58476f96744cb7143b4129af3d1978f949d8b474.zip tcl-58476f96744cb7143b4129af3d1978f949d8b474.tar.gz tcl-58476f96744cb7143b4129af3d1978f949d8b474.tar.bz2 |
* tests/set-old.test:
* doc/unset.n:
* generic/tclVar.c (Tcl_UnsetObjCmd): added -nocomplain and --
options to unset, to allow for a silent unset operation.
-rw-r--r-- | doc/unset.n | 20 | ||||
-rw-r--r-- | generic/tclVar.c | 30 | ||||
-rw-r--r-- | tests/set-old.test | 43 |
3 files changed, 79 insertions, 14 deletions
diff --git a/doc/unset.n b/doc/unset.n index ef93132..6aca9d3 100644 --- a/doc/unset.n +++ b/doc/unset.n @@ -1,20 +1,21 @@ '\" '\" Copyright (c) 1993 The Regents of the University of California. '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. +'\" Copyright (c) 2000 Ajuba Solutions. '\" '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: unset.n,v 1.2 1998/09/14 18:39:56 stanton Exp $ +'\" RCS: @(#) $Id: unset.n,v 1.3 2000/06/01 00:33:27 hobbs Exp $ '\" .so man.macros -.TH unset n "" Tcl "Tcl Built-In Commands" +.TH unset n 8.4 Tcl "Tcl Built-In Commands" .BS '\" Note: do not modify the .SH NAME line immediately below! .SH NAME unset \- Delete variables .SH SYNOPSIS -\fBunset \fIname \fR?\fIname name ...\fR? +\fBunset \fR?\fI\-\-\fR? ?\fI\-nocomplain\fR? ?\fIname name name ...\fR? .BE .SH DESCRIPTION @@ -27,8 +28,17 @@ element is removed without affecting the rest of the array. If a \fIname\fR consists of an array name with no parenthesized index, then the entire array is deleted. The \fBunset\fR command returns an empty string as result. -An error occurs if any of the variables doesn't exist, and any variables -after the non-existent one are not deleted. +.VS 8.4 +If \fI\-nocomplain\fR is specified as the first argument, any possible +errors are suppressed. The option may not be abbreviated, in order to +disambiguate it from possible variable names. \fI\-\-\fR may be specified +prior to \fI\-nocomplain\fR to prevent it from being interpreted as an +option. +.VE 8.4 +If an error occurs, any variables after the named one causing the error not +deleted. An error can occur when the named variable doesn't exist, or the +name refers to an array element but the variable is a scalar, or the name +refers to a variable in a non-existent namespace. .SH KEYWORDS remove, variable diff --git a/generic/tclVar.c b/generic/tclVar.c index 8c279a0..8147f45 100644 --- a/generic/tclVar.c +++ b/generic/tclVar.c @@ -14,7 +14,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclVar.c,v 1.18 2000/05/31 15:03:34 ericm Exp $ + * RCS: @(#) $Id: tclVar.c,v 1.19 2000/06/01 00:33:27 hobbs Exp $ */ #include "tclInt.h" @@ -2589,18 +2589,34 @@ Tcl_UnsetObjCmd(dummy, interp, objc, objv) int objc; /* Number of arguments. */ Tcl_Obj *CONST objv[]; /* Argument objects. */ { - register int i; + register int i, flags = TCL_LEAVE_ERR_MSG; register char *name; if (objc < 2) { - Tcl_WrongNumArgs(interp, 1, objv, "varName ?varName ...?"); + Tcl_WrongNumArgs(interp, 1, objv, + "?--? ?-nocomplain? ?varName varName ...?"); return TCL_ERROR; } - - for (i = 1; i < objc; i++) { + + /* + * Very simple, restrictive argument parsing. The only options are + * -- and -nocomplain (which must come first to be an option). + */ + i = 1; + name = TclGetString(objv[i]); + if ((name[0] == '-') && (strcmp("-nocomplain", name) == 0)) { + flags = 0; + i++; + name = TclGetString(objv[i]); + } + if ((name[0] == '-') && (strcmp("--", name) == 0)) { + i++; + } + + for (; i < objc; i++) { name = TclGetString(objv[i]); - if (Tcl_UnsetVar2(interp, name, (char *) NULL, - TCL_LEAVE_ERR_MSG) != TCL_OK) { + if ((Tcl_UnsetVar2(interp, name, (char *) NULL, flags) != TCL_OK) + && (flags == TCL_LEAVE_ERR_MSG)) { return TCL_ERROR; } } diff --git a/tests/set-old.test b/tests/set-old.test index d9f4084..a4c061a 100644 --- a/tests/set-old.test +++ b/tests/set-old.test @@ -13,7 +13,7 @@ # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # -# RCS: @(#) $Id: set-old.test,v 1.10 2000/05/31 15:03:35 ericm Exp $ +# RCS: @(#) $Id: set-old.test,v 1.11 2000/06/01 00:33:27 hobbs Exp $ if {[lsearch [namespace children] ::tcltest] == -1} { package require tcltest @@ -204,7 +204,7 @@ test set-old-7.1 {unset command} { } {0 0 0 1} test set-old-7.2 {unset command} { list [catch {unset} msg] $msg -} {1 {wrong # args: should be "unset varName ?varName ...?"}} +} {1 {wrong # args: should be "unset ?--? ?-nocomplain? ?varName varName ...?"}} test set-old-7.3 {unset command} { catch {unset a} list [catch {unset a} msg] $msg @@ -266,6 +266,45 @@ test set-old-7.11 {unset command} { unset a list [catch {set a(14)} msg] $msg [catch {array names a} msg2] $msg2 } {1 {can't read "a(14)": no such variable} 0 {}} +test set-old-7.12 {unset command, -nocomplain} { + catch {unset a} + list [info exists a] [catch {unset -nocomplain a}] [info exists a] +} {0 0 0} +test set-old-7.13 {unset command, -nocomplain} { + set -nocomplain abc + list [info exists -nocomplain] [catch {unset -nocomplain}] \ + [info exists -nocomplain] [catch {unset -- -nocomplain}] \ + [info exists -nocomplain] +} {1 0 1 0 0} +test set-old-7.14 {unset command, --} { + set -- abc + list [info exists --] [catch {unset --}] \ + [info exists --] [catch {unset -- --}] \ + [info exists --] +} {1 0 1 0 0} +test set-old-7.15 {unset command, -nocomplain} { + set -nocomplain abc + set -- abc + list [info exists -nocomplain] [catch {unset -- -nocomplain}] \ + [info exists -nocomplain] [info exists --] \ + [catch {unset -- -nocomplain}] [info exists --] \ + [catch {unset -- --}] [info exists --] +} {1 0 0 1 1 1 0 0} +test set-old-7.16 {unset command, -nocomplain} { + set -nocomplain abc + set var abc + list [info exists bogus] [catch {unset -nocomplain bogus var bogus}] \ + [info exists -nocomplain] [info exists var] \ + [catch {unset -nocomplain -nocomplain}] [info exists -nocomplain] +} {0 0 1 0 0 0} +test set-old-7.17 {unset command, -nocomplain (no abbreviation)} { + set -nocomp abc + list [info exists -nocomp] [catch {unset -nocomp}] [info exists -nocomp] +} {1 0 0} +test set-old-7.18 {unset command, -nocomplain (no abbreviation)} { + catch {unset -nocomp} + list [info exists -nocomp] [catch {unset -nocomp}] +} {0 1} # Array command. |