Skip to content

More streamlined support for config objects #109

@DavidBuchanan314

Description

@DavidBuchanan314

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions