Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More streamlined support for config objects #109

Open
DavidBuchanan314 opened this issue Dec 28, 2023 · 0 comments
Open

More streamlined support for config objects #109

DavidBuchanan314 opened this issue Dec 28, 2023 · 0 comments

Comments

@DavidBuchanan314
Copy link

DavidBuchanan314 commented Dec 28, 2023

In my program, I defined my configuration options as their own dataclass, so that the config could more easily be passed around between functions that need it. I was able to integrate clize with this approach remarkably cleanly:

from dataclasses import dataclass

@dataclass(kw_only=True)
class MyConfig:
	"""
	An example program.

	:param foo: foo docs
	:param bar: bar docs
	""" # this docstring is used by clize

	foo: int = 1
	bar: int = 2

def main(cfg: MyConfig):
	print("running main with cfg:", cfg)

if __name__ == "__main__":
	from clize import Clize
	import sys

	try:
		cli = clize.Clize.get_cli(MyConfig)
		cfg = cli(*sys.argv)
		if type(cfg) is not MyConfig:
			print(cfg)
		else:
			main(cfg)
	except clize.errors.ArgumentError as e:
		print(e)

However, it's a bit unergonomic. I have to import sys myself, and I lose out on the _fix_argv functionality that run() would normally do internally, and I have to implement help/error printing myself too.

I think it would be nice if I could just do this:

if __name__ == "__main__":
	from clize import run
      
	cfg = run(MyConfig, exit=False)
	main(cfg)

For this to happen, run() would have to return ret, in the case that exit is not called explicitly, as opposed to the current behaviour that just returns None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant