Yes, it could be done that way.  But we would need the map from our accepted output_format strings to the names of the PETSc viewer create functions, which is not completely trivial (due to capitalization).  Thus one would still need to write some code for each format.  I don't think it would be any more compact than what's here, and it would be somewhat harder for humans to read the code.