https://github.com/miksus/scriptor
High-level abstraction for command-line
https://github.com/miksus/scriptor
command-line library program subprocess
Last synced: 11 months ago
JSON representation
High-level abstraction for command-line
- Host: GitHub
- URL: https://github.com/miksus/scriptor
- Owner: Miksus
- License: mit
- Created: 2022-11-12T10:35:46.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-16T07:21:03.000Z (about 3 years ago)
- Last Synced: 2025-02-28T07:53:57.602Z (12 months ago)
- Topics: command-line, library, program, subprocess
- Language: Python
- Homepage: https://scriptor.readthedocs.io/
- Size: 153 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Scriptor
> Run command-line programs in Python
---
[](https://pypi.org/project/scriptor/)
[](https://github.com/Miksus/scriptor/actions/workflows/main.yml)
[](https://codecov.io/gh/Miksus/scriptor)
[](https://scriptor.readthedocs.io)
[](https://pypi.org/project/scriptor/)
- [Documentation](https://scriptor.readthedocs.io)
- [Source code](https://github.com/Miksus/scriptor)
- [Releases](https://pypi.org/project/scriptor/)
## What is it?
Scriptor is a high-level library for command-line.
Scriptor makes it easy to integrate other CLI programs to your Python application.
Core features:
- Run programs sync or async using the same syntax
- High-level program abstraction
- Easy program parametrization
Install it from PyPI:
```shell
pip install scriptor
```
## Why Scriptor?
Scriptor abstracts ``subprocess`` and ``asyncio.subprocess``
to the same syntax making it easy to use both of them and
switch between.
```python
>>> from scriptor import Program
>>> python = Program("python3")
>>> # Call the program (and wait for finish)
>>> python("myscript.py")
```
## More Examples
Here are some examples:
```python
>>> # Parametrize a script
>>> python("myscript.py", report_date="2022-11-11")
>>> # Use different current working directory
>>> python.use(cwd="path/to/dir")("myscript.py")
>>> # Run script with output (in stdout)
>>> python("print_hello.py")
'Hello world'
>>> # Run failing script
>>> python("failing.py")
Traceback (most recent call last):
...
scriptor.process.ProcessError: Traceback (most recent call last):
File "failing.py", line 1, in
raise RuntimeError("Oops!")
RuntimeError: Oops!
```
Start a process:
```python
>>> process = python.start("print_hello.py")
>>> process.finished
False
>>> # Wait for the process to finish
>>> process.wait()
>>> # Raise error if process failed
>>> process.raise_for_return()
>>> # Read the results
>>> process.read()
'Hello world'
```
Some more examples with async:
```python
>>> # Parametrize a script
>>> await python.call_async("myscript.py", report_date="2022-11-11")
>>> # Run script with output (in stdout)
>>> await python.call_async("print_hello.py")
'Hello world'
```
Start with async:
```python
>>> process = await python.start_async("print_hello.py")
>>> process.finished
False
>>> # Wait for the process to finish
>>> process.wait()
>>> # Raise error if process failed
>>> process.raise_for_return()
>>> # Read the results
>>> process.read()
'Hello world'
```
Change settings ie. the current working directory:
```python
>>> git = Program('git')
>>> repo_1 = git.use(cwd="path/to/repo_1")
>>> repo_2 = git.use(cwd="path/to/repo_2")
>>> repo_1("status")
"""On branch main
nothing to commit, working tree clean"""
```
---
See more from the documentation.
If the library helped you, consider buying a coffee for the maintainer ☕.
## Author
* **Mikael Koli** - [Miksus](https://github.com/Miksus) - koli.mikael@gmail.com