270def parse(legacy_args=False):
271 """parses command line arguments and returns an ``Options`` instance"""
273 # Everything after a single "-" is treated as "user options". This is for
274 # backwards compatibility with AthArgumentParser used in analysis.
275 # FIXME: we should revisit this and find an alternative
277 dashpos = sys.argv.index("-")
278 except ValueError: # normal case, no isolated dash found
282 args = sys.argv[1:dashpos]
283 user_opts = sys.argv[dashpos+1:]
285 parser = getArgumentParser(legacy_args)
287 # perform a pre-parse without -h or --help
288 # if don't have a script, then will just re-parse with help active
289 # otherwise will effectively let script do the parsing
290 # i.e. this allows the following to work:
291 # athena MyScript.py --help
292 # to reveal the help messaging determined by MyScript.py
293 doHelp = any(a in ('-h', '--help') or a.startswith('--help=') for a in args)
295 # need to unrequire any required arguments in order to do a "pre-parse"
296 unrequiredActions = []
297 for a in parser._actions:
299 unrequiredActions.append(a)
302 # remove the help actions for the "pre-parse"
303 helpActions = {'-h' : parser._option_string_actions.pop('-h'),
304 '--help' : parser._option_string_actions.pop('--help')}
306 # parse with the modified parser
307 opts, leftover = parser.parse_known_args(args)
309 # restore original settings
310 parser._option_string_actions.update(helpActions)
311 for a in unrequiredActions:
314 # no script, just run argparsing as normal
316 opts, leftover = parser.parse_known_args(args)
318 opts, leftover = parser.parse_known_args(args)
320 opts.user_opts = user_opts
322 # If the argument parser has been extended, the script name(s) may end up
323 # in the leftovers. Try to find them there:
324 if not (opts.scripts or opts.fromdb) and leftover:
325 JobOptAction([], 'scripts')(parser, opts, leftover)
327 if not (opts.scripts or opts.fromdb) and not opts.interactive:
328 parser.error("the following arguments are required: scripts")
330 set_environment(opts)