def test_cli_run_with_subparsers() -> None: """Test with subparsers.""" bar = Genbu( name="bar", description="Bar.", callback=lambda: "bar", ) baz = Genbu( name="baz", description="Baz.", callback=lambda: "baz", ) foo = Genbu( name="foo", description="Foo.", callback=lambda: "foo", subparsers=[bar, baz], ) assert foo.run([]) == "foo" assert foo.run(["bar"]) == "bar" assert foo.run(["baz"]) == "baz" for source in ["ba", "oof", "-h"]: with pytest.raises(SystemExit): foo.run(source.split())
def test_genbu_run_with_defaults_in_function_args(default: str) -> None: """It should use defaults when no arg is specified.""" def echo(message: str = default) -> t.Any: """Return message.""" return message cli = Genbu(echo) assert cli.run([]) == default
import sys from genbu import Genbu, Param, combinators as comb, usage cli = Genbu( lambda: print("Hello, world!"), params=[ Param("_", ["-?", "-h", "--help"], parser=comb.Emit(True), aggregator=lambda _: sys.exit(usage(cli))), ], ) if __name__ == "__main__": cli.run()
from genbu import Genbu def echo(*args: str) -> str: """Echo strings.""" if not args: return "Usage: echo [--args <str>...]" return " ".join(args) cli = Genbu(echo) if __name__ == "__main__": print(cli.run())