Update a configuration file: Difference between revisions
Line 418: | Line 418: | ||
#---------------------------------------------------------------------------- |
#---------------------------------------------------------------------------- |
||
# GLOBAL |
# GLOBAL VARIABLES |
||
#---------------------------------------------------------------------------- |
#---------------------------------------------------------------------------- |
||
DISABLED_PREFIX = ';' |
DISABLED_PREFIX = ';' |
Revision as of 14:33, 15 November 2011
You are encouraged to solve this task according to the task description, using any language you may know.
We have a configuration file as follows:
# This is a configuration file in standard configuration file format # # Lines begininning with a hash or a semicolon are ignored by the application # program. Blank lines are also ignored by the application program. # The first word on each non comment line is the configuration option. # Remaining words or numbers on the line are configuration parameter # data fields. # Note that configuration option names are not case sensitive. However, # configuration parameter data is case sensitive and the lettercase must # be preserved. # This is a favourite fruit FAVOURITEFRUIT banana # This is a boolean that should be set NEEDSPEELING # This boolean is commented out ; SEEDSREMOVED # How many bananas we have NUMBEROFBANANAS 48
The task is to manipulate the configuration file as follows:
- Disable the needspeeling option (using a semicolon prefix)
- Enable the seedsremoved option by removing the semicolon and any leading whitespace
- Change the numberofbananas parameter to 1024
- Enable (or create if it does not exist in the file) a parameter for numberofstrawberries with a value of 62000
Note that configuration option names are not case sensitive. This means that changes should be effected, regardless of the case.
Options should always be disabled by prefixing them with a semicolon.
Lines beginning with hash symbols should not be manipulated and left unchanged in the revised file.
If a configuration option does not exist within the file (in either enabled or disabled form), it should be added during this update. Duplicate configuration option names in the file should be removed, leaving just the first entry.
For the purpose of this task, the revised file should contain appropriate entries, whether enabled or not for needspeeling,seedsremoved,numberofbananas and numberofstrawberries.)
The update should rewrite configuration option names in capital letters. However lines beginning with hashes and any parameter data must not be altered (eg the banana for favourite fruit must not become capitalized). The update process should also replace double semicolon prefixes with just a single semicolon (unless it is uncommenting the option, in which case it should remove all leading semicolons).
Any lines beginning with a semicolon or groups of semicolons, but no following option should be removed, as should any leading or trailing whitespace on the lines. Whitespace between the option and paramters should consist only of a single space, and any non ascii extended characters, tabs characters, or control codes (other than end of line markers), should also be removed.
See also:
AutoHotkey
<lang AutoHotkey>; Author: AlephX, Aug 17 2011 data = %A_scriptdir%\rosettaconfig.txt outdata = %A_scriptdir%\rosettaconfig.tmp FileDelete, %outdata%
NUMBEROFBANANAS := 1024 numberofstrawberries := 560 NEEDSPEELING = "0" FAVOURITEFRUIT := "bananas" SEEDSREMOVED = "1" BOOL0 = "0" BOOL1 = "1" NUMBER1 := 1 number0 := 0 STRINGA := "string here"
parameters = bool0|bool1|NUMBER1|number0|stringa|NEEDSPEELING|seedsremoved|numberofbananas|numberofstrawberries
Loop, Read, %data%, %outdata% { if (instr(A_LoopReadLine, "#") == 1 OR A_LoopReadLine == "") { Line := A_LoopReadLine } else { if instr(A_LoopReadLine, ";") == 1 { parameter := RegExReplace(Substr(A_LoopReadLine,2), "^[ \s]+|[ \s]+$", "")
parametervalue = %parameter% value := %parametervalue% if value == 0 Line := A_loopReadLine else Line := Parameter } else { parameter := RegExReplace(A_LoopReadLine, "^[ \s]+|[ \s]+$", "") if instr(parameter, A_Space) parameter := substr(parameter, 1, instr(parameter, A_Space)-1)
if instr(parameters, parameter) > 0 { parametervalue = %parameter% value := %parametervalue%
if (value = chr(34) . "0" . chr(34)) Line := "; " . Parameter else { if (value = chr(34) . "1" . chr(34)) Line := Parameter else Line = %parametervalue% %value% } } else Line := A_LoopReadLine }
} StringReplace, parameters, parameters, %parametervalue%,, StringReplace, parameters, parameters,||,|
FileAppend, %Line%`n }
Loop, parse, parameters,| { if (A_Loopfield <> "") { StringUpper, parameter, A_LoopField parametervalue = %parameter% value := %parametervalue%
if (value = chr(34) . "0" . chr(34)) Line := "; " . parameter else { if (value = chr(34) . "1" . chr(34)) Line := parameter else Line = %parametervalue% %value% }
FileAppend, %Line%`n, %outdata% } }
FileCopy, %A_scriptdir%\rosettaconfig.tmp, %A_scriptdir%\rosettaconfig.txt, 1</lang>
C
C with POSIX strcasecmp
function for case-insensitive comparing. Substitute your toolchain's version.
<lang C>#include <stdio.h>
- include <stdlib.h>
- include <string.h>
- define strcomp(X, Y) strcasecmp(X, Y)
struct option { char *name, *value;
int flag; };
/* TODO: dynamically obtain these */ struct option updlist[] = { { "NEEDSPEELING", NULL },
{ "SEEDSREMOVED", "" }, { "NUMBEROFBANANAS", "1024" }, { "NUMBEROFSTRAWBERRIES", "62000" }, { NULL, NULL } };
int output_opt(FILE *to, struct option *opt) { if (opt->value == NULL)
return fprintf(to, "; %s\n", opt->name); else if (opt->value[0] == 0) return fprintf(to, "%s\n", opt->name); else return fprintf(to, "%s %s\n", opt->name, opt->value); }
int update(FILE *from, FILE *to, struct option *updlist) { char line_buf[256], opt_name[128];
int i; for (;;) { size_t len, space_span, span_to_hash; if (fgets(line_buf, sizeof line_buf, from) == NULL) break; len = strlen(line_buf); space_span = strspn(line_buf, "\t "); span_to_hash = strcspn(line_buf, "#"); if (space_span == span_to_hash) goto line_out; if (space_span == len) goto line_out; if ((sscanf(line_buf, "; %127s", opt_name) == 1) || (sscanf(line_buf, "%127s", opt_name) == 1)) { int flag = 0; for (i = 0; updlist[i].name; i++) { if (strcomp(updlist[i].name, opt_name) == 0) { if (output_opt(to, &updlist[i]) < 0) return -1; updlist[i].flag = 1; flag = 1; } } if (flag == 0) goto line_out; } else line_out: if (fprintf(to, "%s", line_buf) < 0) return -1; continue; } { for (i = 0; updlist[i].name; i++) { if (!updlist[i].flag) if (output_opt(to, &updlist[i]) < 0) return -1; } } return feof(from) ? 0 : -1; }
int main(void) { if (update(stdin, stdout, updlist) < 0)
{ fprintf(stderr, "failed\n"); return (EXIT_FAILURE); } return 0; }</lang>
Run:
$ ./a.out < configfile # This is a configuration file in standard configuration file format # # Lines begininning with a hash or a semicolon are ignored by the application # program. Blank lines are also ignored by the application program. # The first word on each non comment line is the configuration option. # Remaining words or numbers on the line are configuration parameter # data fields. # Note that configuration option names are not case sensitive. However, # configuration parameter data is case sensitive and the lettercase must # be preserved. # This is a favourite fruit FAVOURITEFRUIT banana # This is a boolean that should be set ; NEEDSPEELING # This boolean is commented out SEEDSREMOVED # How many bananas we have NUMBEROFBANANAS 1024 NUMBEROFSTRAWBERRIES 62000
Perl
<lang Perl>use warnings; use strict;
my $needspeeling = 0; my $seedsremoved = 1; my $numberofstrawberries = 62000; my $numberofbananas = 1024; my $favouritefruit = 'bananas';
my @out;
sub config {
my (@config) = ; push @config, "NUMBEROFSTRAWBERRIES $numberofstrawberries\n" unless grep { /^;*[ \t]*NUMBEROFSTRAWBERRIES\b/; } @config;
foreach my $line (@config) {
if (substr($line, 0, 1) eq '#') { push @out, $line; next; }
next if $line =~ /^[;\t ]+$/;
my ($option, $option_name);
if ($line =~ /^([A-Z]+[0-9]*)/) { $option_name = lc $1; $option = eval "\\\$$option_name"; my $value = eval "\${$option_name}";
if ($value) { $$option = $value; } else { $line = '; ' . $line; $$option = undef; } } elsif ($line =~ /^;+\s*([A-Z]+[0-9]*)/) { $option_name = lc $1; $option = eval "\\\$$option_name"; my $value = eval "\${$option_name}";
if ($value) { $line =~ s/^;+\s*//; $$option = $value == 1 ? : $value; } else { $$option = undef; } }
if (defined $$option) { push @out, "\U$option_name\E $$option\n" unless grep { /^\U$option_name\E\b/; } @out; } else { $line =~ s/\s*[^[:ascii:]]+\s*$//; $line =~ s/[[:cntrl:]\s]+$//; push(@out, "$line\n"); } }
}
config();
my $file = join(, @out); $file =~ s/\n{3,}/\n\n/g;
print $file;
__DATA__
- This is a configuration file in standard configuration file format
- Lines begininning with a hash or a semicolon are ignored by the application
- program. Blank lines are also ignored by the application program.
- The first word on each non comment line is the configuration option.
- Remaining words or numbers on the line are configuration parameter
- data fields.
- Note that configuration option names are not case sensitive. However,
- configuration parameter data is case sensitive and the lettercase must
- be preserved.
- This is a favourite fruit
FAVOURITEFRUIT banana
- This is a boolean that should be set
NEEDSPEELING
- This boolean is commented out
- SEEDSREMOVED
- How many bananas we have
NUMBEROFBANANAS 48</lang>
PicoLisp
<lang PicoLisp>(let Data # Read all data
(in "config" (make (until (eof) (link (trim (split (line) " "))) ) ) ) (setq Data # Fix comments (mapcar '((L) (while (head '(";" ";") (car L)) (pop L) ) (if (= '(";") (car L)) L (cons NIL L) ) ) Data ) ) (let (Need NIL Seed NIL NBan NIL NStr NIL Favo NIL) (map '((L) (let D (mapcar uppc (cadar L)) (cond ((= '`(chop "NEEDSPEELING") D) (if Need (set L) (on Need) (unless (caar L) (set (car L) '(";")) ) ) ) ((= '`(chop "SEEDSREMOVED") D) (if Seed (set L) (on Seed) (when (caar L) (set (car L)) ) ) ) ((= '`(chop "NUMBEROFBANANAS") D) (if NBan (set L) (on NBan) (set (cddar L) 1024) ) ) ((= '`(chop "NUMBEROFSTRAWBERRIES") D) (if NStr (set L) (on NStr) ) ) ((= '`(chop "FAVOURITEFRUIT") D) (if Favo (set L) (on Favo) ) ) ) ) ) Data ) (unless Need (conc Data (cons (list NIL "NEEDSPEELING"))) ) (unless Seed (conc Data (cons (list NIL "SEEDSREMOVED"))) ) (unless NBan (conc Data (cons (list NIL "NUMBEROFBANANAS" 1024))) ) (unless NStr (conc Data (cons (list NIL "NUMBEROFSTRAWBERRIES" 62000))) ) ) (out "config" (for L Data (prinl (glue " " (if (car L) L (cdr L)))) ) ) )</lang>
Python
<lang Python>#!/usr/bin/env python
- ----------------------------------------------------------------------------
- STANDARD MODULES
- ----------------------------------------------------------------------------
import re import string
- ----------------------------------------------------------------------------
- GLOBAL VARIABLES
- ----------------------------------------------------------------------------
DISABLED_PREFIX = ';'
- ----------------------------------------------------------------------------
- CLASS Option
- ----------------------------------------------------------------------------
class Option(object):
"""An option, characterized by its name and its (optional) value. and by its status, which can be enabled or disabled. If its value is None, it is regarded to as a boolean option with a value of true. """
#------------------------------------------------------------------------ def __init__(self, name, value=None, disabled=False, disabled_prefix=DISABLED_PREFIX): """Create an Option instance, setting its name to 'name' (always converted to a string) and its value to 'value'. If 'disabled' is True, the option is considered disabled, otherwise enabled. The string 'disabled_prefix' is used as a prefix when generating the string representation of the option. """ self.name = str(name) self.value = value self.disabled = bool(disabled) self.disabled_prefix = disabled_prefix
#------------------------------------------------------------------------ def __str__(self): """Return a string representation of the Option instance. This always includes the option name, followed by a space and the option value (if it is not None). If the option is disabled, the whole string is preprendend by the string stored in the instance attribute 'disabled_prefix' and a space. """ disabled = (, '%s ' % self.disabled_prefix)[self.disabled] value = (' %s' % self.value, )[self.value is None] return .join((disabled, self.name, value))
- ----------------------------------------------------------------------------
- CLASS Config
- ----------------------------------------------------------------------------
class Config(object):
"""A set of configuration options and comment strings. """ # Regular expression matching a valid option line. reOPTION = r'^\s*(?P<disabled>%s*)\s*(?P<name>\w+)(?:\s+(?P<value>.+?))?\s*$'
#------------------------------------------------------------------------ def __init__(self, fname=None, disabled_prefix=DISABLED_PREFIX): """Initialize a Config instance, optionally reading the contents of the configuration file 'fname'. The string 'disabled_prefix' is used as a prefix when generating the string representation of the options. """ self.disabled_prefix = disabled_prefix self.contents = [] # Sequence of strings and Option instances. self.options = {} # Map an option name to an Option instance. self.creOPTION = re.compile(self.reOPTION % self.disabled_prefix) if fname: self.parse_file(fname)
#------------------------------------------------------------------------ def __str__(self): """Return a string representation of the Config instance. This is just the concatenation of all the items stored in the attribute 'contents'. """ return '\n'.join(map(str, self.contents))
#------------------------------------------------------------------------ def parse_file(self, fname): """Parse all the lines of file 'fname' by applying the method 'parser_lines' on the file contents. """ with open(fname) as f: self.parse_lines(f) return self
#------------------------------------------------------------------------ def parse_lines(self, lines): """Parse all the lines of iterable 'lines' by invoking the method 'parse_line' for each line in 'lines'. """ for line in lines: self.parse_line(line) return self
#------------------------------------------------------------------------ def parse_line(self, line): """Parse the line 'line', looking for options. If an option line is found, spaces are stripped from the start and the end of 'line' and any non-printable character is removed as well. Only the first occurrence of an option is processed, all the other occurrences are ignored. A valid option is added to the instance attribute 'contents' (in order to preserve its position among the other lines). It is also added to the mapping stored in the instance attribute 'options'. Any non-option string is added the the instance attribute 'contents', except those lines starting with the string stored into the instance attribute 'disabled_prefix' which are not followed by any option name. """ s = .join(c for c in line.strip() if c in string.printable) moOPTION = self.creOPTION.match(s) if moOPTION: name = moOPTION.group('name').upper() if not name in self.options: self.add_option(name, moOPTION.group('value'), moOPTION.group('disabled')) else: if not s.startswith(self.disabled_prefix): self.contents.append(line.rstrip()) return self
#------------------------------------------------------------------------ def add_option(self, name, value=None, disabled=False): """Create a new Option instance, named 'name' (always converted to uppercase) with value 'value' and set its disabled status to 'disabled'. The Option instance is added to the instance attribute 'contents'. It is also added to the mapping stored in the instance attribute 'options'. """ name = name.upper() opt = Option(name, value, disabled) self.options[name] = opt self.contents.append(opt) return opt
#------------------------------------------------------------------------ def set_option(self, name, value=None, disabled=False): """Look for an option named 'name' (always converted to uppercase) among the options stored in the instance attribute 'options'. If it is not found, a new Option instance is created. In any case its value is set to 'value' and its disabled status to 'disabled'. """ name = name.upper() opt = self.options.get(name) if opt: opt.value = value opt.disabled = disabled else: opt = self.add_option(name, value, disabled) return opt
#------------------------------------------------------------------------ def enable_option(self, name, value=None): """Enable the option named 'name' (always converted to uppercase) and set its value to 'value'. If the option is not found, it is created and added to the end of the instance attribute 'contents'. """ return self.set_option(name, value, False)
#------------------------------------------------------------------------ def disable_option(self, name, value=None): """Disable the option named 'name' (always converted to uppercase) and set its value to 'value'. If the option is not found, it is created and added to the end of the instance attribute 'contents'. """ return self.set_option(name, value, True)
#------------------------------------------------------------------------ def get_option(self, name): """Return the value of the option named 'name' (always converted to uppercase). If the option is not found in the instance attribute 'options', None is returned. If the stored value is None, it is regarded to as a boolean option and its enable status is returned. Othrwise its value is returned. """ opt = self.options.get(name.upper()) if opt: enabled = not bool(opt.disabled) if opt.value is None: value = enabled else: value = enabled and opt.value else: value = None return value
- ----------------------------------------------------------------------------
- MAIN
- ----------------------------------------------------------------------------
if __name__ == '__main__':
import sys cfg = Config(sys.argv[1] if len(sys.argv) > 1 else None) cfg.disable_option('needspeeling') cfg.enable_option('seedsremoved') cfg.enable_option('numberofbananas', 1024) cfg.enable_option('numberofstrawberries', 62000) print cfg
</lang>
Ruby
<lang ruby>require 'stringio'
class ConfigFile
# create a ConfigFile object from a file def self.file(filename) fh = File.open(filename) obj = self.new(fh) obj.filename = filename fh.close obj end
# create a ConfigFile object from a string def self.data(string) fh = StringIO.new(string) obj = self.new(fh) fh.close obj end
def initialize(filehandle) @lines = filehandle.readlines @filename = nil tidy_file end attr :filename
def save() if @filename File.open(@filename, "w") {|f| f.write(self)} end end
def tidy_file() @lines.map! do |line| # remove leading whitespace line.lstrip!
if line.match(/^#/) # Lines beginning with hash symbols should not be manipulated and left # unchanged in the revised file. line else # replace double semicolon prefixes with just a single semicolon line.sub!(/^;+\s+/, "; ") if line.match(/^; \s*$/) # Any lines beginning with a semicolon or groups of semicolons, but no # following option should be removed line = "" else # remove ... any trailing whitespace on the lines line = line.rstrip + "\n"
# Whitespace between the option and paramters should consist only of a # single space if m = line.match(/^(; )?(upper:+)\s+(.*)/) line = (m[1].nil? ? "" : m[1]) + format_line(m[2], m[3]) end end
line end end end
def format_line(option, value) "%s%s\n" % [option.upcase.strip, value.nil? ? "" : " " + value.to_s.strip] end
# returns the index of the option, or nil if not found def find_option(option) @lines.find_index {|line| line.match(/^#{option.upcase.strip}\b/)} end
# uncomments a disabled option def enable_option(option) if idx = find_option("; " + option) @lines[idx][/^; /] = "" end end
# comment a line with a semi-colon def disable_option(option) if idx = find_option(option) @lines[idx][/^/] = "; " end end
# add an option, or change the value of an existing option. # use nil for the value to set a boolean option def set_value(option, value) if idx = find_option(option) @lines[idx] = format_line(option, value) else @lines << format_line(option, value) end end
def to_s @lines.join() end
end
config = ConfigFile.data(DATA.read)
config.disable_option('needspeeling')
config.enable_option('seedsremoved')
config.set_value('numberofbananas', 1024)
config.set_value('numberofstrawberries', 62000)
puts config
__END__
- This is a configuration file in standard configuration file format
- Lines begininning with a hash or a semicolon are ignored by the application
- program. Blank lines are also ignored by the application program.
- The first word on each non comment line is the configuration option.
- Remaining words or numbers on the line are configuration parameter
- data fields.
- Note that configuration option names are not case sensitive. However,
- configuration parameter data is case sensitive and the lettercase must
- be preserved.
- This is a favourite fruit
FAVOURITEFRUIT banana
- This is a boolean that should be set
NEEDSPEELING
- This boolean is commented out
- SEEDSREMOVED
- How many bananas we have
NUMBEROFBANANAS 48</lang> outputs
# This is a configuration file in standard configuration file format # # Lines begininning with a hash or a semicolon are ignored by the application # program. Blank lines are also ignored by the application program. # The first word on each non comment line is the configuration option. # Remaining words or numbers on the line are configuration parameter # data fields. # Note that configuration option names are not case sensitive. However, # configuration parameter data is case sensitive and the lettercase must # be preserved. # This is a favourite fruit FAVOURITEFRUIT banana # This is a boolean that should be set ; NEEDSPEELING # This boolean is commented out SEEDSREMOVED # How many bananas we have NUMBEROFBANANAS 1024 NUMBEROFSTRAWBERRIES 62000
Tcl
Creating this to be a general solution: <lang tcl>package require Tcl 8.6 oo::class create Config {
variable filename contents constructor fileName {
set filename $fileName set contents {} try { set f [open $filename] ### Sanitize during input foreach line [split [read $f] \n] { if {[string match "#*" $line]} { lappend contents $line continue } if {[regexp {^;\W*$} $line]} continue set line [string trim [regsub -all {[^\u0020-\u007e]} $line {}]] if {[regexp {^(\W*)(\w+)(.*)$} $line -> a b c]} { set line "[regsub -all {^;+} $a {;}][string toupper $b]$c" } lappend contents $line } } finally { if {[info exists f]} { close $f } }
} method save {} {
set f [open $filename w] puts $f [join $contents \n] close $f
}
# Utility methods (not exposed API) method Transform {pattern vars replacement} {
set matched 0 set line -1 set RE "(?i)^$pattern$" foreach l $contents { incr line if {[uplevel 1 [list regexp $RE $l -> {*}$vars]]} { if {$matched} { set contents [lreplace $contents $line $line] incr line -1 } else { lset contents $line [uplevel 1 [list subst $replacement]] } set matched 1 } } return $matched
} method Format {k v} {
set v " [string trimleft $v]" return "[string toupper $k][string trimright $v]"
}
# Public API for modifying options method enable {option} {
if {![my Transform ";?\\s*($option)\\M\s*(.*)" {k v} \ {[my Format $k $v]}]} { lappend contents [my Format $option ""] }
} method disable {option} {
if {![my Transform ";?\\s*($option)\\M\s*(.*)" {k v} \ {; [my Format $k $v]}]} { lappend contents "; [my Format $option ""]" }
} method set {option {value ""}} {
if {![my Transform ";?\\s*($option)\\M.*" k {[my Format $k $value]}]} { lappend contents [my Format $option $value] }
}
}</lang> Applying to the task at hand (assuming a file in the current directory called sample.cfg): <lang tcl>set cfg [Config new "sample.cfg"] $cfg disable needspeeling $cfg enable seedsremoved $cfg set numberofbananas 1024 $cfg set numberofstrawberries 62000 $cfg save</lang>
TXR
This is a general solution which implements a command-line tool for updating the config file. Omitted are the trivial steps for writing the configuration back into the same file; the final result is output on standard output.
The first argument is the name of the config file. The remaining arguments are of this form:
VAR # define or update VAR as a true-valued boolean VAR= # ensure "; VAR" in the config file. VAR=VAL # ensure "VAR VAL" in the config file
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 new_opt_line
. For each argument, the configuration is then scanned and filtered from config
to new_config
, 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 var_other
) are also being collected into a list. This list is then later used to check via the directive @(bind opt_there option)
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. option
binds with opt_other
if it matches one of the option names in opt_other
. Finally, the updated config is regurgitated.
<lang txr>@(next :args) @configfile @(maybe) @ (next configfile) @ (collect :mintimes 1) @config @ (end) @(or) @ (bind config nil) @(end) @(collect) @ (cases) @option= @ (output :into new_opt_line :filter :upcase)
- @option
@ (end) @ (or) @option=@val @ (output :into new_opt_line :filter :upcase) @option @val @ (end) @ (or) @option @ (output :into new_opt_line :filter :upcase) @option @ (end) @ (end) @ (next :var config) @ (local new_config) @ (bind new_config ()) @ (collect :vars ((opt_there ""))) @ (block) @ (cases) @ (cases) @{line /[ \t]*/} @ (or) @{line /#.*/} @ (end) @ (output :append :into new_config) @line @ (end) @ (accept) @ (or) @ (maybe)
- @opt_there
@ (or) @opt_there @(skip) @ (or) @opt_there @ (or) @original_line @ (end) @ (end) @ (cases) @ (bind opt_there option :filter :upcase) @ (output :append :into new_config) @new_opt_line @ (end) @ (or) @ (output :append :into new_config) @original_line @ (end) @ (end) @ (end) @ (cases) @ (bind opt_there option :filter :upcase) @ (or) @ (output :append :into new_config) @new_opt_line @ (end) @ (end) @ (set config new_config) @(end) @(output) @ (repeat) @config @ (end) @(end)</lang>
Sample invocation:
$ txr configfile2.txr configfile NEEDSPEELING= seedsREMOVED NUMBEROFBANANAS=1024 NUMBEROFSTRAWBERRIES=62000 # This is a configuration file in standard configuration file format # # Lines begininning with a hash or a semicolon are ignored by the application # program. Blank lines are also ignored by the application program. # The first word on each non comment line is the configuration option. # Remaining words or numbers on the line are configuration parameter # data fields. # Note that configuration option names are not case sensitive. However, # configuration parameter data is case sensitive and the lettercase must # be preserved. # This is a favourite fruit FAVOURITEFRUIT banana # This is a boolean that should be set ; NEEDSPEELING # This boolean is commented out SEEDSREMOVED # How many bananas we have NUMBEROFBANANAS 1024 NUMBEROFSTRAWBERRIES 62000
Test run on empty input:
$ echo -n | txr configfile2.txr - NEEDSPEELING= SEEDSREMOVED NUMBEROFBANANAS=1024 NUMBEROFSTRAWBERRIES=62000 ; NEEDSPEELING SEEDSREMOVED NUMBEROFBANANAS 1024 NUMBEROFSTRAWBERRIES 62000
Test run on empty input with no arguments
$ echo -n | txr configfile2.txr - [ no output ]