Skip to content

Commit

Permalink
Merge pull request #402 from zapta/develop
Browse files Browse the repository at this point in the history
Fixed the apio help text logic.
  • Loading branch information
Obijuan authored Sep 3, 2024
2 parents 69ce43a + cbfb1b8 commit a3499cd
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions apio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import string
import click
import re

from apio import util

Expand Down Expand Up @@ -77,29 +78,27 @@ def get_command(self, ctx, cmd_name: string):
return nnss.get("cli")


def find_commands_help(help_list, commands):
def select_commands_help(command_lines, command_names):
"""
Return a list with the given commands and their descriptions.
This information is read from the given help list of string
Given a list of click generated help lines for all the commands,
return a subset that includes only the commands in command_names.
The result order is by command_lines which happens to be alphabetical.
- INPUTS:
* help_list: A list of help lines (strings)
* commands: A list of commands (strings) to obtain their descriptions
* command_lines: Click generated help lines for all the commands.
* commands: A list of commands names to select.
"""

# -- List to return
commands_help = []

# -- Analyze all the lines in the help string
for line in help_list:
# -- Check all the commands passed
for command in commands:
# -- If the command is in the current line of the help string
if f" {command}" in line:
# -- Add the and its description to the list of commands
commands_help.append(line)
result = []

for command_line in command_lines:
# Extract command name. This is the first word.
# E.g.: " build Synthesize the bitstream."
command_name = re.findall(r"^\s*(\S+)\s+.*$", command_line)[0]
if command_name in command_names:
result.append(command_line)

# -- Return the list of comands with their descriptions
return commands_help
return result


# ------------------------------------------------------------------
Expand All @@ -118,28 +117,18 @@ def cli(ctx):

# -- No command typed: show help
if ctx.invoked_subcommand is None:
# -- The help string is automatically generated by Click
# -- It could be directly printed on the console but...
_help = ctx.get_help()

# -- Let's change the format so that the commands are divided
# -- into three categories: Project, Setup and Utilities
# -- Convert the help string into a list of lines
_help = _help.split("\n")

# -- Setup commands
setup_help = find_commands_help(
_help, ["drivers", "init", "install", "uninstall"]
)

# -- Utility commands
util_help = find_commands_help(
_help, ["boards", "config", "examples", "raw", "system", "upgrade"]
)

# -- Project commands:
cmd_help = find_commands_help(
_help,
# -- The auto generated click help lines (apio --help)
help_lines = ctx.get_help().split("\n")

# -- Split the help lines into header and command groups.
# -- We later split the command lines into command groups.
index = help_lines.index("Commands:")
header_lines = help_lines[:index]
command_lines = help_lines[index + 1 :]

# -- Select project commands:
project_help = select_commands_help(
command_lines,
[
"build",
"clean",
Expand All @@ -152,17 +141,27 @@ def cli(ctx):
"graph",
],
)
# -- Select setup commands
setup_help = select_commands_help(
command_lines, ["drivers", "init", "install", "uninstall"]
)

# -- Select utility commands
util_help = select_commands_help(
command_lines,
["boards", "config", "examples", "raw", "system", "upgrade"],
)

# -- Get the Help header
index = _help.index("Commands:")
header_help = _help[:index]
# -- Sanity check, in case we mispelled or ommited a command name.
num_selected = len(project_help) + len(setup_help) + len(util_help)
assert len(command_lines) == num_selected

# -- Print header
click.secho("\n".join(header_help))
click.secho("\n".join(header_lines))

# -- Print project commands:
click.secho("Project commands:")
click.secho("\n".join(cmd_help))
click.secho("\n".join(project_help))
click.secho()

# -- Print Setup commands:
Expand Down

0 comments on commit a3499cd

Please sign in to comment.