Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Joseph L. Casale" Newsgroups: comp.lang.python Subject: argparse and subparsers Date: Sun, 26 Jun 2016 18:51:14 +0000 Lines: 41 Message-ID: References: <4ab10ce55d564780b158c19817009b62@activenetwerx.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de GqNSfmGBcwLMxVAkpeVXtACRxZDAmtAerMXZwxTfVAWg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'root': 0.04; 'matches': 0.07; 'arg': 0.09; 'invocation': 0.09; 'mess': 0.09; 'optional': 0.09; 'parsers': 0.09; 'underlying': 0.09; '2.7': 0.13; 'argparse': 0.16; 'etc...': 0.16; 'failures,': 0.16; 'grouped': 0.16; 'invocations': 0.16; 'pinned': 0.16; 'received:172.18.0': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpler,': 0.16; 'sliced': 0.16; 'subject:argparse': 0.16; 'alternate': 0.18; 'example.': 0.18; 'exists': 0.18; 'stick': 0.18; 'input': 0.18; 'arguments': 0.22; 'parameter': 0.22; 'parser': 0.22; 'handling': 0.27; 'switch': 0.27; 'code': 0.30; "i'd": 0.31; 'changed': 0.33; 'instances': 0.33; 'could': 0.35; 'but': 0.36; 'created': 0.36; 'keyword': 0.36; 'limitation': 0.36; 'to:addr:python-list': 0.36; 'two': 0.37; 'thanks': 0.37; 'desired': 0.37; 'done.': 0.37; 'thought': 0.37; 'charset:us-ascii': 0.37; 'manual': 0.38; 'several': 0.38; 'goes': 0.39; 'easily': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'address': 0.61; 'skip:u 10': 0.61; 'here': 0.66; 'subject': 0.70; 'overcome': 0.72; 'compelling': 0.84; 'discrete': 0.84; 'pear': 0.84; 'thoughts,': 0.91 X-Authority-Analysis: v=2.2 cv=QZUkhYTv c=1 sm=1 tr=0 a=g3mLq75WYuDrh3Lt0JSDww==:117 a=g3mLq75WYuDrh3Lt0JSDww==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=pD_ry4oyNxEA:10 a=094zOx3Sjwdbqc3mld0A:9 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.activenetwerx.com X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.0 Thread-Topic: argparse and subparsers Thread-Index: AdHP2gSoxuCFxHcPRWiE5r3lwpEgMg== Accept-Language: en-CA, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.18.0.200] X-CMAE-Envelope: MS4wfMxAKzdKzdQkMUgVYK4XQg5z+LntLnzkgfOKbXkAZOU70PaJdh54bnE5HVOkXYk23KUKY5rUNfvRdi2uLzhAm/oCmSsGhXQQ+ZJhtMgSSKVo1olMXN1C 2eQFwM+EXIwT9GkHDEZQ9vFipt+U0GE9lePqAXetAE0HCs+YZT+1eObhB0YX5TXh6iSxR6V9TYq1/g== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <4ab10ce55d564780b158c19817009b62@activenetwerx.com> Xref: csiph.com comp.lang.python:110533 I have some code where sys.argv is sliced up and manually fed to discrete a= rgparse 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 t= he 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 exampl= e. Also, since argv is grouped by subparser specifiers, if a parameter has input that matc= hes a keyword it all goes pear shaped. The underlying root of this mess is a long unaddressed limitation in argpar= se 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 subPars= erB -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 th= ought 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 fail= ures, its desired to stick to base lib but that can easily be changed given a compell= ing reason if an alternate implementation exists that works well. Thanks for any thoughts, jlc