diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index bbbb977dd..a2b6e5d49 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -43,16 +43,16 @@ repos:
- --allow-past-years
- --fuzzy-match-generates-todo
- --comment-style
- - ''
+ - ""
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.6
hooks:
- - id: ruff
- name: Run Ruff linter
- args: [ --fix ]
- - id: ruff-format
- name: Run Ruff formatter
+ - id: ruff
+ name: Run Ruff linter
+ args: [--fix]
+ - id: ruff-format
+ name: Run Ruff formatter
- repo: https://github.com/pycqa/pylint
rev: "v3.3.4"
@@ -62,9 +62,9 @@ repos:
description: This hook runs pylint.
types: [python]
args:
- - -rn # Only display messages
- - -sn # Don't display the score
- - --rcfile=pyproject.toml # Link to config file
+ - -rn # Only display messages
+ - -sn # Don't display the score
+ - --rcfile=pyproject.toml # Link to config file
additional_dependencies:
- anta[cli]
- types-PyYAML
@@ -123,5 +123,14 @@ repos:
pass_filenames: false
additional_dependencies:
- anta[cli]
- # TODO: next can go once we have it added to anta properly
- - numpydoc
+ - id: doc-snippets
+ name: Generate doc snippets
+ entry: >-
+ sh -c "docs/scripts/generate_doc_snippets.py"
+ language: python
+ types: [python]
+ files: anta/cli/
+ verbose: true
+ pass_filenames: false
+ additional_dependencies:
+ - anta[cli]
diff --git a/anta/cli/nrfu/__init__.py b/anta/cli/nrfu/__init__.py
index a6f76c4cd..6dc912dcc 100644
--- a/anta/cli/nrfu/__init__.py
+++ b/anta/cli/nrfu/__init__.py
@@ -42,9 +42,10 @@ def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
if "--help" not in args:
raise
- # remove the required params so that help can display
+ # Fake presence of the required params so that help can display
for param in self.params:
- param.required = False
+ if param.required:
+ param.value_is_missing = lambda value: False # type: ignore[method-assign] # noqa: ARG005
return super().parse_args(ctx, args)
diff --git a/anta/cli/nrfu/commands.py b/anta/cli/nrfu/commands.py
index d1a72a01f..ed0f43244 100644
--- a/anta/cli/nrfu/commands.py
+++ b/anta/cli/nrfu/commands.py
@@ -45,7 +45,10 @@ def table(ctx: click.Context, group_by: Literal["device", "test"] | None) -> Non
help="Path to save report as a JSON file",
)
def json(ctx: click.Context, output: pathlib.Path | None) -> None:
- """ANTA command to check network state with JSON results."""
+ """ANTA command to check network state with JSON results.
+
+ If no `--output` is specified, the output is printed to stdout.
+ """
run_tests(ctx)
print_json(ctx, output=output)
exit_with_code(ctx)
@@ -72,11 +75,11 @@ def text(ctx: click.Context) -> None:
path_type=pathlib.Path,
),
show_envvar=True,
- required=False,
+ required=True,
help="Path to save report as a CSV file",
)
def csv(ctx: click.Context, csv_output: pathlib.Path) -> None:
- """ANTA command to check network states with CSV result."""
+ """ANTA command to check network state with CSV report."""
run_tests(ctx)
save_to_csv(ctx, csv_file=csv_output)
exit_with_code(ctx)
diff --git a/docs/cli/nrfu.md b/docs/cli/nrfu.md
index cb008a327..9e06f3645 100644
--- a/docs/cli/nrfu.md
+++ b/docs/cli/nrfu.md
@@ -48,12 +48,7 @@ The `text` subcommand provides a straightforward text report for each test execu
### Command overview
```bash
-Usage: anta nrfu text [OPTIONS]
-
- ANTA command to check network states with text result.
-
-Options:
- --help Show this message and exit.
+--8<-- "anta_nrfu_text_help.txt"
```
### Example
@@ -71,13 +66,7 @@ The `table` command under the `anta nrfu` namespace offers a clear and organized
### Command overview
```bash
-Usage: anta nrfu table [OPTIONS]
-
- ANTA command to check network states with table result.
-
-Options:
- --group-by [device|test] Group result by test or device.
- --help Show this message and exit.
+--8<-- "anta_nrfu_table_help.txt"
```
The `--group-by` option show a summarized view of the test results per host or per test.
@@ -125,15 +114,7 @@ The JSON rendering command in NRFU testing will generate an output of all test r
### Command overview
```bash
-anta nrfu json --help
-Usage: anta nrfu json [OPTIONS]
-
- ANTA command to check network state with JSON result.
-
-Options:
- -o, --output FILE Path to save report as a JSON file [env var:
- ANTA_NRFU_JSON_OUTPUT]
- --help Show this message and exit.
+--8<-- "anta_nrfu_json_help.txt"
```
The `--output` option allows you to save the JSON report as a file. If specified, no output will be displayed in the terminal. This is useful for further processing or integration with other tools.
@@ -153,15 +134,7 @@ The `csv` command in NRFU testing is useful for generating a CSV file with all t
### Command overview
```bash
-anta nrfu csv --help
-Usage: anta nrfu csv [OPTIONS]
-
- ANTA command to check network states with CSV result.
-
-Options:
- --csv-output FILE Path to save report as a CSV file [env var:
- ANTA_NRFU_CSV_CSV_OUTPUT]
- --help Show this message and exit.
+--8<-- "anta_nrfu_csv_help.txt"
```
### Example
@@ -175,16 +148,7 @@ The `md-report` command in NRFU testing generates a comprehensive Markdown repor
### Command overview
```bash
-anta nrfu md-report --help
-
-Usage: anta nrfu md-report [OPTIONS]
-
- ANTA command to check network state with Markdown report.
-
-Options:
- --md-output FILE Path to save the report as a Markdown file [env var:
- ANTA_NRFU_MD_REPORT_MD_OUTPUT; required]
- --help Show this message and exit.
+--8<-- "anta_nrfu_mdreport_help.txt"
```
### Example
@@ -198,17 +162,7 @@ ANTA offers a CLI option for creating custom reports. This leverages the Jinja2
### Command overview
```bash
-anta nrfu tpl-report --help
-Usage: anta nrfu tpl-report [OPTIONS]
-
- ANTA command to check network state with templated report
-
-Options:
- -tpl, --template FILE Path to the template to use for the report [env var:
- ANTA_NRFU_TPL_REPORT_TEMPLATE; required]
- -o, --output FILE Path to save report as a file [env var:
- ANTA_NRFU_TPL_REPORT_OUTPUT]
- --help Show this message and exit.
+--8<-- "anta_nrfu_tplreport_help.txt"
```
The `--template` option is used to specify the Jinja2 template file for generating the custom report.
diff --git a/docs/getting-started.md b/docs/getting-started.md
index b36ea74c7..878e04be7 100644
--- a/docs/getting-started.md
+++ b/docs/getting-started.md
@@ -86,12 +86,14 @@ This entrypoint has multiple options to manage test coverage and reporting.
--8<-- "anta_help.txt"
```
+To run the NRFU, you need to select an output format amongst [`csv`, `json`, `md-report`, `table`, `text`, `tpl-report`].
+
+For a first usage, `table` is recommended. By default all test results for all devices are rendered but it can be changed to a report per test case or per host
+
```bash
--8<-- "anta_nrfu_help.txt"
```
-To run the NRFU, you need to select an output format amongst ["json", "table", "text", "tpl-report"]. For a first usage, `table` is recommended. By default all test results for all devices are rendered but it can be changed to a report per test case or per host
-
!!! Note
The following examples shows how to pass all the CLI options.
diff --git a/docs/imgs/anta_debug_help.svg b/docs/imgs/anta_debug_help.svg
new file mode 100644
index 000000000..7c8f271ca
--- /dev/null
+++ b/docs/imgs/anta_debug_help.svg
@@ -0,0 +1,100 @@
+
diff --git a/docs/imgs/anta_help.svg b/docs/imgs/anta_help.svg
new file mode 100644
index 000000000..8a8f8f37b
--- /dev/null
+++ b/docs/imgs/anta_help.svg
@@ -0,0 +1,139 @@
+
diff --git a/docs/imgs/anta_nrfu_csv_help.svg b/docs/imgs/anta_nrfu_csv_help.svg
new file mode 100644
index 000000000..8657d5538
--- /dev/null
+++ b/docs/imgs/anta_nrfu_csv_help.svg
@@ -0,0 +1,91 @@
+
diff --git a/docs/imgs/anta_nrfu_help.svg b/docs/imgs/anta_nrfu_help.svg
new file mode 100644
index 000000000..d687be309
--- /dev/null
+++ b/docs/imgs/anta_nrfu_help.svg
@@ -0,0 +1,303 @@
+
diff --git a/docs/imgs/anta_nrfu_json_help.svg b/docs/imgs/anta_nrfu_json_help.svg
new file mode 100644
index 000000000..f546ace15
--- /dev/null
+++ b/docs/imgs/anta_nrfu_json_help.svg
@@ -0,0 +1,91 @@
+
diff --git a/docs/imgs/anta_nrfu_mdreport_help.svg b/docs/imgs/anta_nrfu_mdreport_help.svg
new file mode 100644
index 000000000..b0c3964f2
--- /dev/null
+++ b/docs/imgs/anta_nrfu_mdreport_help.svg
@@ -0,0 +1,91 @@
+
diff --git a/docs/imgs/anta_nrfu_table_help.svg b/docs/imgs/anta_nrfu_table_help.svg
new file mode 100644
index 000000000..55448db64
--- /dev/null
+++ b/docs/imgs/anta_nrfu_table_help.svg
@@ -0,0 +1,87 @@
+
diff --git a/docs/imgs/anta_nrfu_text_help.svg b/docs/imgs/anta_nrfu_text_help.svg
new file mode 100644
index 000000000..c5929b9c1
--- /dev/null
+++ b/docs/imgs/anta_nrfu_text_help.svg
@@ -0,0 +1,83 @@
+
diff --git a/docs/imgs/anta_nrfu_tplreport_help.svg b/docs/imgs/anta_nrfu_tplreport_help.svg
new file mode 100644
index 000000000..77f30a06d
--- /dev/null
+++ b/docs/imgs/anta_nrfu_tplreport_help.svg
@@ -0,0 +1,99 @@
+
diff --git a/docs/scripts/__init__.py b/docs/scripts/__init__.py
deleted file mode 100644
index e702a5103..000000000
--- a/docs/scripts/__init__.py
+++ /dev/null
@@ -1,4 +0,0 @@
-# Copyright (c) 2024-2025 Arista Networks, Inc.
-# Use of this source code is governed by the Apache License 2.0
-# that can be found in the LICENSE file.
-"""Scripts for ANTA documentation."""
diff --git a/docs/scripts/generate_doc_snippets.py b/docs/scripts/generate_doc_snippets.py
new file mode 100755
index 000000000..ccaa02ee7
--- /dev/null
+++ b/docs/scripts/generate_doc_snippets.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+# Copyright (c) 2024-2025 Arista Networks, Inc.
+# Use of this source code is governed by the Apache License 2.0
+# that can be found in the LICENSE file.
+"""Generates SVG for documentation purposes."""
+
+import sys
+from pathlib import Path
+
+# TODO: svg in another PR
+from generate_snippet import main as generate_snippet
+
+sys.path.insert(0, str(Path(__file__).parents[2]))
+
+COMMANDS = [
+ "anta --help",
+ "anta nrfu --help",
+ "anta nrfu csv --help",
+ "anta nrfu json --help",
+ "anta nrfu table --help",
+ "anta nrfu text --help",
+ "anta nrfu tpl-report --help",
+ "anta nrfu md-report --help",
+]
+
+for command in COMMANDS:
+ # TODO: svg in another PR
+ generate_snippet(command.split(" "), output="txt")
diff --git a/docs/scripts/generate_svg.py b/docs/scripts/generate_snippet.py
old mode 100644
new mode 100755
similarity index 62%
rename from docs/scripts/generate_svg.py
rename to docs/scripts/generate_snippet.py
index 0add9f1b2..da3064553
--- a/docs/scripts/generate_svg.py
+++ b/docs/scripts/generate_snippet.py
@@ -1,11 +1,12 @@
+#!/usr/bin/env python
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
-"""A script to generate svg files from anta command.
+"""A script to generate svg or txt files from anta command.
usage:
-python generate_svg.py anta ...
+python generate_snippet.py anta ...
"""
# This script is not a package
# ruff: noqa: INP001
@@ -20,12 +21,15 @@
from contextlib import redirect_stdout, suppress
from importlib import import_module
from importlib.metadata import entry_points
+from typing import Literal
from unittest.mock import patch
-from rich.console import Console
from rich.logging import RichHandler
+from rich.markup import escape
from rich.progress import Progress
+sys.path.insert(0, str(pathlib.Path(__file__).parents[2]))
+
from anta.cli.console import console
from anta.cli.nrfu.utils import anta_progress_bar
@@ -35,9 +39,6 @@
root.addHandler(r)
-OUTPUT_DIR = pathlib.Path(__file__).parent.parent / "imgs"
-
-
def custom_progress_bar() -> Progress:
"""Set the console of progress_bar to main anta console.
@@ -50,12 +51,14 @@ def custom_progress_bar() -> Progress:
return progress
-if __name__ == "__main__":
+def main(args: list[str], output: Literal["svg", "txt"] = "svg") -> None:
+ """Execute the script."""
# Sane rich size
os.environ["COLUMNS"] = "120"
+ output_dir = pathlib.Path(__file__).parent.parent / "snippets" if output == "txt" else pathlib.Path(__file__).parent.parent / "imgs"
+
# stolen from https://github.com/ewels/rich-click/blob/main/src/rich_click/cli.py
- args = sys.argv[1:]
script_name = args[0]
console_scripts = entry_points(group="console_scripts")
scripts = {script.name: script for script in console_scripts}
@@ -80,27 +83,32 @@ def custom_progress_bar() -> Progress:
module = import_module(module_path)
function = getattr(module, function_name)
- # Console to captur everything
- new_console = Console(record=True)
-
pipe = io.StringIO()
console.record = True
console.file = pipe
- with redirect_stdout(io.StringIO()) as f:
- # tweaks to record and redirect to a dummy file
-
- console.print(f"ant@anthill$ {' '.join(sys.argv)}")
-
- # Redirect stdout of the program towards another StringIO to capture help
- # that is not part or anta rich console
- # redirect potential progress bar output to console by patching
- with patch("anta.cli.nrfu.utils.anta_progress_bar", custom_progress_bar), suppress(SystemExit):
- function()
+ # Redirect stdout of the program towards another StringIO to capture help
+ # that is not part or anta rich console
+ # redirect potential progress bar output to console by patching
+ with redirect_stdout(io.StringIO()) as f, patch("anta.cli.nrfu.utils.anta_progress_bar", custom_progress_bar), suppress(SystemExit):
+ if output == "txt":
+ console.print(f"$ {' '.join(sys.argv)}")
+ function()
if "--help" in args:
- console.print(f.getvalue())
+ console.print(escape(f.getvalue()))
+
+ filename = f"{'_'.join(x.replace('/', '_').replace('-', '').replace('.', '') for x in args)}.{output}"
+ filename = output_dir / filename
+ if output == "txt":
+ content = console.export_text()[:-1]
+ with filename.open("w") as fd:
+ fd.write(content)
+ # TODO: Not using this to avoid newline console.save_text(str(filename))
+ elif output == "svg":
+ console.save_svg(str(filename), title=" ".join(args))
- filename = f"{'_'.join(x.replace('/', '_').replace('-', '_').replace('.', '_') for x in args)}.svg"
- filename = f"{OUTPUT_DIR}/{filename}"
print(f"File saved at {filename}")
- console.save_svg(filename, title=" ".join(args))
+
+
+if __name__ == "__main__":
+ main(sys.argv[1:], "txt")
diff --git a/docs/snippets/anta_debug_help.txt b/docs/snippets/anta_debug_help.txt
new file mode 100644
index 000000000..0b74be25b
--- /dev/null
+++ b/docs/snippets/anta_debug_help.txt
@@ -0,0 +1,11 @@
+$ anta debug --help
+Usage: anta debug [OPTIONS] COMMAND [ARGS]...
+
+ Commands to execute EOS commands on remote devices.
+
+Options:
+ --help Show this message and exit.
+
+Commands:
+ run-cmd Run arbitrary command to an ANTA device.
+ run-template Run arbitrary templated command to an ANTA device.
diff --git a/docs/snippets/anta_help.txt b/docs/snippets/anta_help.txt
index 7bc37adeb..dd552fd04 100644
--- a/docs/snippets/anta_help.txt
+++ b/docs/snippets/anta_help.txt
@@ -1,8 +1,10 @@
+$ anta --help
Usage: anta [OPTIONS] COMMAND [ARGS]...
Arista Network Test Automation (ANTA) CLI.
Options:
+ --help Show this message and exit.
--version Show the version and exit.
--log-file FILE Send the logs to a file. If logging level is
DEBUG, only INFO or higher will be sent to
@@ -10,7 +12,6 @@ Options:
-l, --log-level [CRITICAL|ERROR|WARNING|INFO|DEBUG]
ANTA logging level [env var:
ANTA_LOG_LEVEL; default: INFO]
- --help Show this message and exit.
Commands:
check Commands to validate configuration files.
diff --git a/docs/snippets/anta_nrfu_csv_help.txt b/docs/snippets/anta_nrfu_csv_help.txt
new file mode 100644
index 000000000..483b1c7d4
--- /dev/null
+++ b/docs/snippets/anta_nrfu_csv_help.txt
@@ -0,0 +1,9 @@
+$ anta nrfu csv --help
+Usage: anta nrfu csv [OPTIONS]
+
+ ANTA command to check network state with CSV report.
+
+Options:
+ --csv-output FILE Path to save report as a CSV file [env var:
+ ANTA_NRFU_CSV_CSV_OUTPUT; required]
+ --help Show this message and exit.
diff --git a/docs/snippets/anta_nrfu_help.txt b/docs/snippets/anta_nrfu_help.txt
index cb23fa7ed..5801f4e3a 100644
--- a/docs/snippets/anta_nrfu_help.txt
+++ b/docs/snippets/anta_nrfu_help.txt
@@ -1,3 +1,4 @@
+$ anta nrfu --help
Usage: anta nrfu [OPTIONS] COMMAND [ARGS]...
Run ANTA tests on selected inventory devices.
diff --git a/docs/snippets/anta_nrfu_json_help.txt b/docs/snippets/anta_nrfu_json_help.txt
new file mode 100644
index 000000000..6aebec9c4
--- /dev/null
+++ b/docs/snippets/anta_nrfu_json_help.txt
@@ -0,0 +1,11 @@
+$ anta nrfu json --help
+Usage: anta nrfu json [OPTIONS]
+
+ ANTA command to check network state with JSON results.
+
+ If no `--output` is specified, the output is printed to stdout.
+
+Options:
+ -o, --output FILE Path to save report as a JSON file [env var:
+ ANTA_NRFU_JSON_OUTPUT]
+ --help Show this message and exit.
diff --git a/docs/snippets/anta_nrfu_mdreport_help.txt b/docs/snippets/anta_nrfu_mdreport_help.txt
new file mode 100644
index 000000000..0d4581190
--- /dev/null
+++ b/docs/snippets/anta_nrfu_mdreport_help.txt
@@ -0,0 +1,9 @@
+$ anta nrfu md-report --help
+Usage: anta nrfu md-report [OPTIONS]
+
+ ANTA command to check network state with Markdown report.
+
+Options:
+ --md-output FILE Path to save the report as a Markdown file [env var:
+ ANTA_NRFU_MD_REPORT_MD_OUTPUT; required]
+ --help Show this message and exit.
diff --git a/docs/snippets/anta_nrfu_table_help.txt b/docs/snippets/anta_nrfu_table_help.txt
new file mode 100644
index 000000000..9d368ab96
--- /dev/null
+++ b/docs/snippets/anta_nrfu_table_help.txt
@@ -0,0 +1,8 @@
+$ anta nrfu table --help
+Usage: anta nrfu table [OPTIONS]
+
+ ANTA command to check network state with table results.
+
+Options:
+ --group-by [device|test] Group result by test or device.
+ --help Show this message and exit.
diff --git a/docs/snippets/anta_nrfu_text_help.txt b/docs/snippets/anta_nrfu_text_help.txt
new file mode 100644
index 000000000..3bc587a90
--- /dev/null
+++ b/docs/snippets/anta_nrfu_text_help.txt
@@ -0,0 +1,7 @@
+$ anta nrfu text --help
+Usage: anta nrfu text [OPTIONS]
+
+ ANTA command to check network state with text results.
+
+Options:
+ --help Show this message and exit.
diff --git a/docs/snippets/anta_nrfu_tplreport_help.txt b/docs/snippets/anta_nrfu_tplreport_help.txt
new file mode 100644
index 000000000..b19bc8c19
--- /dev/null
+++ b/docs/snippets/anta_nrfu_tplreport_help.txt
@@ -0,0 +1,11 @@
+$ anta nrfu tpl-report --help
+Usage: anta nrfu tpl-report [OPTIONS]
+
+ ANTA command to check network state with templated report.
+
+Options:
+ -tpl, --template FILE Path to the template to use for the report [env var:
+ ANTA_NRFU_TPL_REPORT_TEMPLATE; required]
+ -o, --output FILE Path to save report as a file [env var:
+ ANTA_NRFU_TPL_REPORT_OUTPUT]
+ --help Show this message and exit.