Skip to content

Commit

Permalink
fix(core): simplify conditions to display defaults and types
Browse files Browse the repository at this point in the history
  • Loading branch information
rndmh3ro committed Oct 11, 2024
1 parent da5aa77 commit 989992d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
63 changes: 32 additions & 31 deletions aar_doc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,40 +188,41 @@ def parse_options(ctx: typer.Context) -> dict:
.replace("\n", " ")
.strip()
)
details["display_type"] = details.get("type", "str")
details["display_default"] = ""
if details["display_type"] == "bool":
details["display_default"] = (
"true" if details.get("default", False) else "false"
)
elif details["display_type"] == "list":
if default := details.get("default", None):
details["display_default"] = json.dumps(default)
if "elements" in details:
if (details["elements"] == "dict") and ("options" in details):
details["display_type"] = (
f"list of dicts of '{option}' options"
)
else:
details["display_type"] = (
"list of '" + details["elements"] + "'"
)
elif details["display_type"] == "dict":
if default := details.get("default", None):
details["display_default"] = json.dumps(default)

display_type = details.get("type", "str")

if display_type == "list":
elements = details.get("elements", "")
if elements == "dict" and "options" in details:
display_type = f"list of dicts of '{option}' options"
else:
display_type = f"list of '{elements}'"
elif display_type == "dict":
if "options" in details:
details["display_type"] = f"dict of '{option}' options"
elif details["display_type"] == "str":
try:
details["display_default"] = details.get("default", "").strip()
except AttributeError as exc:
typer.echo(
f"The default value of the argument {option} "
f"is of type {type(details.get('default')).__name__}, need str",
display_type = f"dict of '{option}' options"

details["display_type"] = display_type

if "default" in details:
default_value = details.get("default", "")
details["display_default"] = str(default_value).strip()

if display_type in ["list", "dict"]:
details["display_default"] = (
json.dumps(default_value) if default_value else ""
)
raise typer.Exit(code=1) from exc

elif display_type == "str":
if not isinstance(default_value, str):
typer.echo(
f"The default value of the argument {option} "
f"is of type {type(default_value).__name__}, need str",
)
raise typer.Exit(1)

else:
details["display_default"] = str(details.get("default", "")).strip()
details["display_default"] = ""

entrypoint_options[entrypoint] = gathered_options

return entrypoint_options
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/roles/extended/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ The bool entry point for the extended role.

|Option|Description|Type|Required|Default|
|---|---|---|---|---|
| bool_true | A true boolean value | bool | no | true |
| bool_false | A false boolean value | bool | no | false |
| bool_yes | A truthy boolean value | bool | no | true |
| bool_no | A falsy boolean value | bool | no | false |
| bool_true | A true boolean value | bool | no | True |
| bool_false | A false boolean value | bool | no | False |
| bool_yes | A truthy boolean value | bool | no | yes |
| bool_no | A falsy boolean value | bool | no | no |



Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/roles/meta_main_yaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ The bool entry point for the extended role.

|Option|Description|Type|Required|Default|
|---|---|---|---|---|
| bool_true | A true boolean value | bool | no | true |
| bool_false | A false boolean value | bool | no | false |
| bool_yes | A truthy boolean value | bool | no | true |
| bool_no | A falsy boolean value | bool | no | false |
| bool_true | A true boolean value | bool | no | True |
| bool_false | A false boolean value | bool | no | False |
| bool_yes | A truthy boolean value | bool | no | yes |
| bool_no | A falsy boolean value | bool | no | no |



Expand Down

0 comments on commit 989992d

Please sign in to comment.