Looks like that code part was copied (at the time of `msfvenom` creation) from `msfpayload`. I can't really follow the code in there either, but at least it defines a payload variable before using it, so perhaps it helps to look there to see how to fix `msfvenom`.