Typically you want to separate option handling into two steps:
- Collect all options and do any preprocessing on them (such as checking their validity) and storing them as necessary in variables or structures.
- Actually taking action based on the full set of options encountered.
So basically you will probably want to set a global variable (such as opt_mode = ENCRYPT
or opt_mode = DECRYPT
or something similar), and store the key as necessary. Then after all option processing is done, actually do the encryption or decryption based on the opt_mode
variable.