Skip to main content

App

Struct App 

Source
pub struct App { /* private fields */ }
Expand description

The top-level CLI application builder.

Construct with App::new, chain configuration methods, register commands with App::add_command, then call App::run to parse std::env::args and dispatch to the appropriate handler.

§Example

use vecli::{App, Command, CommandContext};

fn greet(_ctx: &CommandContext) {
    println!("Hello!");
}

let app = App::new("mytool")
    .name("My Tool")
    .version("1.0.0")
    .add_command(Command::new("greet", greet));

// app.run();

Implementations§

Source§

impl App

Source

pub fn new(prog_name: impl Into<String>) -> Self

Creates a new App with the given program name.

prog_name is used in usage strings and error messages (typically the binary name, e.g. "mytool").

Source

pub fn name(self, name: impl Into<String>) -> Self

Sets the display name shown in help and version output.

Source

pub fn description(self, desc: impl Into<String>) -> Self

Sets the description shown in the app-level help output.

Source

pub fn version(self, version: impl Into<String>) -> Self

Sets the version string shown by --version.

Source

pub fn print_help_if_no_args(self, show: bool) -> Self

When true, prints help and exits if no arguments are provided.

Mutually exclusive with App::main. If both are set, the main entry point takes priority and a warning is printed to stderr.

Source

pub fn print_help_on_fail(self, show: bool) -> Self

When true, prints the full help listing after any dispatch error.

Source

pub fn strict_flags(self, strict: bool) -> Self

When true, aborts with an error if an unknown app-level flag is passed.

When false (the default), unknown flags produce a warning and execution continues. Per-command strict mode is configured separately via Command::strict_flags.

Source

pub fn add_command(self, command: Command) -> Self

Registers a command built with the Command builder.

Source

pub fn flag(self, flag: Flag) -> Self

Registers a flag on the app.

If the flag was created with Flag::global, it is available to all commands and merged into CommandContext::flags automatically. Otherwise it is treated as an entry-point flag, visible in the OPTIONS section of help and delivered via PassedFlags to the main entry handler.

Source

pub fn add_command_param( self, name: impl Into<String>, flags: Option<Vec<Flag>>, description: impl Into<String>, handler: fn(&CommandContext), usage: Option<impl Into<String>>, strict_flags: bool, ) -> Self

Registers a command from individual parameters without the Command builder.

Prefer App::add_command for most cases. This variant is useful when constructing commands dynamically at runtime.

Source

pub fn main(self, entry: fn(PassedFlags)) -> Self

Registers a handler called when no subcommand is provided.

The handler receives a PassedFlags map containing any flags the user passed before a subcommand. Mutually exclusive with App::print_help_if_no_args; if both are set, this handler takes priority.

Source

pub fn print_help(&self)

Prints the app-level help text to stdout.

Output includes the app name, version, description, COMMANDS listing, OPTIONS (entry-point flags and --version), and GLOBAL FLAGS.

Source

pub fn run(self)

Parses std::env::args, resolves aliases, and dispatches to the matching command handler.

Handles the following built-in flags before reaching any user-defined handler:

  • --help / -h: prints command-specific or app-level help and exits.
  • --version: prints the app name and version and exits.

If no subcommand is provided and a main entry point is registered, the entry handler is called with the resolved flags. If no subcommand is found in the registry, an error is printed and the function returns without calling any handler. When print_help_on_fail is set, the full help listing is also printed.

Trait Implementations§

Source§

impl Default for App

Source§

fn default() -> App

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for App

§

impl RefUnwindSafe for App

§

impl Send for App

§

impl Sync for App

§

impl Unpin for App

§

impl UnsafeUnpin for App

§

impl UnwindSafe for App

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.