Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'args': 0.04; 'none)': 0.07; 'assumed': 0.09; 'python': 0.11; 'def': 0.14; 'missed': 0.15; 'thu,': 0.15; 'callables': 0.16; 'main():': 0.16; 'namespace,': 0.16; 'wrote:': 0.16; 'detect': 0.18; 'trying': 0.22; 'so.': 0.22; 'parser': 0.22; 'am,': 0.23; '2015': 0.23; 'wrote': 0.23; "i've": 0.24; 'seems': 0.24; 'header:In-Reply- To:1': 0.24; 'example': 0.25; 'specify': 0.27; 'message- id:@mail.gmail.com': 0.28; 'callable': 0.29; "i'd": 0.31; "can't": 0.32; 'skip:_ 10': 0.32; 'related': 0.32; 'url:python': 0.33; 'class': 0.33; 'subject:?': 0.34; 'this?': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'there': 0.36; 'actions': 0.36; 'url:library': 0.36; 'so,': 0.37; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'url:docs': 0.39; 'to:addr:python.org': 0.39; 'sure': 0.40; 'url:3': 0.60; 'hope': 0.61; 'hate': 0.66; 'url:4': 0.70; 'to:name:python': 0.84 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=CKy8+QtUTpMVNXRt4/ySawI8rfEbsM4ILh9Pz7eObpo=; b=Ghq9awqhLsrjKdF2HN6WZa7M39rDXfnNrMt8wgoHSHJfjeJMqKut3ik382p8CwEGzS 4V5aNSmQkYYnXXP668rhoBH1q+Ipb8jOSnTqv/luFdXm3Ene11wdKoCDm5ky1uJE6nHj 2LHR0We9eBewt5VXn1RPB27AztnXZLwt8CaT8CfHDfauzvzNEJrvfU3NFALWlzoTxvey CGgrLwtmhvBlGqYUnPJvyACtNMnEMiesQv065VFaF9tLSybDzIdO+VWhFCUL2iLZB8Xt 2t8MlzxpHmnNXh75W3405Sq3CI+MUHtFX7YNWiN/DD/5KvuOLdryqMg+i7LhWMyzm3jM G5Lw== X-Received: by 10.13.229.198 with SMTP id o189mr15178690ywe.108.1434653774971; Thu, 18 Jun 2015 11:56:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <7DDgx.1121732$LW6.214416@fx09.am4> References: <7DDgx.1121732$LW6.214416@fx09.am4> From: Ian Kelly Date: Thu, 18 Jun 2015 12:55:34 -0600 Subject: Re: CLI Arguments That Call Functions? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434654145 news.xs4all.nl 2896 [2001:888:2000:d::a6]:45916 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92844 On Thu, Jun 18, 2015 at 11:56 AM, Tony the Tiger wrote: > I would have assumed there would be something built in to the > ArgumentParser, but I can't detect anything that seems to do what I want, > so I wrote the following: [SNIP] > So, is there something already in the Python libs? Do I continue with > this? Or what? > > There ought to be something already built in to Python, but I think I've > missed it. Sure hope so. I'd hate to continue with this mess. You can specify custom actions for arguments. See https://docs.python.org/3.4/library/argparse.html#action-classes For example (untested): class AppendCallable(argparse.Action): def __call__(self, parser, namespace, values, option_string): callables = getattr(namespace, self.dest, None) or [] callables.append(functools.partial(self.const, *values)) setattr(namespace, self.dest, callables) def main(): parser = argparse.ArgumentParser() parser.add_argument( '-a', '--alfa', action=AppendCallable, const=option_alfa, dest='callables', nargs=1, type=int) parser.add_argument( '-b', '--bravo', action=AppendCallable, const=option_bravo, dest='callables', nargs=0) args = parser.parse_args() for callable in args.callables: callable() You might also be interested in reading https://docs.python.org/3.4/library/argparse.html#sub-commands in case that's related to what you're trying to accomplish.