1

I am thinking about using argparse as my source of truth for command line settings and reduce a lot of code this way. For example, at the moment I am using separate variables to store the argparse arguments and also do some basic operations on them.

# Store product sets for upload, if argument is "all", take all options from available choices
args = parser.parse_args()
product_sets = args.product_sets
if "all" in product_sets: 
    product_sets = PRODUCT_SET_CHOICES[:-1]

Is it possible to do such a logical operation also directly on argparse?

Is it frowned upon to use argparse across the script rather than saving everything in a separate settings dict or variables?

Nico Müller
  • 1,784
  • 1
  • 17
  • 37
  • 3
    `args` is a simple class object. `args.product_sets = new_value` is allowed. `print` `args` to see its 'content'. – hpaulj May 23 '20 at 14:58
  • 1
    Whether you pass the `args` namespace around, or individual attributes is entirely up to you. It's a good idea to put the `parse_args` in the `if __name__` block so it only runs when used as a script, and not when imported. That way you can use easily use the functions in other modules. So it's desirable to pass arguments in a way that's easy both ways. – hpaulj May 23 '20 at 17:16
  • Thank you for the tips, very good to know! – Nico Müller May 23 '20 at 17:18

0 Answers0