Skip to content

Add non-interactive mode#60

Open
jerem wants to merge 1 commit into
tinyerp:masterfrom
jerem:non-interactive-mode
Open

Add non-interactive mode#60
jerem wants to merge 1 commit into
tinyerp:masterfrom
jerem:non-interactive-mode

Conversation

@jerem
Copy link
Copy Markdown

@jerem jerem commented May 12, 2026

Summary

Add a non-interactive mode so Odooly can be used in scripts and one-off automations without dropping into the REPL.

  • --command "<code>" runs an inline Python snippet against a connected client, then exits.
  • When stdin is not a TTY (piped script, heredoc, CI), stdin is read as a script and executed, then exits.

In both cases client and env are pre-populated as globals. Client._set_interactive() is not called, so the snippet behaves like a regular Python script: login errors propagate as exceptions, and REPL-only helpers like client.connect() raise.

Note on the flag name

The de-facto standard short flag for "run inline code" is -c (python -c, bash -c, sh -c, perl -e/-c, etc.). In Odooly -c is already taken by --config, so this PR uses --command (long form only).

If you would like to free up -c for the convention, we could rename the short flag for --config in a follow-up. Options:

  • -C, --config FILE (uppercase -C, used by git -C and make -C for related concepts). Closest to the existing UX.
  • Drop the short flag entirely and keep --config as long form only. Cleanest, very small breaking change for users who relied on -c.
  • -f, --config FILE. Generic, less idiomatic.

This would be a breaking CLI change, so happy to keep --command as-is if you prefer to leave -c alone. Let me know which direction you want and I will fold it into this PR or open a separate one.

Examples

odooly --env demo --command 'print(env["res.users"].search_count([]))'
odooly --env demo < provision.py
cat <<'PY' | odooly --env demo
users = env['res.users'].search([])
print(len(users), 'users')
PY

Notes

  • exec semantics, not REPL semantics: bare expressions do not auto-print, use print(...). This matches python -c "...".
  • Existing interactive tests pin sys.stdin.isatty to True since pytest stdin is not a real TTY; otherwise auto-detection would route them to the script path.

Test plan

  • python -m unittest tests.test_interact tests.test_client tests.test_model tests.test_util: 286 tests pass (284 existing plus 2 new).
  • New test_command covers --command (no REPL invoked, snippet runs, env/client available).
  • New test_stdin_pipe covers non-tty stdin (no REPL invoked, stdin read and executed).
  • Manual smoke test against a live Odoo instance.

Run a Python snippet against a connected client and exit, instead of
dropping into the REPL.  Two ways to trigger:

* ``--command "<code>"`` passes the code as a string;
* a non-tty stdin is read as a script (e.g. ``odooly --env demo <
  script.py``).

In both cases the ``client`` and ``env`` globals are pre-populated and
``_set_interactive`` is not called, so the snippet runs as a plain
Python script (``client.connect()`` raises, login errors propagate).
@jerem jerem marked this pull request as draft May 12, 2026 06:35
@jerem jerem marked this pull request as ready for review May 12, 2026 06:43
@florentx
Copy link
Copy Markdown
Member

Thank you for the idea, and the implementation.

Maybe I will add this feature, or part of it in a next release.


I've tested alternatives, and there's ways to achieve this without much boilerplate:

~$ python3 <<'PY'
> import odooly
> client = odooly.Client.from_config('demo')
> env = client.env
> users = env['res.users'].search([])
> print(len(users), 'users')
> PY
28 users
~$ python3 <<<'from odooly import Client;print(Client.from_config("demo").env["res.users"].search_count([]))'
28

@florentx florentx added the enhancement New feature or request label May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants