How do you delete an argument from a namespace
Asked Answered
S

2

9

Question: Given an argparse parser instance with some arguments added, is there a way to delete/remove an argument defined for it?

Why: Considering the exemple below:

>>>import argparse
>>>parser = argparse.ArgumentParser()
>>>parser.add_argument('--imagePath', action = "store", default = 'toto')
_StoreAction(option_strings=['--imagePath'], dest='imagePath', nargs=None, const=None, default='toto', type=None, choices=None, help=None, metavar=None)
>>>args = parser.parse_args()
>>>args
Namespace(imagePath='toto')
>>>parser.add_argument('--resultPath', action = "store", default = 'titi')
_StoreAction(option_strings=['--resultPath'], dest='resultPath', nargs=None, const=None, default='titi', type=None, choices=None, help=None, metavar=None)
>>>args = parser.parse_args()
>>>args
Namespace(imagePath='toto', resultPath='titi')

If we will, later in the script, change the value of the args.imagePath?

I does not found a method for change the value, but if I can delete / remove the argument imagePath it could be possible to define again imagePath with a new value !

Sequoia answered 9/11, 2015 at 17:9 Comment(3)
Why would you parse_args and then change the argument definitions?Kowtow
Why parse then ignore the input parameters? Do you actually want to remove the parameter, or just replace its value (and if the former, why not just ignore it)?Alps
In a big script, there is a lot of possible arguments with some that are connected. I just add a new argument, but depending of the value of it, i need to change dynamically, in flight, the value for others. hpaulj gave the correct way to do it properlySequoia
T
17

Just do:

args.imagePath = 'newvalue'

args is an argparse.Namespace object. This is a simple class, that just adds a display method - what you see with print(args). Otherwise, all the normal Python object methods work. So you can fetch, set, even add attributes as desired.

The parser tries to keep its assumptions about the namespace object to a minimum. It uses the most generic access methods - hasattr, getattr, setattr.

e.g.

dest = 'imagePath'
value = 'newvalue'
setattr(args, dest, value)

You can also delete, e.g.

del args.imagePath
delattr(args, 'imagePath')

I'm tempted to change your title to 'How do you delete an argument from a namespace'. I don't think you mean to delete the argument (Action created by add_argument) from the parser itself. Your focus is on the namespace created by parsing.

Teddytedeschi answered 9/11, 2015 at 19:7 Comment(1)
Great ! Thank you very much hpaulj, you have exactly understood what was my problem and what I needed to fix it ! Very clear answer ! Indeed the good title would be "How do you delete an argument from a namespace". Thanks again !Sequoia
S
1

To delete an argparse argument, one can use the oneliner

imagePath = args.__dict__.pop('imagePath')

Here the deleted value is even stored at the same time in a new variable.

If the deleted value is not needed at all, then just:

del args.imagePath
Sequestrate answered 4/2, 2022 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.