Skip to main content

App

Struct App 

Source
pub struct App<C> { /* private fields */ }
Available on crate feature app only.
Expand description

One ctl process: short-circuits, help, parsing, execution, and presentation.

C is the CLI’s Clap parser. Build an App with Self::new, configure it with the builder methods, and finish with Self::run. Every builder takes self, so a setting set later replaces an earlier one. The one exception: Self::theme wins over the theme in Self::styles, whichever is called first.

Implementations§

Source§

impl<C> App<C>

Source

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

Build a CLI named bin with pretty output and automatic colour.

bin prefixes error lines (bin: message) and names the binary in JSON error envelopes. Self::view replaces the default view.

Source

pub fn view(self, select: impl Fn(&C) -> View + 'static) -> Self

Pick the View for a run from the parsed CLI, usually from OutputArgs.

The select closure runs after a successful parse. Help and parse errors come before it, so they read the format and colour from the raw arguments instead. The width, style, and layout settings of this App are applied on top of the view select returns.

Source

pub fn automatic_width_buffer(self, cells: u16) -> Self

Override the number of cells subtracted from an automatically detected width, for help, errors, and command output. Zero disables the subtraction. An explicit view width stays exact.

Source

pub fn automatic_width_buffer_envs(self, names: &'static [&'static str]) -> Self

Replace the ordered environment names that set the buffer, for every automatic-width render. An empty slice disables environment lookup.

Source

pub fn minimum_automatic_width(self, cells: u16) -> Self

Set the floor, in cells, for an automatically detected width.

Source

pub fn fallback_width(self, width: Option<u16>) -> Self

Lay out to width cells when no width is detected. None turns the fallback off, so tables render at their natural width.

Source

pub fn input_limits(self, limits: Limits) -> Self

Available on crate features input-toml or input-yaml only.

Set the process-wide default input limits as soon as it is called, not when the App runs. The limits of a single call win over them, and so does a repository’s .ctl/input.yaml with input-yaml.

Source

pub fn styles(self, styles: RenderOptions) -> Self

Take the theme, record style, table frame, row separation, and the variables they read from styles, for help, errors, and command output. Its colour and width settings are ignored.

Source

pub fn json_layout(self, layout: JsonLayout) -> Self

Lay out JSON output as layout. This beats the environment.

Source

pub fn json_layout_envs(self, names: &'static [&'static str]) -> Self

Replace the ordered environment names read for the JSON layout. An empty slice disables lookup.

Source

pub fn theme(self, theme: Theme) -> Self

Draw help, errors, and command output in theme, in place of Theme::default. It also replaces the theme a Self::styles value carries. Colour is still the colour mode’s choice: --color never writes no style.

Source

pub fn broken_pipe(self, policy: BrokenPipe) -> Self

Exit as policy says when the reader closes stdout or stderr before the output is written. The default, BrokenPipe::Ignore, exits with the command’s own code and writes nothing to stderr.

Source

pub fn before_parse( self, hook: impl Fn(&[OsString]) -> Option<ExitCode> + 'static, ) -> Self

Add a hook that runs before parsing and may end the run.

A hook gets the raw argv, program name first. Returning Some(code) stops the run with that code. Hooks run in the order added, after a --usage-spec request and before help and parsing.

Source

pub fn completion(self) -> Self
where C: CommandFactory + 'static,

Available on crate feature completion only.

Answer COMPLETE=<shell> before parsing, so the script the completion verb prints can ask this binary for candidates.

A run without the variable, or with it empty or 0, parses as usual. See the completion module.

Source

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

Available on crate feature usage only.

Answer a --usage-spec request from mise for the task named task.

When argv matches, the run prints the Usage spec to stdout and exits 0 without parsing. See the usage module for the argv shape.

Source

pub fn usage_spec( self, render: impl Fn(Command, &str) -> String + 'static, ) -> Self

Available on crate feature usage only.

Replace how the Usage document is rendered, keeping the short-circuit and the stdout write. The render callback receives the declared Clap command and the requested task name, and returns the document text.

Source§

impl<C> App<C>

Source

pub fn run<T>(self, execute: impl FnOnce(C) -> Result<T>) -> ExitCode
where T: Present,

Run against the process argv and return the exit code for main.

The handler execute gets the parsed CLI. An Err from it is written as an error (bin: message, or a JSON envelope) with exit code 1. Its message includes the whole anyhow chain.

§Panics

Panics when the command disables -h/--help or -V/--version.

Source

pub fn run_from<T>( self, args: impl IntoIterator<Item = impl Into<OsString>>, execute: impl FnOnce(C) -> Result<T>, ) -> ExitCode
where T: Present,

Run against explicit argv. The first item is the binary name.

The result is the same as Self::run. Use it in tests with a fixed argv.

§Panics

Panics when the command disables -h/--help or -V/--version.

Trait Implementations§

Source§

impl<C> Debug for App<C>

Names the binary only: the hooks are closures, which have no Debug.

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> !RefUnwindSafe for App<C>

§

impl<C> !Send for App<C>

§

impl<C> !Sync for App<C>

§

impl<C> !UnwindSafe for App<C>

§

impl<C> Forget for App<C>

§

impl<C> Freeze for App<C>
where Box<dyn Fn(&C) -> View>: Freeze, PhantomData<C>: Freeze,

§

impl<C> Unpin for App<C>
where Box<dyn Fn(&C) -> View>: Unpin, PhantomData<C>: Unpin,

§

impl<C> UnsafeUnpin for App<C>

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.