Skip to content

Instantly share code, notes, and snippets.

@mapix
Created May 4, 2015 10:49
Show Gist options
  • Select an option

  • Save mapix/764d8a10ccc699713582 to your computer and use it in GitHub Desktop.

Select an option

Save mapix/764d8a10ccc699713582 to your computer and use it in GitHub Desktop.
def checkRequiredArguments(opts, parser):
missing_options = []
for option in parser.option_list:
if re.match(r'^\[REQUIRED\]', option.help) and eval('opts.' + option.dest) == None:
missing_options.extend(option._long_opts)
if len(missing_options) > 0:
parser.error('Missing REQUIRED parameters: ' + str(missing_options))
parser = OptionParser()
parser.add_option("-s", "--start-date", help="[REQUIRED] Start date")
parser.add_option("-e", "--end-date", dest="endDate", help="[REQUIRED] End date")
(opts, args) = parser.parse_args(['-s', 'some-date'])
checkRequiredArguments(opts, parser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment