@@ -562,8 +562,12 @@ class SelectFilesInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
562
562
desc = "When matching mutliple files, return them in sorted order." )
563
563
raise_on_empty = traits .Bool (True , usedefault = True ,
564
564
desc = "Raise an exception if a template pattern matches no files." )
565
- force_lists = traits .Bool (False , usedefault = True ,
566
- desc = "Return all values as lists even when matching a single file." )
565
+ force_lists = traits .Either (traits .Bool (), traits .List (traits .Str ()),
566
+ default = False , usedefault = True ,
567
+ desc = ("Whether to return outputs as a list even when only one file "
568
+ "matches the template. Either a boolean that applies to all "
569
+ "output fields or a list of output field names to coerce to "
570
+ " a list" ))
567
571
568
572
569
573
class SelectFiles (IOBase ):
@@ -646,6 +650,18 @@ def _list_outputs(self):
646
650
info = dict ([(k , v ) for k , v in self .inputs .__dict__ .items ()
647
651
if k in self ._infields ])
648
652
653
+ force_lists = self .inputs .force_lists
654
+ if isinstance (force_lists , bool ):
655
+ force_lists = self ._outfields if force_lists else []
656
+ bad_fields = set (force_lists ) - set (self ._outfields )
657
+ if bad_fields :
658
+ bad_fields = ", " .join (list (bad_fields ))
659
+ plural = "s" if len (bad_fields ) > 1 else ""
660
+ verb = "were" if len (bad_fields ) > 1 else "was"
661
+ msg = ("The field%s '%s' %s set in 'force_lists' and not in "
662
+ "'templates'." ) % (plural , bad_fields , verb )
663
+ raise ValueError (msg )
664
+
649
665
for field , template in self ._templates .iteritems ():
650
666
651
667
# Build the full template path
@@ -673,7 +689,7 @@ def _list_outputs(self):
673
689
filelist .sort ()
674
690
675
691
# Handle whether this must be a list or not
676
- if not self . inputs . force_lists :
692
+ if field not in force_lists :
677
693
filelist = list_to_filename (filelist )
678
694
679
695
outputs [field ] = filelist
0 commit comments