Mercurial > public > mercurial-scm > hg
comparison mercurial/dispatch.py @ 35032:cd235d6f851b stable
dispatch: add option to not strip command args parsed by _earlygetopt()
This allows us to parse the original args later by full-blown getopt() in
order to verify the result of the faulty early parsing. Still we need the
'strip=True' behavior for shell aliases.
Note that this series is RFC because it seems to change too much to be
included in stable release.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 11 Nov 2017 16:46:41 +0900 |
parents | e273b6671827 |
children | d3d35a55e03b |
comparison
equal
deleted
inserted
replaced
35031:e273b6671827 | 35032:cd235d6f851b |
---|---|
641 raise error.Abort(_('malformed --config option: %r ' | 641 raise error.Abort(_('malformed --config option: %r ' |
642 '(use --config section.name=value)') % cfg) | 642 '(use --config section.name=value)') % cfg) |
643 | 643 |
644 return configs | 644 return configs |
645 | 645 |
646 def _earlygetopt(aliases, args): | 646 def _earlygetopt(aliases, args, strip=True): |
647 """Return list of values for an option (or aliases). | 647 """Return list of values for an option (or aliases). |
648 | 648 |
649 The values are listed in the order they appear in args. | 649 The values are listed in the order they appear in args. |
650 The options and values are removed from args. | 650 The options and values are removed from args if strip=True. |
651 | 651 |
652 >>> args = [b'x', b'--cwd', b'foo', b'y'] | 652 >>> args = [b'x', b'--cwd', b'foo', b'y'] |
653 >>> _earlygetopt([b'--cwd'], args), args | 653 >>> _earlygetopt([b'--cwd'], args), args |
654 (['foo'], ['x', 'y']) | 654 (['foo'], ['x', 'y']) |
655 | 655 |
656 >>> args = [b'x', b'--cwd=bar', b'y'] | 656 >>> args = [b'x', b'--cwd=bar', b'y'] |
657 >>> _earlygetopt([b'--cwd'], args), args | 657 >>> _earlygetopt([b'--cwd'], args), args |
658 (['bar'], ['x', 'y']) | 658 (['bar'], ['x', 'y']) |
659 | 659 |
660 >>> args = [b'x', b'--cwd=bar', b'y'] | |
661 >>> _earlygetopt([b'--cwd'], args, strip=False), args | |
662 (['bar'], ['x', '--cwd=bar', 'y']) | |
663 | |
660 >>> args = [b'x', b'-R', b'foo', b'y'] | 664 >>> args = [b'x', b'-R', b'foo', b'y'] |
661 >>> _earlygetopt([b'-R'], args), args | 665 >>> _earlygetopt([b'-R'], args), args |
662 (['foo'], ['x', 'y']) | 666 (['foo'], ['x', 'y']) |
663 | 667 |
668 >>> args = [b'x', b'-R', b'foo', b'y'] | |
669 >>> _earlygetopt([b'-R'], args, strip=False), args | |
670 (['foo'], ['x', '-R', 'foo', 'y']) | |
671 | |
664 >>> args = [b'x', b'-Rbar', b'y'] | 672 >>> args = [b'x', b'-Rbar', b'y'] |
665 >>> _earlygetopt([b'-R'], args), args | 673 >>> _earlygetopt([b'-R'], args), args |
666 (['bar'], ['x', 'y']) | 674 (['bar'], ['x', 'y']) |
675 | |
676 >>> args = [b'x', b'-Rbar', b'y'] | |
677 >>> _earlygetopt([b'-R'], args, strip=False), args | |
678 (['bar'], ['x', '-Rbar', 'y']) | |
667 | 679 |
668 >>> args = [b'x', b'-R=bar', b'y'] | 680 >>> args = [b'x', b'-R=bar', b'y'] |
669 >>> _earlygetopt([b'-R'], args), args | 681 >>> _earlygetopt([b'-R'], args), args |
670 (['=bar'], ['x', 'y']) | 682 (['=bar'], ['x', 'y']) |
671 | 683 |
687 equals = arg.find('=') | 699 equals = arg.find('=') |
688 if equals > -1: | 700 if equals > -1: |
689 arg = arg[:equals] | 701 arg = arg[:equals] |
690 if arg in aliases: | 702 if arg in aliases: |
691 if equals > -1: | 703 if equals > -1: |
692 del args[pos] | |
693 values.append(fullarg[equals + 1:]) | 704 values.append(fullarg[equals + 1:]) |
694 argcount -= 1 | 705 if strip: |
706 del args[pos] | |
707 argcount -= 1 | |
708 else: | |
709 pos += 1 | |
695 else: | 710 else: |
696 if pos + 1 >= argcount: | 711 if pos + 1 >= argcount: |
697 # ignore and let getopt report an error if there is no value | 712 # ignore and let getopt report an error if there is no value |
698 break | 713 break |
699 del args[pos] | 714 values.append(args[pos + 1]) |
700 values.append(args.pop(pos)) | 715 if strip: |
701 argcount -= 2 | 716 del args[pos:pos + 2] |
717 argcount -= 2 | |
718 else: | |
719 pos += 2 | |
702 elif arg[:2] in shortopts: | 720 elif arg[:2] in shortopts: |
703 # short option can have no following space, e.g. hg log -Rfoo | 721 # short option can have no following space, e.g. hg log -Rfoo |
704 values.append(args.pop(pos)[2:]) | 722 values.append(args[pos][2:]) |
705 argcount -= 1 | 723 if strip: |
724 del args[pos] | |
725 argcount -= 1 | |
726 else: | |
727 pos += 1 | |
706 else: | 728 else: |
707 pos += 1 | 729 pos += 1 |
708 return values | 730 return values |
709 | 731 |
710 def _earlyreqoptbool(req, name, aliases): | 732 def _earlyreqoptbool(req, name, aliases): |