@@ -664,6 +664,35 @@ Output:
664664 4^2 == 16
665665
666666
667+ .. _specifying-ambiguous-arguments :
668+
669+ Specifying ambiguous arguments
670+ ------------------------------
671+
672+ When there is ambiguity in deciding whether an argument is positional or for an
673+ argument, ``-- `` can be used to tell :meth: `~ArgumentParser.parse_args ` that
674+ everything after that is a positional argument::
675+
676+ >>> parser = argparse.ArgumentParser(prog='PROG')
677+ >>> parser.add_argument('-n', nargs='+')
678+ >>> parser.add_argument('args', nargs='*')
679+
680+ >>> # ambiguous, so parse_args assumes it's an option
681+ >>> parser.parse_args(['-f'])
682+ usage: PROG [-h] [-n N [N ...]] [args ...]
683+ PROG: error: unrecognized arguments: -f
684+
685+ >>> parser.parse_args(['--', '-f'])
686+ Namespace(args=['-f'], n=None)
687+
688+ >>> # ambiguous, so the -n option greedily accepts arguments
689+ >>> parser.parse_args(['-n', '1', '2', '3'])
690+ Namespace(args=[], n=['1', '2', '3'])
691+
692+ >>> parser.parse_args(['-n', '1', '--', '2', '3'])
693+ Namespace(args=['2', '3'], n=['1'])
694+
695+
667696Conflicting options
668697-------------------
669698
0 commit comments