-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import click | ||
|
||
|
||
class _CommandWrapper(object): | ||
def __init__(self, command=None, children=None): | ||
self.command = command | ||
self.children = [] | ||
|
||
@property | ||
def name(self): | ||
return self.command.name | ||
|
||
|
||
def _build_command_tree(click_command): | ||
wrapper = _CommandWrapper(click_command) | ||
|
||
if isinstance(click_command, click.core.Group): | ||
for _, cmd in click_command.commands.items(): | ||
if not getattr(cmd, "hidden", False): | ||
wrapper.children.append(_build_command_tree(cmd)) | ||
|
||
return wrapper | ||
|
||
|
||
def _print_tree(command, depth=0, is_last_item=False, is_last_parent=False): | ||
if depth == 0: | ||
prefix = '' | ||
tree_item = '' | ||
else: | ||
prefix = ' ' if is_last_parent else '│ ' | ||
tree_item = '└── ' if is_last_item else '├── ' | ||
|
||
root_line = '│ ' if is_last_parent else '' | ||
|
||
line = root_line + prefix * (depth - 1) + tree_item + command.name | ||
|
||
click.echo(line) | ||
|
||
for i, child in enumerate(sorted(command.children, key=lambda x: x.name)): | ||
_print_tree(child, | ||
depth=(depth + 1), | ||
is_last_item=(i == (len(command.children) - 1)), | ||
is_last_parent=is_last_item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import click | ||
from opnsense_cli.click_addons.command_tree import _build_command_tree, _print_tree | ||
|
||
|
||
@click.command() | ||
@click.pass_context | ||
def tree(ctx): | ||
""" | ||
Show the command tree of your CLI | ||
""" | ||
root_cmd = _build_command_tree(ctx.find_root().command) | ||
_print_tree(root_cmd) | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
from click.testing import CliRunner | ||
from opnsense_cli.commands.tree import tree | ||
import click | ||
import textwrap | ||
|
||
|
||
class TestTreeCommands(unittest.TestCase): | ||
def test_tree(self): | ||
@click.group(name='root') | ||
def root(): | ||
pass | ||
|
||
@root.command(name='command-one') | ||
def command_one(): | ||
pass | ||
|
||
@root.command(name='command-two') | ||
def command_two(): | ||
pass | ||
|
||
@click.group(name='sub_level1') | ||
def sub_level1(): | ||
pass | ||
|
||
@click.group(name='sub_level2') | ||
def sub_level2(): | ||
pass | ||
|
||
root.add_command(tree) | ||
|
||
root.add_command(sub_level1) | ||
sub_level1.add_command(command_one) | ||
sub_level1.add_command(command_two) | ||
|
||
sub_level1.add_command(sub_level2) | ||
sub_level2.add_command(command_one) | ||
sub_level2.add_command(command_two) | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(root, ['tree']) | ||
|
||
tree_output = textwrap.dedent("""\ | ||
root | ||
├── command-one | ||
├── command-two | ||
├── sub_level1 | ||
│ ├── command-one | ||
│ ├── command-two | ||
│ └── sub_level2 | ||
│ ├── command-one | ||
│ └── command-two | ||
└── tree | ||
""") | ||
|
||
self.assertEqual(tree_output, result.output) |