Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110533

argparse and subparsers

From "Joseph L. Casale" <jcasale@activenetwerx.com>
Newsgroups comp.lang.python
Subject argparse and subparsers
Date 2016-06-26 18:51 +0000
Message-ID <mailman.11.1466967381.2358.python-list@python.org> (permalink)
References <4ab10ce55d564780b158c19817009b62@activenetwerx.com>

Show all headers | View raw


I have some code where sys.argv is sliced up and manually fed to discrete argparse
instances each with a single subparser. The reason the discrete parsers all having a
single subparser was to make handling the input simpler, the first arg in the slice
could be left in.

This has become unmaintainable as the manual slicing is always subject to a new case
by where a parser has a positional, switch or optional parameter for example. Also, since
argv is grouped by subparser specifiers, if a parameter has input that matches a keyword
it all goes pear shaped.

The underlying root of this mess is a long unaddressed limitation in argparse to support
multiple subparser specifications on the same invocation:

prog.py -x -y 42 -foo bar subParserA -a 1 -b 2 subParserB -a 1 -b 2 subParserB -a 1 -b 2

The base arguments (-x -y 42 -foo bar).
An invocation of "subParserA" and its arguments (-a 1 -b 2).
Two invocations of "subParserB" and their arguments.

etc...

I have seen some creative ways to overcome this on stacktrace, however I thought I'd
see what people here have done. The code is pinned at 2.7 and while several people
have created alternate implementations which address many of argparses failures, its
desired to stick to base lib but that can easily be changed given a compelling reason
if an alternate implementation exists that works well.

Thanks for any thoughts,
jlc

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

argparse and subparsers "Joseph L. Casale" <jcasale@activenetwerx.com> - 2016-06-26 18:51 +0000
  Re: argparse and subparsers Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-26 20:15 -0700
  Re: argparse and subparsers Sachin Garg <s.garg.computer@gmail.com> - 2016-06-27 00:55 -0400
    Re: argparse and subparsers Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-26 22:34 -0700
      Re: argparse and subparsers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-27 20:28 +1000
        Re: argparse and subparsers Sachin Garg <s.garg.computer@gmail.com> - 2016-06-27 13:22 -0400
          Re: argparse and subparsers Michele Simionato <michele.simionato@gmail.com> - 2016-06-28 03:02 -0700
    RE: argparse and subparsers "Joseph L. Casale" <jcasale@activenetwerx.com> - 2016-06-28 02:22 +0000

csiph-web