add cmd unset

This commit is contained in:
Matthias@Dell 2023-11-19 19:28:22 +01:00
parent 8ff1093877
commit 36efa54904
2 changed files with 24 additions and 2 deletions

View File

@ -97,6 +97,19 @@ Same as `set`, but it returns the value of the variable that is being set. This
### default ### default
Same as `set`, but it sets the variable's value only if it has no value yet. Same as `set`, but it sets the variable's value only if it has no value yet.
### unset
Unset a variable
**Synopsis**:
Unset `varname`, it will no longer be defined and can therefor be set with `default` again.
`<!-- #unset varname -->`
**Argument**:
Name of the variable
**Return Value**:
Empty string
--- ---
### comment ### comment

View File

@ -273,7 +273,7 @@ def cmd_return(args: str, variables:dict[str, str]={}) -> str:
# pdebug(f"cmd_set: varname='{args[:space]}, 'arg='{args[space+1:]}', variables='{variables}'") # pdebug(f"cmd_set: varname='{args[:space]}, 'arg='{args[space+1:]}', variables='{variables}'")
if not (space > 0 and space < len(args)-1): if not (space > 0 and space < len(args)-1):
variables[args] = "" variables[args] = ""
pdebug(f"cmd_set: Setting to emptry string: {args}") pdebug(f"cmd_set: Setting to empty string: {args}")
else: else:
varname = args[:space] varname = args[:space]
variables[varname] = "" variables[varname] = ""
@ -308,6 +308,14 @@ def cmd_set(args: str, variables:dict[str, str]={}) -> str:
cmd_return(args, variables) cmd_return(args, variables)
return "" return ""
def cmd_unset(args: str, variables:dict[str, str]={}) -> str:
variable = args.strip(' ')
if variable not in variables:
pdebug(f"unset: variable '{variable}' is not set", level=error_levels["light"])
else:
variables.pop(variable)
return ""
def cmd_default(args: str, variables:dict[str, str]={}) -> str: def cmd_default(args: str, variables:dict[str, str]={}) -> str:
separator = args.find(' ') separator = args.find(' ')
if args[:separator] not in variables: if args[:separator] not in variables:
@ -331,8 +339,9 @@ def cmd_warning(args: str, variables:dict[str, str]={}) -> str:
command2function:dict[str, Callable[[str, dict[str,str]], str]] = { command2function:dict[str, Callable[[str, dict[str,str]], str]] = {
"include": cmd_include, "include": cmd_include,
"section": cmd_section, "section": cmd_section,
"set": cmd_set,
"return": cmd_return, "return": cmd_return,
"set": cmd_set,
"unset": cmd_unset,
"default": cmd_default, "default": cmd_default,
"comment": cmd_comment, "comment": cmd_comment,
"uncomment": cmd_uncomment, "uncomment": cmd_uncomment,