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


Groups > comp.lang.python > #63504 > unrolled thread

argparse action on default values

Started byFlorian Lindner <mailinglists@xgm.de>
First post2014-01-08 19:20 +0100
Last post2014-01-08 19:20 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

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

#63504 — argparse action on default values

FromFlorian Lindner <mailinglists@xgm.de>
Date2014-01-08 19:20 +0100
Subjectargparse action on default values
Message-ID<mailman.5189.1389205227.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web