Skip to content

Commit 88c1401

Browse files
authored
feat(docs): use mkdocs-click for documenting CLIs (#985)
1 parent 1ea34d8 commit 88c1401

File tree

9 files changed

+68
-58
lines changed

9 files changed

+68
-58
lines changed

docs/dev/documenting_clis.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Documenting CLIs
2+
3+
EEST command line interfaces (CLIs) are documented using the [`click`](https://click.palletsprojects.com) library's built-in help system and the [`mkdocs-click`](https://github.com/mkdocs/mkdocs-click) extension for mkdocs. This allows generation of CLI documentation directly from the (click) source code, ensuring that the documentation is always up-to-date with the code.
4+
5+
Current limitations:
6+
7+
1. `mkdocs serve` does not automatically update the CLI documentation when the source code changes. You must restart the server to see the changes.
8+
2. `mkdocs-click` does not automatically generate a short help string from sub-command docstrings. You must provide a short help string for each sub-command in the source code with `@click.command(short_help="...")`.
9+
10+
See the [markdown](https://github.com/ethereum/execution-spec-tests/blob/main/docs/library/cli/evm_bytes.md) and corresponding [Python docstrings](https://github.com/ethereum/execution-spec-tests/blob/main/src/cli/evm_bytes.py) for the [`evm_bytes` CLI documentation](../library/cli/evm_bytes.md) as an example of how to document a CLI using `mkdocs-click`.

docs/dev/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This documentation is aimed at maintainers of `execution-spec-tests` but may be
55
- [Managing configurations](./configurations.md): Instructions for setting up and modifying test configurations.
66
- [Interactive usage](./interactive_usage.md): Guide on interactive use of EEST packages using `ipython`.
77
- [Generating documentation](./docs.md): Steps to create and build documentation for the project.
8+
- [Documenting CLI commands](./documenting_clis.md): Instructions for documenting command line interfaces (CLIs).
89
- [Coding style](./coding_style.md): Standards and best practices for code formatting and to maintain consistency across the repository.
910
- [Enabling pre-commit checks](./precommit.md): A guide for setting up pre-commit hooks to enforce code quality before commits.
1011
- [Running github actions locally](./test_actions_locally.md): Instructions for testing GitHub Actions workflows on your local machine to streamline development and debugging.

docs/library/cli/evm_bytes.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
# The `evm_bytes` CLI
22

3-
::: cli.evm_bytes.cli
4-
options:
5-
show_source: false
6-
show_root_toc_entry: false
7-
8-
## `evm_bytes hex-string <bytecode>`
9-
10-
::: cli.evm_bytes.hex_string
11-
options:
12-
show_source: false
13-
show_root_toc_entry: false
14-
15-
## `evm_bytes binary-file <contract.bin>`
16-
17-
::: cli.evm_bytes.binary_file
18-
options:
19-
show_source: false
20-
show_root_toc_entry: false
3+
::: mkdocs-click
4+
:module: cli.evm_bytes
5+
:command: evm_bytes
6+
:depth: 1
7+
:list_subcommands: true

docs/navigation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* [Managing Configurations](dev/configurations.md)
3636
* [Interactive Library Usage](dev/interactive_usage.md)
3737
* [Generating Documentation](dev/docs.md)
38+
* [Documenting CLI Commands](dev/documenting_clis.md)
3839
* [Coding Style](dev/coding_style.md)
3940
* [Enabling Precommit Checks](dev/precommit.md)
4041
* [Running Github Actions Locally](dev/test_actions_locally.md)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ markdown_extensions:
7171
- def_list
7272
- footnotes
7373
- md_in_html
74+
- mkdocs-click
7475
- toc:
7576
permalink: true
7677
toc_depth: 4

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ docs = [
7777
"cairosvg>=2.7.0,<3",
7878
"mike>=1.1.2,<2",
7979
"mkdocs>=1.4.3,<2",
80+
"mkdocs-click>=0.8,<1",
8081
"mkdocs-gen-files>=0.5.0,<1",
8182
"mkdocs-git-authors-plugin>=0.7.1,<1",
8283
"mkdocs-glightbox>=0.3.4,<1",
@@ -104,7 +105,7 @@ eofwrap = "cli.eofwrap:eof_wrap"
104105
pyspelling_soft_fail = "cli.tox_helpers:pyspelling"
105106
markdownlintcli2_soft_fail = "cli.tox_helpers:markdownlint"
106107
order_fixtures = "cli.order_fixtures:order_fixtures"
107-
evm_bytes = "cli.evm_bytes:cli"
108+
evm_bytes = "cli.evm_bytes:evm_bytes"
108109
hasher = "cli.hasher:main"
109110
env_init = "config.env:create_default_config"
110111
et = "cli.et.cli:et"

src/cli/evm_bytes.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -148,94 +148,87 @@ def process_evm_bytes_string(evm_bytes_hex_string: str, assembly: bool = False)
148148
"--assembly",
149149
default=False,
150150
is_flag=True,
151-
help="Output the code as assembly instead of python.",
151+
help="Output the code as assembly instead of Python Opcodes.",
152152
)
153153

154154

155-
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
156-
def cli():
155+
@click.group("evm_bytes", context_settings=dict(help_option_names=["-h", "--help"]))
156+
def evm_bytes():
157157
"""
158-
Convert the given EVM bytes to EEST's python opcodes or assembly string.
158+
Convert EVM bytecode to EEST's Python Opcodes or an assembly string.
159159
160160
The input can be either a hex string or a binary file containing EVM bytes.
161161
"""
162162
pass
163163

164164

165-
@cli.command()
165+
@evm_bytes.command(short_help="Convert a hex string to Python Opcodes or assembly.")
166166
@assembly_option
167167
@click.argument("hex_string")
168168
def hex_string(hex_string: str, assembly: bool):
169169
"""
170-
Process a hex string representing EVM bytes and convert it into EEST's Python opcodes.
170+
Convert the HEX_STRING representing EVM bytes to EEST Python Opcodes.
171171
172-
Args:
173-
hex_string (str): The hex string representing the EVM bytes.
174-
assembly (bool): Whether to print the output as assembly or Python opcodes.
172+
HEX_STRING is a string containing EVM bytecode.
175173
176174
Returns:
175+
177176
(str): The processed EVM opcodes in Python or assembly format.
178177
179-
Example: Convert a hex string to EEST Python `Opcodes`
180-
```bash
178+
Example 1: Convert a hex string to EEST Python `Opcodes`
179+
181180
uv run evm_bytes hex-string 604260005260206000F3
182-
```
183181
184-
Output:
182+
Output 1:
185183
186-
```python
184+
\b
187185
Op.PUSH1[0x42] + Op.PUSH1[0x0] + Op.MSTORE + Op.PUSH1[0x20] + Op.PUSH1[0x0] + Op.RETURN
188-
```
189186
190-
Example: Convert a hex string to assembly
191-
```bash
187+
Example 2: Convert a hex string to assembly
188+
192189
uv run evm_bytes hex-string --assembly 604260005260206000F3
193-
```
194190
195-
Output:
191+
Output 2:
196192
197-
```text
193+
\b
198194
push1 0x42
199195
push1 0x00
200196
mstore
201197
push1 0x20
202198
push1 0x00
203199
return
204-
```
205-
""" # noqa: E501
200+
""" # noqa: D301
206201
processed_output = process_evm_bytes_string(hex_string, assembly=assembly)
207202
click.echo(processed_output)
208203

209204

210-
@cli.command()
205+
@evm_bytes.command(short_help="Convert a binary file to Python Opcodes or assembly.")
211206
@assembly_option
212-
@click.argument("binary_file_path", type=click.File("rb"))
213-
def binary_file(binary_file_path, assembly: bool):
207+
@click.argument("binary_file", type=click.File("rb"))
208+
def binary_file(binary_file, assembly: bool):
214209
"""
215-
Convert the given EVM bytes binary file.
210+
Convert the BINARY_FILE containing EVM bytes to Python Opcodes or assembly.
211+
212+
BINARY_FILE is a binary file containing EVM bytes, use `-` to read from stdin.
216213
217-
Args:
218-
binary_file_path (BinaryIO): A binary file containing EVM bytes to be processed or use `-`
219-
to read from stdin.
220-
assembly (bool): Whether to print the output as assembly or Python opcodes.
214+
Returns:
215+
216+
(str): The processed EVM opcodes in Python or assembly format.
221217
222218
Example: Convert the Withdrawal Request contract to assembly
223-
```bash
219+
220+
\b
224221
uv run evm_bytes binary-file ./src/ethereum_test_forks/forks/contracts/withdrawal_request.bin --assembly
225-
```
226222
227-
Output:
223+
Output:
228224
229-
```text
225+
\b
230226
caller
231227
push20 0xfffffffffffffffffffffffffffffffffffffffe
232228
eq
233229
push1 0x90
234230
jumpi
235231
...
236-
```
237-
""" # noqa: E501
238-
processed_output = format_opcodes(
239-
process_evm_bytes(binary_file_path.read()), assembly=assembly
240-
)
232+
""" # noqa: E501,D301
233+
processed_output = format_opcodes(process_evm_bytes(binary_file.read()), assembly=assembly)
241234
click.echo(processed_output)

uv.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

whitelist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ str
400400
streetsidesoftware
401401
subcall
402402
subclasscheck
403+
subcommands
403404
subdir
404405
subdirectories
405406
subdirectory

0 commit comments

Comments
 (0)