Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'arguments': 0.07; 'parser': 0.07; 'arg': 0.09; 'args.': 0.09; 'logic': 0.09; 'positional': 0.09; 'resulting': 0.13; "'foobar'": 0.16; '--config': 0.16; 'required):': 0.16; 'roy': 0.16; 'subject:argparse': 0.16; 'wrote:': 0.17; 'config': 0.17; 'specify': 0.17; '(in': 0.18; 'command': 0.24; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'separate': 0.27; '(as': 0.27; 'message- id:@mail.gmail.com': 0.27; 'interface': 0.27; 'represent': 0.28; 'optional': 0.29; 'wind': 0.29; 'case,': 0.29; 'fri,': 0.30; 'received:209.85.215.46': 0.30; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'subject: -- ': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'described': 0.35; 'nov': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'skip:g 30': 0.36; 'two': 0.37; 'quite': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:a 30': 0.60; "you'll": 0.62; 'more': 0.63; 'smith': 0.71; 'exclusive': 0.81; "'foo'": 0.84; 'to:name:python': 0.84; 'friendly,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Tb0Xx8JasVzu1eJ1Cs1wtRoSUWvcFxSftC0DhXOdB+s=; b=QBP7vCZG+BD+58u9KXXjhgN5gFGntsVY1w17keOpXBGFNu9SOmUo7qQjWtSbTYVkpR G7kPtbXMxU8ya2x2JnoLAIbruWd6lIwFGrAItBWMF2GVjetNMKwPr22J+Pefr/vv0MD+ kpC0+J16CaUiUcmKL9fUh9OHyH0XySuk6pRcT4Fv7BJ7WpZk36iWqMxCm/OORCvLuNcb 2NwmZKnnMTTloarvhUKyptD7Myo6ztSbhcwx/JRDXUuNBEHDpHsEi+MrEKfGiH1OvaFp JR9Hmup8R/ykK2TRW9goG85MSs6AKz6MAn6jHuXPOM5+TLO+ME4LPdmFMPprRgZdRmaL 4hoA== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 23 Nov 2012 18:07:46 -0700 Subject: Re: argparse -- mutually exclusive sets of arguments? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353719298 news.xs4all.nl 6928 [2001:888:2000:d::a6]:36075 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33871 On Fri, Nov 23, 2012 at 11:46 AM, Roy Smith wrote: > My command either takes two positional arguments (in which case, both > are required): > > $ command foo bar > > or the name of a config file (in which case, the positional arguments > are forbidden): > > $ command --config file > > How can I represent this with argparse; add_mutually_exclusive_group() > isn't quite the right thing. It could specify that foo and --config are > mutually exclusive, but not (as far as I can see) the more complicated > logic described above. I don't think you could even do the former. An argument must be optional in order to be mutually exclusive with anything. This works, however: parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--config', type=file) group.add_argument('--foobar', nargs=2, metavar=('FOO', 'BAR')) print parser.parse_args() Downsides are that the resulting interface is a little more formal and a little less friendly, and unless you customize the action you'll wind up with a 2-element 'foobar' arg instead of separate 'foo' and 'bar' args.