title: Setting options from environment variables when using argparse
body: <p>I have a script which has certain options that can either be passed on the command line, or from environment variables. The CLI should take precedence if both are present, and an error occur if neither are set.</p>
  
  <p>I could check that the option is assigned after parsing, but I prefer to let argparse to do the heavy lifting and be responsible for displaying the usage statement if parsing fails.</p>
  
  <p>I have come up with a couple of alternative approaches to this (which I will post below as answers so they can be discussed separately) but they feel pretty kludgey to me and I think that I am missing something.</p>
  
  <p>Is there an accepted "best" way of doing this?</p>
  
  <p>(Edit to make the desired behaviour clear when both the CLI option and environment variable are unset)</p>