diff --git a/tackle/utils/help.py b/tackle/utils/help.py new file mode 100644 index 000000000..0f560f7fc --- /dev/null +++ b/tackle/utils/help.py @@ -0,0 +1,92 @@ +import sys + +from tackle.models import Context + + +HELP_TEMPLATE = """ +usage: tackle {{ input_string }} + +{% if positional_arguments is not None %}positional arguments: + {% for i in positional_arguments %} + +{% endfor %}{% endif %} + +{% if positional_arguments is not None %}optional arguments: + {% for i in optional_arguments %} + +{% endfor %}{% endif %} +""" + + +def find_help_functions(context: 'Context', args: list): + + # if len(args) == 0: + # context.provider_hooks = + + for arg in args: + if arg in context.context_functions: + + h = context.provider_hooks[arg] + context.context_functions = "" + print() + + +def run_help(context: 'Context', args: list): + """ + Print the help screen then exit. Help can be displayed in three different scenarios. + 1. For the base (no args) -> this shows all the different function's help + 2. For a function (len(args) = 1) -> this shows function's help along with its + associated methods. This is recursive so depends on depth of methods. + 3. For a function's methods (len(args) > 1) -> this drills into methods + """ + find_help_functions(context, args) + help = [context.provider_hooks[i] for i in context.context_functions] + for arg in args: + if arg in context.context_functions: + context.context_functions + + if len(args) >= 1: + context.context_functions = [args.pop(0)] + + if len(args) != 0: + # We need to drill down into methods + raise NotImplementedError + + help_msg = [] + for i in context.context_functions: + hook_fields = context.provider_hooks[i].__fields__ + + if hook_fields['help'].default is None: + continue + + help_msg.append(context.provider_hooks[i]) + x = context.provider_hooks[i] + z = context.provider_hooks[i].__fields__ + print() + + print() + sys.exit(0) + + # if len(args) == 0: + # help_functions = context.context_functions + # else: + # for i, v in enumerate(args): + # if i == 0: + # if v not in context.context_functions: + # print( + # f"Help for {v} not available, declarative hook not in context.") + # sys.exit(0) + # help_functions = [args.pop(0)] + # + # # if len(args) >= 0: + # for i, v in enumerate(args): + # # Drill into functions + + # if len(args) >= 1: + # # We have been asked for help on a function in the tackle file. Check if it + # # exists and then drill into that func and then it's methods if len(args) > 1 + # # if + # + # # for i in args: + # # if i in + # pass diff --git a/tests/cli/fixtures/help-mixed.yaml b/tests/cli/fixtures/help-mixed.yaml new file mode 100644 index 000000000..630186cde --- /dev/null +++ b/tests/cli/fixtures/help-mixed.yaml @@ -0,0 +1,46 @@ + +help: + + +str: foo +list: + - stuff + - things + +map: + bar: baz + ding: dong + +str_hook->: input +list_hook->: + - stuff + - things + +map_hook->: + bar: baz + ding: dong + + +with_help<-: + help: Do stuff to things + things: + type: str + description: The things + render_by_default: true + exec<-: + p->: print {{things}} + + +no_help<-: + + no_show_str: foo + no_show_list: + - foo + - bar + + things: + type: str + description: The things + render_by_default: true + exec<-: + p->: print {{things}} diff --git a/tests/cli/fixtures/help.yaml b/tests/cli/fixtures/help.yaml new file mode 100644 index 000000000..5f155c606 --- /dev/null +++ b/tests/cli/fixtures/help.yaml @@ -0,0 +1,38 @@ +with_help<-: + help: Do stuff to things + things: + type: str + description: The things + render_by_default: true + exec<-: + p->: print {{things}} + + +with_help_methods<-: + help: Do stuff to things + things: + type: str + description: The things + render_by_default: true + exec<-: + p->: print {{things}} + + a_method_without_help<-: + foo->: print bar + + a_method_with_help<-: + help: Stuff n tings + foo->: print bar + +no_help<-: + no_show_str: foo + no_show_list: + - foo + - bar + + things: + type: str + description: The things + render_by_default: true + exec<-: + p->: print {{things}} diff --git a/tests/cli/help.yaml b/tests/cli/help.yaml deleted file mode 100644 index d0ccd3fb0..000000000 --- a/tests/cli/help.yaml +++ /dev/null @@ -1,18 +0,0 @@ - - -stuff<-: - help: Do stuff to things - args: - things: - type: str - description: The things - render_by_default: true - execute: - - - -stuff: - help: Do stuff to things - - - diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index 1b924ad58..21c1fbb52 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -5,7 +5,6 @@ from tackle.models import Context from tackle.cli import main - # fmt: off INPUT_SOURCES = [ ("github.com/robcxyz/tackle-demo", @@ -59,10 +58,6 @@ def test_cli_parse_args_print_option( assert '{"stuff": "things"}' in capsys.readouterr().out -# def test_cli_parse_args_help_arg(change_curdir_fixtures): -# main(["tackle-help.yaml help"]) - - def test_cli_parse_args_help(): """Test help arg.""" with pytest.raises(SystemExit) as e: diff --git a/tests/cli/test_cli_help.py b/tests/cli/test_cli_help.py new file mode 100644 index 000000000..5dd254b51 --- /dev/null +++ b/tests/cli/test_cli_help.py @@ -0,0 +1,43 @@ +import pytest + +from tackle.cli import main + + +def test_cli_parse_args_help_arg_base(change_curdir_fixtures, capsys): + with pytest.raises(SystemExit): + main(["help.yaml", "help"]) + out, err = capsys.readouterr() + assert out == "Foo\n" + print(out, err) + + +def test_cli_parse_args_help_arg_for_func(change_curdir_fixtures, capsys): + with pytest.raises(SystemExit): + main(["help.yaml", "with_help", "help"]) + out, err = capsys.readouterr() + assert out == "Foo\n" + print(out, err) + + +def test_cli_parse_args_help_arg_for_func_with_method(change_curdir_fixtures, capsys): + with pytest.raises(SystemExit): + main(["help.yaml", "with_help_methods", "help"]) + out, err = capsys.readouterr() + assert out == "Foo\n" + print(out, err) + + +def test_cli_parse_args_help_arg_base_remote(change_curdir_fixtures, capsys): + with pytest.raises(SystemExit): + main(["robcxyz/tackle-hello-world", "help"]) + out, err = capsys.readouterr() + assert out == "Foo\n" + print(out, err) + + +def test_cli_parse_args_help_arg_for_func_remote(change_curdir_fixtures, capsys): + with pytest.raises(SystemExit): + main(["robcxyz/tackle-hello-world", "with_help", "help"]) + out, err = capsys.readouterr() + assert out == "Foo\n" + print(out, err)