298def parse(legacy_args=False):
299 """parses command line arguments and returns an ``Options`` instance"""
301 # Everything after a single "-" is treated as "user options". This is for
302 # backwards compatibility with AthArgumentParser used in analysis.
303 # FIXME: we should revisit this and find an alternative
305 dashpos = sys.argv.index("-")
306 except ValueError: # normal case, no isolated dash found
310 args = sys.argv[1:dashpos]
311 user_opts = sys.argv[dashpos+1:]
313 parser = getArgumentParser(legacy_args)
315 # perform a pre-parse without -h or --help
316 # if don't have a script, then will just re-parse with help active
317 # otherwise will effectively let script do the parsing
318 # i.e. this allows the following to work:
319 # athena MyScript.py --help
320 # to reveal the help messaging determined by MyScript.py
321 doHelp = any(a in ('-h', '--help') or a.startswith('--help=') for a in args)
323 # need to unrequire any required arguments in order to do a "pre-parse"
324 unrequiredActions = []
325 for a in parser._actions:
327 unrequiredActions.append(a)
330 # remove the help actions for the "pre-parse"
331 helpActions = {'-h' : parser._option_string_actions.pop('-h'),
332 '--help' : parser._option_string_actions.pop('--help')}
334 # parse with the modified parser
335 opts, leftover = parser.parse_known_args(args)
337 # restore original settings
338 parser._option_string_actions.update(helpActions)
339 for a in unrequiredActions:
342 # no script, just run argparsing as normal
344 opts, leftover = parser.parse_known_args(args)
346 opts, leftover = parser.parse_known_args(args)
348 opts.user_opts = user_opts
350 # If the argument parser has been extended, the script name(s) may end up
351 # in the leftovers. Try to find them there:
352 if not (opts.scripts or opts.fromdb) and leftover:
353 JobOptAction([], 'scripts')(parser, opts, leftover)
355 if not (opts.scripts or opts.fromdb) and not opts.interactive:
356 parser.error("the following arguments are required: scripts")
358 set_environment(opts)