Skip to content

Commit 7d564d6

Browse files
authored
feat: Add CLI to python package (#350)
Usage with uv for example: `uv run -m encoderfile build ...`
1 parent 0c71940 commit 7d564d6

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from encoderfile import run_cli
2+
import sys
3+
4+
if __name__ == "__main__":
5+
args = ["encoderfile"] + sys.argv[1:]
6+
7+
try:
8+
run_cli(args)
9+
except RuntimeError as e:
10+
print(e, file=sys.stderr)

encoderfile-py/src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use pyo3::{exceptions::PyRuntimeError, prelude::*};
2+
3+
// For use ONLY in encoderfile-py's __main__.py
4+
#[pyfunction]
5+
pub fn run_cli(args: Vec<String>) -> PyResult<()> {
6+
encoderfile::builder::cli::run_cli(args).map_err(|e| PyRuntimeError::new_err(format!("{}", e)))
7+
}

encoderfile-py/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use pyo3::prelude::*;
22

33
mod builder;
4+
mod cli;
45

56
/// A Python module implemented in Rust.
67
#[pymodule(name = "_core")]
@@ -31,4 +32,7 @@ mod encoderfile {
3132

3233
#[pymodule_export]
3334
use super::builder::PyTokenizerBuildConfig;
35+
36+
#[pymodule_export]
37+
use super::cli::run_cli;
3438
}

encoderfile/src/builder/cli/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ pub use build::{test_build_args, test_build_args_working_dir};
1313
pub use build::BuildArgs;
1414
pub use inspect::inspect_encoderfile;
1515

16+
pub fn run_cli(args: Vec<String>) -> Result<()> {
17+
use clap::Parser;
18+
19+
let cli = Cli::try_parse_from(args).map_err(|e| anyhow::anyhow!(e))?;
20+
21+
cli.command.run(&cli.global_args)
22+
}
23+
1624
#[derive(Debug, Parser)]
1725
pub struct Cli {
1826
#[command(subcommand)]

0 commit comments

Comments
 (0)