Should I pass the arguments as * args & ** kwargs?

I have a class that handles command line arguments in my program using the python module optparse

. It is also inherited by several classes for creating subsets of parameters. To encapsulate the parameter parsing mechanism, I only want to show a function add_option

for class inheritance. This function makes a call optparse.make_option


Is it good practice to just add_option

tell my method that it takes the same arguments as optparse.make_option

in the documentation and forwards the arguments like *args

and **kwargs


Should I do some parameter checking beforehand? In some way I want to avoid this in order to separate this piece of code from the specific version optparse



source to share

2 answers

It seems that you want your subclasses to have an understanding of the command line stuff, which is often not very good.

You want to encapsulate all of the login portion of your program's configuration so that you can manipulate it with the command line, a config file, another python program, whatever.

So, I would remove any add_option call from your subclasses.

If you want to know what your configuration requirements look like at runtime, I'll just add this information to my subclasses; let each one have a member or method that can be used to determine what input it needs.

You can then go through the input organizer class, output that data, and use it to manipulate the command line, config file, or whatever you have.

But to be honest, I've never had to do this while working. I usually pull all the contents of this configuration into my own thing that answers the question "What should the user be told about the tool?" And then the subclasses look in the configuration data structure for what they need.



Are you sure subclassing is what you want to do? Your superior behavior can simply be implemented in a function.



All Articles