Factors out the common code around dealing with option types into a
library function.