Update a configuration file: Difference between revisions

→‎{{header|TXR}}: Fixed section header, added explanation, and a couple of test runs for some corner cases.
(→‎{{header|TXR}}: Fixed section header, added explanation, and a couple of test runs for some corner cases.)
Line 561:
$cfg save</lang>
 
=={{header|TXR}}==
{{works with|TXR git head}}
 
Line 575:
VAR=VAL # ensure "VAR VAL" in the config file
</pre>
 
This works by reading the configuration into a variable, and then making multiple passes over it, using the same constructs that normally operate on files or pipes. The first 30% of the script deals with reading the configuration file and parsing each command line argument, and converting its syntax into configuration syntax, stored in <code>new_opt_line</code>. For each argument, the configuration is then scanned and filtered from <code>config</code> to <code>new_config</code>, using the same syntax which could be used to do the same job with temporary files. When the interesting variable is encountered in the config, using one of the applicable pattern matches, then the prepared configuration line is substituted for it. While this is going on, the encountered variable names (bindings for <code>var_other</code>) are also being collected into a list. This list is then later used to check via the directive <code>(@bind opt_there option)</code> to determine whether the option occurred in the configuration or not. The bind construct will not only check whether the left and right hand side are equal, but if nested lists are involved, it checks whether either side occurs in the other as a subtree. <code>option</code> binds with <code>opt_other</code> if it matches one of the option names in <code>opt_other</code>. Finally, the updated config is regurgitated.
 
<lang txr>@(next :args)
Line 681 ⟶ 683:
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000</pre>
 
Test run on empty input:
 
<pre>$ echo -n | txr configfile2.txr - NEEDSPEELING= SEEDSREMOVED NUMBEROFBANANAS=1024 NUMBEROFSTRAWBERRIES=62000
; NEEDSPEELING
SEEDSREMOVED
NUMBEROFBANANAS 1024
NUMBEROFSTRAWBERRIES 62000</pre>
 
Test run on empty input with no arguments
 
<pre>$ echo -n | ./txr configfile2.txr -
[ no output ]</pre>
 
 
 
 
Anonymous user