I am relatively new to Python. I've so far mostly worked with PHP and C#, so most of it is relatively familiar.
I'm making a simple console application that will store a few API calls in a local database through a cronjob. It will have a timeout, so if the program is run too often it won't spam the API and use up all of my free access tokens.
Of course this begs for my favourite flag in any console application:
--force. If something doesn't work, try forcing it, right?
Now, my first attempt at adding this was using
sys.argv[n] like I would in PHP, though I read that's a rather insecure solution, which could also produce nasty bugs.
So, wanting to better my Python OOP knowledge as well as add a better implementation of CLI arguments, I've now written this:
import argparse class Arguments: def __init__(self): self.argument_parser = argparse.ArgumentParser() self.argument_parser.add_argument( '--force', default=False, help="Force the program to run, even if the timer returns False.", action="store_true" ) self.parsed = self.argument_parser.parse_args() @property def force_run(self): return self.parsed.force
Is this the right way to use classes in Python, and this is a "better" implementation of CLI arguments?
Sidenote: This was written with the assumption I'll be adding more flags in the future, all of which are accessed through the