275def parse(legacy_args=False):
276 """parses command line arguments and returns an ``Options`` instance"""
278 # Everything after a single "-" is treated as "user options". This is for
279 # backwards compatibility with AthArgumentParser used in analysis.
280 # FIXME: we should revisit this and find an alternative
282 dashpos = sys.argv.index("-")
283 except ValueError: # normal case, no isolated dash found
287 args = sys.argv[1:dashpos]
288 user_opts = sys.argv[dashpos+1:]
290 parser = getArgumentParser(legacy_args)
292 # perform a pre-parse without -h or --help
293 # if don't have a script, then will just re-parse with help active
294 # otherwise will effectively let script do the parsing
295 # i.e. this allows the following to work:
296 # athena MyScript.py --help
297 # to reveal the help messaging determined by MyScript.py
298 doHelp = any(a in ('-h', '--help') or a.startswith('--help=') for a in args)
300 # need to unrequire any required arguments in order to do a "pre-parse"
301 unrequiredActions = []
302 for a in parser._actions:
304 unrequiredActions.append(a)
307 # remove the help actions for the "pre-parse"
308 helpActions = {'-h' : parser._option_string_actions.pop('-h'),
309 '--help' : parser._option_string_actions.pop('--help')}
311 # parse with the modified parser
312 opts, leftover = parser.parse_known_args(args)
314 # restore original settings
315 parser._option_string_actions.update(helpActions)
316 for a in unrequiredActions:
319 # no script, just run argparsing as normal
321 opts, leftover = parser.parse_known_args(args)
323 opts, leftover = parser.parse_known_args(args)
325 opts.user_opts = user_opts
327 # If the argument parser has been extended, the script name(s) may end up
328 # in the leftovers. Try to find them there:
329 if not (opts.scripts or opts.fromdb) and leftover:
330 JobOptAction([], 'scripts')(parser, opts, leftover)
332 if not (opts.scripts or opts.fromdb) and not opts.interactive:
333 parser.error("the following arguments are required: scripts")
335 # Deprecation warning:
337 print(f"{os.path.basename(sys.argv[0])}: Warning: --CA is deprecated."
338 + f" Add a shebang (#!) to {opts.scripts[0]} to enable CA mode." if opts.scripts else "",
341 set_environment(opts)