| | 319 | |
|---|
| | 320 | |
|---|
| | 321 | def get_inferior_argv( |
|---|
| | 322 | opts, default_opts, flag_opts, argv, ia, short_opts=None): |
|---|
| | 323 | if short_opts is None: |
|---|
| | 324 | short_opts = {} |
|---|
| | 325 | |
|---|
| | 326 | assert ia <= len(argv), ( |
|---|
| | 327 | 'ia=%s, argv="%s"' |
|---|
| | 328 | ) % (ia, str(argv) |
|---|
| | 329 | ) |
|---|
| | 330 | if ia == len(argv): |
|---|
| | 331 | return [] |
|---|
| | 332 | |
|---|
| | 333 | # Unconsumed arguments exist and start at index `ia` in `argv` As a |
|---|
| | 334 | # convenience we allow any *one* of the caller options to be used as to |
|---|
| | 335 | # delimit the end of its non option arguments. This means that we need to |
|---|
| | 336 | # examine the first remaining option and determine whether its intended for |
|---|
| | 337 | # the caller and if so whether it means we have more to do. But, and this |
|---|
| | 338 | # is *IMPORTANT*, if any option value was taken before calling this routine |
|---|
| | 339 | # it is assumed that the instance remaining in argv is for the inferior |
|---|
| | 340 | # target and and hence is left in place. For example ``pyrun -m bar ~/foo |
|---|
| | 341 | # -m 12`` means search under `~/foo` for package paths, then run module |
|---|
| | 342 | # `bar` with an argv of: ``['bar', '-m', '12']`` |
|---|
| | 343 | # |
|---|
| | 344 | |
|---|
| | 345 | if argv[ia] == '--': |
|---|
| | 346 | del argv[ia] |
|---|
| | 347 | else: |
|---|
| | 348 | |
|---|
| | 349 | # Short or long ? |
|---|
| | 350 | if argv[ia].startswith('--'): |
|---|
| | 351 | nextopt = argv[ia][2:] |
|---|
| | 352 | else: |
|---|
| | 353 | nextopt = argv[ia][1:] |
|---|
| | 354 | if nextopt not in default_opts: |
|---|
| | 355 | nextopt = short_opts[nextopt] |
|---|
| | 356 | |
|---|
| | 357 | # If its one of the callers options and the callers option values does |
|---|
| | 358 | # not have the corresponding value set then take nextopt; This alows |
|---|
| | 359 | # for a degree of overlap in options between the caller and the |
|---|
| | 360 | # inferior target - only the first instance of an option will be taken |
|---|
| | 361 | # by the caller. |
|---|
| | 362 | |
|---|
| | 363 | if nextopt in default_opts and ( |
|---|
| | 364 | getattr(opts, nextopt) == default_opts[nextopt]): |
|---|
| | 365 | |
|---|
| | 366 | # It's possibly a caller option. |
|---|
| | 367 | |
|---|
| | 368 | # Distinguish between those arguments that require values and those |
|---|
| | 369 | # that don't |
|---|
| | 370 | if nextopt in flag_opts: |
|---|
| | 371 | |
|---|
| | 372 | setattr(opts, nextopt, not default_opts[nextopt]) |
|---|
| | 373 | del argv[ia] |
|---|
| | 374 | |
|---|
| | 375 | else: |
|---|
| | 376 | # if its not a flag and it is a caller option then it takes a |
|---|
| | 377 | # single argument. |
|---|
| | 378 | if len(argv) >= ia+1: |
|---|
| | 379 | setattr(opts, nextopt, argv[ia+1]) |
|---|
| | 380 | del argv[ia:ia+2] |
|---|
| | 381 | else: |
|---|
| | 382 | log.warning( |
|---|
| | 383 | 'The `-%s` option requires an argument', nextopt) |
|---|
| | 384 | |
|---|
| | 385 | |
|---|
| | 386 | # else: its definitely *not* a caller option, leave it for the inferior |
|---|
| | 387 | return argv[ia:] |
|---|
| 684 | | |
|---|
| 685 | | assert ia <= len(argv), ( |
|---|
| 686 | | 'ia=%s, argv="%s"' |
|---|
| 687 | | ) % (ia, str(argv) |
|---|
| 688 | | ) |
|---|
| 689 | | if ia == len(argv): |
|---|
| 690 | | argv.append('--') |
|---|
| 691 | | |
|---|
| 692 | | # Unconsumed arguments exist and start at index `ia` in `argv` As a |
|---|
| 693 | | # convenience we allow any *one* of the pyrun options to be used as to |
|---|
| 694 | | # delimit the end of the discovery path. This means that we need to |
|---|
| 695 | | # examine the first remaining option and determine whether its intended |
|---|
| 696 | | # for pyrun and if so whether it means we have more to do. But, and |
|---|
| 697 | | # this is *IMPORTANT*, if any option was set before the discovery path |
|---|
| 698 | | # it is assumed to be for the target module and hence is ignored. For |
|---|
| 699 | | # example ``pyrun -m bar ~/foo -m 12`` means search under `~/foo` for |
|---|
| 700 | | # package paths, then run module `bar` with an argv of: |
|---|
| 701 | | # ``['bar', '-m', '12']`` |
|---|
| 702 | | # |
|---|
| 703 | | |
|---|
| 704 | | if argv[ia] == '--': |
|---|
| 705 | | del argv[ia] |
|---|
| 706 | | else: |
|---|
| 707 | | # All pyrun options are short options. |
|---|
| 708 | | nextopt = argv[ia][1:] |
|---|
| 709 | | if nextopt in default_opts and ( |
|---|
| 710 | | getattr(opts, nextopt) == default_opts[nextopt]): |
|---|
| 711 | | |
|---|
| 712 | | # It's possibly a pyrun option. |
|---|
| 713 | | |
|---|
| 714 | | # Distinguish between those arguments that require values and |
|---|
| 715 | | # those that don't |
|---|
| 716 | | if nextopt in flagOPTIONS_runex: |
|---|
| 717 | | |
|---|
| 718 | | setattr(opts, nextopt, not default_opts[nextopt]) |
|---|
| 719 | | del argv[ia] |
|---|
| 720 | | |
|---|
| 721 | | else: |
|---|
| 722 | | # if its not a flag and it is a pyrun option then it |
|---|
| 723 | | # takes a single argument. |
|---|
| 724 | | if len(argv) < ia+1: |
|---|
| 725 | | print ('The `-%s` pyrun option requires an argument' |
|---|
| 726 | | ) % nextopt |
|---|
| 727 | | return -1 |
|---|
| 728 | | |
|---|
| 729 | | setattr(opts, nextopt, argv[ia+1]) |
|---|
| 730 | | del argv[ia:ia+2] |
|---|
| 731 | | |
|---|
| 732 | | # else: its definitely *not* a pyrun option |
|---|
| | 755 | inferior_argv = get_inferior_argv( |
|---|
| | 756 | opts, default_opts, flagOPTIONS_runex, argv, ia) |
|---|