Argparse positional parameter sequest -


i'm trying use argparse keep having issue reason positional parameters required first on command line. want positional last since list of file names.

    self.parser = argumentparser(description=program_license,                      formatter_class=rawdescriptionhelpformatter,                      conflict_handler='resolve')     self.parser.add_argument('-v', '--version',                      action='version',                      version=program_version_message)     self.parser.add_argument('--logfile', action='store',                      dest='logfile', default='daddyvision.log')     self.parser.add_argument('prog', help=suppress, nargs=1)     self.parser.add_argument('library', metavar="library", nargs='*')      group_loglvl = self.parser.add_mutually_exclusive_group()     group_loglvl.add_argument("--verbose", dest="loglevel",                      action="store_const", const="verbose",                      default='info')     group_loglvl.add_argument("--debug", dest="loglevel",                      action="store_const", const="debug")     group_loglvl.add_argument("--trace", dest="loglevel",                      action="store_const", const="trace")     group_loglvl.add_argument("--quiet", dest="loglevel",                      action="store_const", const="warning")     group_loglvl.add_argument("--errors", dest="loglevel",                      action="store_const", const="error")      args = self.parser.parse_args(arg) 

if command line is: pgm --error filename "error: unrecognized arguments: filename"

if cmd line pgm filename --error works without error.

what doing wrong. i've read leads me believe positional can come first or last. python 2.7 environment.

if make positional requires (nargs='+') works parm optional.

it easier test code if clean up, removing things self. import argparse, , able paste rest, , running example.

how testing this?

parser.parse_args('pgm --error filename'.split()) 

or

parser.parse_args('--error filename'.split()) 

parser.parse_args() parses sys.argv[1:]. if ran script named pgm argparse, expect parse --error filename.

help, without suppress is:

usage: ipython [-h] [-v] [--logfile logfile]                [--verbose | --debug | --trace | --quiet | --errors]                prog [library [library ...]]  positional arguments:   prog   library  optional arguments:   -h, --help         show message , exit   ...   --errors 

regardless, key error prog [library [library ...]] part of usage.

what's happening pgm --error filename pgm string sets both prog='pgm' , library=[]. * (or ?), library satisfied no string @ all. having done that, has no place put filename string.

pgm filename --error gives library=['filename']. --error pgm filename should work.

the issue isn't postionals must first (or last) must (if 2nd '?' or '*').

this bug report post tries explain interaction of '*' positionals , optionals

http://bugs.python.org/issue14191#msg185517


if pgm program name sys.argv[0], don't need in argparse. remove prog argument, , problem goes away (then there 1 positional).


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -