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


Groups > comp.lang.python > #63504

argparse action on default values

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mailinglists@xgm.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'parser': 0.07; 'received:localnet': 0.09; 'skip:o 50': 0.09; 'python': 0.11; 'def': 0.12; '"default': 0.16; "%r'": 0.16; 'absolute,': 0.16; 'arguments:': 0.16; 'assembled': 0.16; 'namespace,': 0.16; 'parts.': 0.16; 'subject:argparse': 0.16; 'subject:default': 0.16; 'subject:values': 0.16; 'supplied': 0.16; 'think?': 0.16; 'header :User-Agent:1': 0.23; 'config': 0.24; 'expanded': 0.24; 'fine': 0.24; 'values': 0.27; 'skip:p 30': 0.29; 'values.': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'joined': 0.36; 'url:org': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'actions': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'skip:n 30': 0.60; 'url:3': 0.61; 'skip:n 10': 0.64; 'default': 0.69; 'florian': 0.84; 'parser,': 0.84
From Florian Lindner <mailinglists@xgm.de>
To python-list@python.org
Subject argparse action on default values
Date Wed, 08 Jan 2014 19:20:24 +0100
User-Agent KMail/4.12 (Linux/3.12.6-1-ARCH; KDE/4.12.0; x86_64; ; )
MIME-Version 1.0
Content-Transfer-Encoding 7Bit
Content-Type text/plain; charset="utf-8"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5189.1389205227.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389205227 news.xs4all.nl 2893 [2001:888:2000:d::a6]:45563
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63504

Show key headers only | View raw


Hello,

I use argparse from Python 3.3.3 with a custom action that normalizes path arguments:

http://docs.python.org/3/library/argparse.html#action

def norm_path(*parts):
    """ Returns the normalized, absolute, expanded and joined path, assembled of all parts. """
    parts = [ str(p) for p in parts ] 
    return os.path.abspath(os.path.expanduser(os.path.join(*parts)))

# Taken from the docs
class NormPath(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        print('%r %r %r' % (namespace, values, option_string))
        setattr(namespace, self.dest, norm_path(values))


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", help="Path to config file.",
                        default = "~/.foobar/config", action=NormPath)

    return parser.parse_args()


This works fine when there is actually a --config=path supplied. But it's not being applied on default arguments. Of course, I could use "default = norm_path('~/.foobar/config')" but I expect that custom actions are applied to default values as well. The store action works alike for default and supplied values.

What do you think?

Florian

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


Thread

argparse action on default values Florian Lindner <mailinglists@xgm.de> - 2014-01-08 19:20 +0100

csiph-web