0

I'd like to allow the user of my program to use the "~" shortcut in the arguments that are paths to some files, i.e., the "home" shortcut for linux systems. But to do so, as far as I understand, I need to all the os.path.expanduser function over each argument that is a path.

Is there a nicer way to do so, using the options provided in argparse?

rgalhama
  • 314
  • 1
  • 4
  • 14
  • I doubt, but I really wonder why you would consider a possible `argparse` solution to be nicer than a solution in the `os` module, which is indented for things related to the **o**perating **s**ystem? – DeepSpace Oct 17 '18 at 13:46
  • Oh, I don't mean to override the os function. What I am trying to ask is whether I can invoke it through some option in argparse (something similar to the "action" option). – rgalhama Oct 17 '18 at 14:16
  • 1
    Sounds like something you could package in a custom `type` function. `type` accepts any Python callable. In fact, other than the default `None` `type`, `argparse` doesn't define any special functions. `int`, `float` are standard Python functions. Well there is the `argparse.FileType` that illustrates how to write a custom type factory. – hpaulj Oct 17 '18 at 16:45
  • sounds like an idea! I'll report back when I try it. Thanks! – rgalhama Oct 18 '18 at 14:39

0 Answers0