Skip to main content

Engine

Struct Engine 

Source
pub struct Engine<'a> {
    pub args_p: &'a mut ArgsP,
    pub cmds: CmdMap<'a>,
    pub subcmds: SubCmdsMap<'a>,
    pub basic_handler: BasicHandler<'a>,
    pub about: String,
}

Fields§

§args_p: &'a mut ArgsP§cmds: CmdMap<'a>§subcmds: SubCmdsMap<'a>§basic_handler: BasicHandler<'a>§about: String

Implementations§

Source§

impl Engine<'_>

Source

pub fn process(&mut self, cmd: &str, args: &[&str]) -> ErrorType

Source§

impl<'a> Engine<'a>

Source

pub fn new(args_p: &'a mut ArgsP, about: String) -> Self

Examples found in repository?
examples/demo.rs (line 109)
107fn main() {
108    let mut ap = ArgsP::new();
109    let mut engine = Engine::new(&mut ap, "TAP DEMO — lightweight CLI parsing".to_string());
110
111    commands! {
112        engine;
113        "greet"   => greet_handler   { optional: ["name"],         docs: "Greet someone. Flag: --loud" }
114        "add"     => add_handler     { required: ["a", "b"],       docs: "Sum two numbers" }
115        "serve"   => serve_handler   { optional: ["host", "port"], docs: "Start a server. Flags: --verbose, --tls=on|off|auto, --workers=N" }
116        "report"  => report_handler  {                             docs: "Render templates and JSON" }
117        "version" => version_handler {                             docs: "Print version" }
118    }
119
120    subcommands! {
121        engine;
122        "echo" | "say" => echo_handler {
123            unlimited: true,
124            docs: "Echo back all arguments (subcommand)."
125        }
126    }
127
128    add_help_handler(vec!["help", "-h"], &mut engine);
129
130    if let Err(e) = engine.main() {
131        cprintln!("[?RD][error][?RT] {}", e);
132        std::process::exit(1);
133    }
134}
Source

pub fn getter(&'a self) -> EngineGetter<'a>

Source

pub fn new_command(&mut self, name: &'a str, cmdinfo: CmdInfoCmd<'a>)

Source

pub fn new_commands(&mut self, names: Vec<&'a str>, cmdinfo: CmdInfoCmd<'a>)

Source

pub fn new_subcommand(&mut self, name: &'a str, cmdinfo: CmdInfoSubcmd<'a>)

Source

pub fn new_subcommands( &mut self, names: Vec<&'a str>, cmdinfo: CmdInfoSubcmd<'a>, )

Source

pub fn work(&mut self, args: &[&str]) -> ErrorType

Source

pub fn main(&mut self) -> ErrorType

Examples found in repository?
examples/demo.rs (line 130)
107fn main() {
108    let mut ap = ArgsP::new();
109    let mut engine = Engine::new(&mut ap, "TAP DEMO — lightweight CLI parsing".to_string());
110
111    commands! {
112        engine;
113        "greet"   => greet_handler   { optional: ["name"],         docs: "Greet someone. Flag: --loud" }
114        "add"     => add_handler     { required: ["a", "b"],       docs: "Sum two numbers" }
115        "serve"   => serve_handler   { optional: ["host", "port"], docs: "Start a server. Flags: --verbose, --tls=on|off|auto, --workers=N" }
116        "report"  => report_handler  {                             docs: "Render templates and JSON" }
117        "version" => version_handler {                             docs: "Print version" }
118    }
119
120    subcommands! {
121        engine;
122        "echo" | "say" => echo_handler {
123            unlimited: true,
124            docs: "Echo back all arguments (subcommand)."
125        }
126    }
127
128    add_help_handler(vec!["help", "-h"], &mut engine);
129
130    if let Err(e) = engine.main() {
131        cprintln!("[?RD][error][?RT] {}", e);
132        std::process::exit(1);
133    }
134}
Source§

impl Engine<'_>

Source

pub fn flag(&self, name: &str) -> bool

Examples found in repository?
examples/demo.rs (line 28)
23fn greet_handler(e: &mut Engine, raw: &[&str]) -> ErrorType {
24    args! { raw;
25        name: String = "World".to_string(),
26    }
27    cprintln!("[?BGN]Hello, [?YW]{}[?BGN]![?RT]", name);
28    if e.flag("loud") {
29        cprintln!("[?BD][?RD]>>> LOUD MODE ON <<<[?RT]");
30    }
31    Ok(())
32}
33
34/// `add <a> <b>` — typed parsing with helpful errors on bad input.
35fn add_handler(_: &mut Engine, raw: &[&str]) -> ErrorType {
36    args! { raw;
37        a: f64,
38        b: f64,
39    }
40    cprintln!("[?GN]{} + {} = [?BGN]{}[?RT]", a, b, a + b);
41    Ok(())
42}
43
44/// `serve [host] [port] [--verbose] [--tls=on|off|auto] [--workers=N]`
45fn serve_handler(e: &mut Engine, raw: &[&str]) -> ErrorType {
46    args! { raw;
47        host: String = "127.0.0.1".to_string(),
48        port: u16 = 8080,
49    }
50    ensure!(port > 0, "port must be positive (got {})", port);
51
52    let verbose = e.flag("verbose");
53    let tls = e.value("tls").unwrap_or("off");
54    ensure!(
55        matches!(tls, "on" | "off" | "auto"),
56        "invalid --tls value {:?} (expected on|off|auto)",
57        tls
58    );
59    let workers: u16 = e.value_t("workers")?.unwrap_or(4);
60    ensure!(workers > 0, "--workers must be positive");
61
62    cprintln!("[?YW]Starting server...[?RT]");
63    cprintln!("  [?CN]host[?RT]    = {}", host);
64    cprintln!("  [?CN]port[?RT]    = {}", port);
65    cprintln!("  [?CN]tls[?RT]     = {}", tls);
66    cprintln!("  [?CN]workers[?RT] = {}", workers);
67    cprintln!("  [?CN]verbose[?RT] = {}", verbose);
68    Ok(())
69}
Source

pub fn value(&self, name: &str) -> Option<&str>

Examples found in repository?
examples/demo.rs (line 53)
45fn serve_handler(e: &mut Engine, raw: &[&str]) -> ErrorType {
46    args! { raw;
47        host: String = "127.0.0.1".to_string(),
48        port: u16 = 8080,
49    }
50    ensure!(port > 0, "port must be positive (got {})", port);
51
52    let verbose = e.flag("verbose");
53    let tls = e.value("tls").unwrap_or("off");
54    ensure!(
55        matches!(tls, "on" | "off" | "auto"),
56        "invalid --tls value {:?} (expected on|off|auto)",
57        tls
58    );
59    let workers: u16 = e.value_t("workers")?.unwrap_or(4);
60    ensure!(workers > 0, "--workers must be positive");
61
62    cprintln!("[?YW]Starting server...[?RT]");
63    cprintln!("  [?CN]host[?RT]    = {}", host);
64    cprintln!("  [?CN]port[?RT]    = {}", port);
65    cprintln!("  [?CN]tls[?RT]     = {}", tls);
66    cprintln!("  [?CN]workers[?RT] = {}", workers);
67    cprintln!("  [?CN]verbose[?RT] = {}", verbose);
68    Ok(())
69}
Source

pub fn value_t<T: FromStr>(&self, name: &str) -> Result<Option<T>, String>
where T::Err: Display,

Examples found in repository?
examples/demo.rs (line 59)
45fn serve_handler(e: &mut Engine, raw: &[&str]) -> ErrorType {
46    args! { raw;
47        host: String = "127.0.0.1".to_string(),
48        port: u16 = 8080,
49    }
50    ensure!(port > 0, "port must be positive (got {})", port);
51
52    let verbose = e.flag("verbose");
53    let tls = e.value("tls").unwrap_or("off");
54    ensure!(
55        matches!(tls, "on" | "off" | "auto"),
56        "invalid --tls value {:?} (expected on|off|auto)",
57        tls
58    );
59    let workers: u16 = e.value_t("workers")?.unwrap_or(4);
60    ensure!(workers > 0, "--workers must be positive");
61
62    cprintln!("[?YW]Starting server...[?RT]");
63    cprintln!("  [?CN]host[?RT]    = {}", host);
64    cprintln!("  [?CN]port[?RT]    = {}", port);
65    cprintln!("  [?CN]tls[?RT]     = {}", tls);
66    cprintln!("  [?CN]workers[?RT] = {}", workers);
67    cprintln!("  [?CN]verbose[?RT] = {}", verbose);
68    Ok(())
69}

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Engine<'a>

§

impl<'a> !Send for Engine<'a>

§

impl<'a> !Sync for Engine<'a>

§

impl<'a> !UnwindSafe for Engine<'a>

§

impl<'a> Freeze for Engine<'a>

§

impl<'a> Unpin for Engine<'a>

§

impl<'a> UnsafeUnpin for Engine<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.