# openloops.command_syntax

Does each command of a run plan use only flags its program knows? (ADR-018)

A plan is checked before it is saved, so a step that would die on its first word
(`unrecognized arguments: --trust`) is caught by the planner, not by the owner at run
time. The check is cheap and read-only: for every simple command that passes long flags,
it asks the program for its help text (`<program> [<subcommand>...] --help`, no stdin,
pagers off, a short timeout) and reports the long flags the help never mentions.

It is a warning, not a proof. Help text that cannot be had (a program that does not
answer `--help`, BSD tools) is reported as such, and a program whose help omits some
flags gives false alarms; the planner reads each one. What it cannot see at all is a
requirement that depends on run-time state (“revive requires –cwd” when there is
nothing to resume): the runbook skill covers that by asking for every argument the
fallback path needs.

One seam: `help_of=`, `(argv) -> str` (empty when no help could be had), default
[`help_text()`](#openloops.command_syntax.help_text).

### Module Attributes

| [`SKIP_PROGRAMS`](#openloops.command_syntax.SKIP_PROGRAMS)   | Shell builtins and small tools whose flags are not worth a `--help` round trip (and whose BSD versions answer `--help` with an error).   |
|------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| [`LEAD_OPTIONS`](#openloops.command_syntax.LEAD_OPTIONS)    | Options a program takes before its subcommand, each with how many values follow it.                                                      |
| [`LAUNCHERS`](#openloops.command_syntax.LAUNCHERS)       | Programs that run a script or module named next, whose own subcommands follow it.                                                        |

### Functions

| [`help_text`](#openloops.command_syntax.help_text)(argv[, cwd, timeout])                  | `argv --help`'s output (run in *cwd*), or `""` when the program gives none (missing, an error without a usage line, a timeout, no option named) or when asking could run it (`_answers_help()`).   |
|---------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`long_flags`](#openloops.command_syntax.long_flags)(words)                                | The `--long` options in *words* (values after `=` dropped), in order, once each.                                                                                                                   |
| [`subcommand_path`](#openloops.command_syntax.subcommand_path)(words, \*[, depth])              | The program and up to *depth* subcommand-looking words after it.                                                                                                                                   |
| [`unknown_flags`](#openloops.command_syntax.unknown_flags)(command, \*[, cwd, help_of, skip]) | Per simple command with long flags: the flags its help text never mentions.                                                                                                                        |

### openloops.command_syntax.LAUNCHERS *= frozenset({'bun', 'deno', 'node', 'python', 'python3'})*

Programs that run a script or module named next, whose own subcommands follow it.

### openloops.command_syntax.LEAD_OPTIONS *: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [int](https://docs.python.org/3/builtins/functions.html#int)]]* *= {'gh': {'--repo': 1, '-R': 1}, 'git': {'--no-pager': 0, '-C': 1, '-P': 0, '-c': 1}}*

Options a program takes before its subcommand, each with how many values follow it.

### openloops.command_syntax.SKIP_PROGRAMS *= frozenset({'.', '[', '[[', 'awk', 'basename', 'cat', 'cd', 'chmod', 'command', 'cp', 'cut', 'date', 'dirname', 'echo', 'env', 'eval', 'exec', 'exit', 'export', 'false', 'find', 'grep', 'head', 'ln', 'ls', 'mkdir', 'mv', 'printf', 'read', 'return', 'rmdir', 'sed', 'seq', 'set', 'shift', 'sleep', 'sort', 'source', 'stat', 'tail', 'tee', 'test', 'touch', 'tr', 'true', 'type', 'uniq', 'unset', 'wait', 'wc', 'xargs'})*

Shell builtins and small tools whose flags are not worth a `--help` round trip
(and whose BSD versions answer `--help` with an error).

### openloops.command_syntax.help_text(argv, cwd=None, , timeout=20.0)

`argv --help`’s output (run in *cwd*), or `""` when the program gives none
(missing, an error without a usage line, a timeout, no option named) or when asking
could run it (`_answers_help()`).

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### openloops.command_syntax.long_flags(words)

The `--long` options in *words* (values after `=` dropped), in order, once each.

```pycon
>>> long_flags(['crowsnest', 'revive', 'x', '--trust', '--cwd=/a', '--', '--not'])
['--trust', '--cwd']
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### openloops.command_syntax.subcommand_path(words, , depth=2)

The program and up to *depth* subcommand-looking words after it.

```pycon
>>> subcommand_path(['crowsnest', 'revive', 'yoga', '--trust'])
['crowsnest', 'revive', 'yoga']
>>> subcommand_path(['gh', 'issue', 'comment', '46', '-R', 'o/r'])
['gh', 'issue', 'comment']
>>> subcommand_path(['git', '-C', 'x', 'pull', '-q', '--ff-only'])
['git', 'pull']
>>> subcommand_path(['python', '-m', 'desk', 'pin-passkeys', '--name', 'jobs'])
['python', '-m', 'desk', 'pin-passkeys']
>>> subcommand_path(['python', 'deploy.py', 'cmd-deploy', '--app', 'x'])
['python', 'deploy.py', 'cmd-deploy']
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### openloops.command_syntax.unknown_flags(command, \*, cwd=None, help_of=<function help_text>, skip=frozenset({'.', '[', '[[', 'awk', 'basename', 'cat', 'cd', 'chmod', 'command', 'cp', 'cut', 'date', 'dirname', 'echo', 'env', 'eval', 'exec', 'exit', 'export', 'false', 'find', 'grep', 'head', 'ln', 'ls', 'mkdir', 'mv', 'printf', 'read', 'return', 'rmdir', 'sed', 'seq', 'set', 'shift', 'sleep', 'sort', 'source', 'stat', 'tail', 'tee', 'test', 'touch', 'tr', 'true', 'type', 'uniq', 'unset', 'wait', 'wc', 'xargs'}))

Per simple command with long flags: the flags its help text never mentions.

Help is asked in the folder the command would be in: *cwd*, then each `cd DIR`
met along the way (a `cd` to a computed path leaves the folder unknown).

Each finding is `{"command": "<program subcommand>", "unknown": [...]}`, or
`{"command": ..., "unknown": [], "no_help": True}` when no help could be had.

```pycon
>>> helps = {('crowsnest', 'revive', 'yoga'): 'usage: revive [--cwd CWD] [--home HOME]'}
>>> unknown_flags('crowsnest revive yoga --trust', help_of=lambda a, c: helps.get(tuple(a), ''))
[{'command': 'crowsnest revive yoga', 'unknown': ['--trust']}]
>>> unknown_flags('crowsnest revive yoga --cwd x', help_of=lambda a, c: helps.get(tuple(a), ''))
[]
>>> unknown_flags('cd ~/x && ls --color', help_of=lambda a, c: '')
[]
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Any`](https://docs.python.org/3/library/typing.html#typing.Any)]]
