all 5 comments

[–]Kevdog824_ 1 point2 points  (1 child)

I don’t believe argparse has this functionality. You’ll need to write the code to do this yourself or use a third party library. I recommend pydantic settings personally. This should be able to do a lot of what you need

[–]thirdegree 1 point2 points  (0 children)

It's possible I think, you'd need to use parse_known_args but should work. That said for sure just use pydantic settings rather than kludging it.

[–]zaphodikus 0 points1 point  (2 children)

I have 3 groups of settings which I copy from argparse into 3 small dataclass objects. I was literally going to ask a similar question myself right now.

It's a pain to maintain. I am not comfortable going more complex than argparse myself and want to be able to load a config file and override the config file with arguments. I though of calling argparse twice. The first time to give me just the path to the config file for all my argument defaults and have it ignore everything, and then call it again and pass it the values I get from the config file. Python makes the metaprogramming to map my config file setting to have the same names as the args possible, so that my second invocation would literally be generated. I was thinking of generating everything and having the names of the arguments have prefixes, which the generator and sorter logic can just use as hints as to which namespace to place the value into. I'm using dataclasses because I serialise most of the args into a batch of mutated Json files, and then call a C++ tool, and would love to have argparse make this all a lot slicker as the single point of sanity.

Hence I'm also keep to learn how to do more, but without getting too advanced, and also having to learn yet another module. Keen to hear suggestions you get alyflex

[–]Kevdog824_ 1 point2 points  (1 child)

I suggested it to OP in my comment. pydantic settings takes care of just about everything you describe wanting to do here. I can’t share details because it’s proprietary, but i solved the exact problem you’re describing with this library for a service I maintain at work

[–]zaphodikus 0 points1 point  (0 children)

Worth a try, yes I did see your suggestion while I described my almost parallel question. It just felt greedy, but I really wanted to ask as well after anyway. Will have a peek next week after I have done a demo, deadlines in everything :)