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").

Examples found in repository?
examples/taskr.rs (line 148)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
Source

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

Sets the display name shown in help and version output.

Examples found in repository?
examples/taskr.rs (line 149)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
Source

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

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

Examples found in repository?
examples/taskr.rs (line 150)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
Source

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

Sets the version string shown by --version.

Examples found in repository?
examples/taskr.rs (line 151)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
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.

Examples found in repository?
examples/taskr.rs (line 158)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
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.

Examples found in repository?
examples/taskr.rs (lines 159-169)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
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.

Examples found in repository?
examples/taskr.rs (lines 153-157)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
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, print_help_if_no_args: bool, subcommands: Vec<Command>, ) -> Self

👎Deprecated since 0.2.0:

Use App::add_command instead.

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.

Examples found in repository?
examples/taskr.rs (line 152)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}
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.

Examples found in repository?
examples/taskr.rs (line 200)
147fn main() {
148    App::new("taskr")
149        .name("Taskr")
150        .description("A toy task manager. Demonstrates all vecli features.")
151        .version("0.1.0")
152        .main(entry)
153        .flag(
154            Flag::global("verbose")
155                .alias("v")
156                .description("Enable verbose output."),
157        )
158        .print_help_on_fail(true)
159        .add_command(
160            Command::new("add", add)
161                .description("Add a new task.")
162                .usage("<task> [--priority <level>]")
163                .flag(
164                    Flag::new("priority")
165                        .alias("p")
166                        .description("Task priority: low, medium, or high."),
167                )
168                .strict_flags(true),
169        )
170        .add_command(
171            Command::new("list", list)
172                .description("List pending tasks.")
173                .flag(
174                    Flag::new("all")
175                        .alias("a")
176                        .description("Include completed tasks."),
177                )
178                .subcommand(
179                    Command::new("urgent", list_urgent)
180                        .description("List only high-priority tasks."),
181                ),
182        )
183        .add_command(
184            Command::new("done", done)
185                .description("Mark a task as done.")
186                .usage("<task>"),
187        )
188        .add_command(
189            Command::parent("config")
190                .description("Manage taskr configuration.")
191                .print_help_if_no_args(true)
192                .subcommand(
193                    Command::new("set", config_set)
194                        .description("Set a config value.")
195                        .usage("<key> <value>"),
196                )
197                .subcommand(Command::new("show", config_show).description("Show current config.")),
198        )
199        .add_command(Command::new("clear", clear).description("Clear all tasks."))
200        .run();
201}

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.