Skip to content

Commit

Permalink
wip: adding init help screen
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Sep 4, 2022
1 parent b495cf0 commit d3689d8
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 23 deletions.
92 changes: 92 additions & 0 deletions tackle/utils/help.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions tests/cli/fixtures/help-mixed.yaml
Original file line number Diff line number Diff line change
@@ -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}}
38 changes: 38 additions & 0 deletions tests/cli/fixtures/help.yaml
Original file line number Diff line number Diff line change
@@ -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}}
18 changes: 0 additions & 18 deletions tests/cli/help.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from tackle.models import Context
from tackle.cli import main


# fmt: off
INPUT_SOURCES = [
("github.com/robcxyz/tackle-demo",
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions tests/cli/test_cli_help.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d3689d8

Please sign in to comment.