Ler parâmetros
Assinatura do handler
Section titled “Assinatura do handler”async def cmd_foo(inv: WireCommand, ctx: CliHandlerContext) -> None: ...| Parâmetro | Conteúdo |
|---|---|
inv | Comando parseado a partir da linha digitada |
ctx | Transporte BLE, buffer de entrada, UI — ver Enviar wire |
Campos de WireCommand
Section titled “Campos de WireCommand”from api.protocol_utils import WireCommand| Campo | Tipo | Significado |
|---|---|---|
name | str | Nome do comando (ex.: param_get) |
kind | "single" | "list_header" | "list_body" | Tipo wire |
is_response | bool | False = requisição (r), True = resposta (s) |
index | int | Índice da linha em list_body; 0 em single/header |
arguments | tuple[str, ...] | Argumentos de payload (já sem aspas wire) |
Para invocações via atalho nome(arg1, arg2), os argumentos ficam em inv.arguments.
Exemplos de leitura
Section titled “Exemplos de leitura”Sem argumentos
Section titled “Sem argumentos”helpasync def cmd_help(inv: WireCommand, ctx: CliHandlerContext) -> None: extra = inv.arguments # ()Com argumentos
Section titled “Com argumentos”set_speed(42)# inv.arguments == ("42",)speed = inv.arguments[0] if inv.arguments else "0"Vários argumentos
Section titled “Vários argumentos”echo(hello, "mundo com espaço")# inv.arguments == ("hello", "mundo com espaço")Wire completo digitado
Section titled “Wire completo digitado”param_get(s,r,"temperature");# inv.name == "param_get"# inv.kind == "single"# inv.is_response == False# inv.arguments == ("temperature",)Helpers de parse
Section titled “Helpers de parse”Importe de api.command_handlers ou api.protocol_utils:
from api.protocol_utils import unquote_field, split_top_level_commas
# Tokens individuais já vêm unquoted em inv.arguments.# Para parsear strings customizadas:parts = split_top_level_commas('a, "b,c", 42')CliHandlerContext
Section titled “CliHandlerContext”| Membro | Uso |
|---|---|
ctx.nus | Cliente Nordic UART (send_message) |
ctx.incoming | Router FIFO de comandos recebidos |
ctx.prompt_line(msg) | Pede uma linha ao usuário (async) |
ctx.log(msg, color) | Log colorido no terminal |
ctx.send_wire(text) | Envia mensagem wire (adiciona ; se faltar) |
Cores comuns: "WHITE", "GREEN", "YELLOW", "RED", "CYAN".
Múltiplos comandos na mesma linha
Section titled “Múltiplos comandos na mesma linha”Se o usuário digitar vários comandos registrados separados por ;, dispatch_cli_command chama cada handler em sequência.
Próximo passo
Section titled “Próximo passo”Enviar wire — montar e enviar mensagens ao dispositivo.