I'd like to fetch all parameters passed to sys.argv
that have the formatsomeprogram.py --someparameter 23 -p 42 -anotherparam somevalue
.
Result I'm looking for is a namespace containing all the variables, already parsed.
To my understanding, argparse is expecting the user to define what are the parameters he is expecting.
Any way to do that with argparse ? Thanks !
If you know that the parameters will always be given in the format --name value
or -name value
you can do it easily
class ArgHolder(object):
pass
name = None
for x in sys.argv[1:]:
if name:
setattr(ArgHolder, curname, x)
name = None
elif x.startswith('-'):
name = x.lstrip('-')
Now you will have collected all arguments in the class ArgHolder
which is a namespace. You may also collect the values in an instance of ArgHolder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With