Reputation: 15
I have a class containing the main method and I've been wondering if I should parse input arguments and check if they're correct in main or in an object to which i pass those arguments. What makes classes more reusable?
Upvotes: 0
Views: 388
Reputation: 41281
Ideally, you will want to make clean, modular code. Imagine if one day you decide you need to take arguments from somewhere other than the command-line.
A good way to go about this is with an interface, ArgumentParser
, that the rest of your code can use (for example, by passing an instance implementing that interface to whatever parts of your code need to read arguments). Include methods like hasSwitch
for args like --foo
and getValue
for ones like --foo=bar
.
If you ever need to get arguments from a different location (e.g. interactive user prompts, config file, etc), it is as simple as changing a few lines of code to instantiate a different kind of argument parser.
Arguments should be checked at two points:
Upvotes: 1