Python Argparse As A Function
Is there anything inherently wrong with getting command-line arguments in this way? I mean by putting the argument parsing into its own function. Would it be considered non-Pytho
Solution 1:
It looks good, feels good, conforms to Python Zen - so, what's the problem if you didn't see this particular code like that?
Moving a somewhat independent piece of functionality into a subroutine is essential good practice - a manifestation of separation of concerns, to be precise. It's not even about Python.
Solution 2:
This is fine. The only suggestion I would make is to allow get_args
to take as a parameter (None
by default) a list of arguments to pass to parse_args()
. This makes testing easier. Also, the import
statement should still go at the top of the script, not inside get_args
itself.
import argparse
# ...defget_args(argv=None):
parser = argparse.ArgumentParser(description="calculate X to the power of Y")
# ...return parser.parse_args(argv)
Post a Comment for "Python Argparse As A Function"