From 7d4691e9d85acb3d51444a401a6e49b833042e32 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Tue, 23 Nov 2004 18:03:51 +0000 Subject: [PATCH] Add the ability to map keywords to ListOption (like we already do with EnumOption). git-svn-id: http://scons.tigris.org/svn/scons/trunk@1175 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- doc/man/scons.1 | 10 +++++++++- src/CHANGES.txt | 3 +++ src/engine/SCons/Options/ListOption.py | 12 +++++------- src/engine/SCons/Options/ListOptionTests.py | 10 +++++++++- test/Options/ListOption.py | 17 +++++++++++------ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/doc/man/scons.1 b/doc/man/scons.1 index 2f504a69..870377e2 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -7536,7 +7536,7 @@ and all input values will be converted to lower case. .TP -.RI ListOption( key ", " help ", " default ", " names ) +.RI ListOption( key ", " help ", " default ", " names ", [", map ]) Return a tuple of arguments to set up an option whose value may be one or more @@ -7560,6 +7560,14 @@ with all values separated by commas. The default may be a string of comma-separated default values, or a list of the default values. +The optional +.I map +argument is a dictionary +that can be used to convert +input values into specific legal values +in the +.I names +list. .TP .RI PackageOption( key ", " help ", " default ) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 62d691a0..48483d18 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -139,6 +139,9 @@ RELEASE 0.97 - XXX $SHF90COMSTR, $SHF95COMSTR, $SHFORTRANCOMSTR, $SHLINKCOMSTR and $YACCCOMSTR. + - Add an optional "map" keyword argument to ListOption() that takes a + dictionary to map user-specified values to legal values from the list + (like EnumOption() already doee). From Wayne Lee: diff --git a/src/engine/SCons/Options/ListOption.py b/src/engine/SCons/Options/ListOption.py index 903d52b1..d14b22ac 100644 --- a/src/engine/SCons/Options/ListOption.py +++ b/src/engine/SCons/Options/ListOption.py @@ -89,7 +89,7 @@ class _ListOption(UserList.UserList): def __repr__(self): return self.__str__() -def _converter(val, allowedElems): +def _converter(val, allowedElems, mapdict): """ """ if val == 'none': @@ -98,10 +98,8 @@ def _converter(val, allowedElems): val = allowedElems else: val = filter(None, string.split(val, ',')) - notAllowed = [] - for v in val: - if not v in allowedElems: - notAllowed.append(v) + val = map(lambda v, m=mapdict: m.get(v, v), val) + notAllowed = filter(lambda v, aE=allowedElems: not v in aE, val) if notAllowed: raise ValueError("Invalid value(s) for option: %s" % string.join(notAllowed, ',')) @@ -115,7 +113,7 @@ def _converter(val, allowedElems): ## return 1 -def ListOption(key, help, default, names): +def ListOption(key, help, default, names, map={}): """ The input parameters describe a 'package list' option, thus they are returned with the correct converter and validater appended. The @@ -132,4 +130,4 @@ def ListOption(key, help, default, names): '\n ') return (key, help, default, None, #_validator, - lambda val, elems=names: _converter(val, elems)) + lambda val, elems=names, m=map: _converter(val, elems, m)) diff --git a/src/engine/SCons/Options/ListOptionTests.py b/src/engine/SCons/Options/ListOptionTests.py index ec604f81..a892e18b 100644 --- a/src/engine/SCons/Options/ListOptionTests.py +++ b/src/engine/SCons/Options/ListOptionTests.py @@ -56,7 +56,8 @@ class ListOptionTestCase(unittest.TestCase): """Test the ListOption converter""" opts = SCons.Options.Options() opts.Add(SCons.Options.ListOption('test', 'test option help', 'all', - ['one', 'two', 'three'])) + ['one', 'two', 'three'], + {'ONE':'one', 'TWO':'two'})) o = opts.options[0] @@ -68,9 +69,13 @@ class ListOptionTestCase(unittest.TestCase): x = o.converter('one') assert str(x) == 'one', x + x = o.converter('ONE') + assert str(x) == 'one', x x = o.converter('two') assert str(x) == 'two', x + x = o.converter('TWO') + assert str(x) == 'two', x x = o.converter('three') assert str(x) == 'three', x @@ -96,6 +101,9 @@ class ListOptionTestCase(unittest.TestCase): x = o.converter('three,two,one') assert str(x) == 'all', x + x = o.converter('three,ONE,TWO') + assert str(x) == 'all', x + caught = None try: x = o.converter('no_match') diff --git a/test/Options/ListOption.py b/test/Options/ListOption.py index 3f0c4263..5dbe0c37 100644 --- a/test/Options/ListOption.py +++ b/test/Options/ListOption.py @@ -51,7 +51,8 @@ opts.AddOptions( ListOption('shared', 'libraries to build as shared libraries', 'all', - names = list_of_libs), + names = list_of_libs, + map = {'GL':'gl', 'QT':'qt'}), ) env = Environment(options=opts) @@ -77,13 +78,17 @@ test.run(arguments='shared=x11,ical') check(['ical,x11', '1', 'ical x11', 'ical x11']) test.run(arguments='shared=x11,,ical,,') check(['ical,x11', '1', 'ical x11', 'ical x11']) +test.run(arguments='shared=GL') +check(['gl', '0', 'gl', 'gl']) +test.run(arguments='shared=QT,GL') +check(['gl,qt', '0', 'gl qt', 'gl qt']) test.run(arguments='shared=foo', stderr = """ scons: *** Error converting option: shared Invalid value(s) for option: foo -File "SConstruct", line 14, in ? +File "SConstruct", line 15, in ? """, status=2) # be paranoid in testing some more combinations @@ -92,28 +97,28 @@ test.run(arguments='shared=foo,ical', stderr = """ scons: *** Error converting option: shared Invalid value(s) for option: foo -File "SConstruct", line 14, in ? +File "SConstruct", line 15, in ? """, status=2) test.run(arguments='shared=ical,foo', stderr = """ scons: *** Error converting option: shared Invalid value(s) for option: foo -File "SConstruct", line 14, in ? +File "SConstruct", line 15, in ? """, status=2) test.run(arguments='shared=ical,foo,x11', stderr = """ scons: *** Error converting option: shared Invalid value(s) for option: foo -File "SConstruct", line 14, in ? +File "SConstruct", line 15, in ? """, status=2) test.run(arguments='shared=foo,x11,,,bar', stderr = """ scons: *** Error converting option: shared Invalid value(s) for option: foo,bar -File "SConstruct", line 14, in ? +File "SConstruct", line 15, in ? """, status=2) test.write('SConstruct', """ -- 2.26.2