Skip to main content

Command

Struct Command 

Source
pub struct Command { /* private fields */ }
Available on crate feature std only.
Expand description

One node in the command tree.

Build with Command::new and refine with the chaining methods. Attach a run handler to do the work, arg to accept input, and subcommand to nest.

§Examples

use cli_forge::{Arg, Command};

let build = Command::new("build")
    .about("compile the project")
    .arg(Arg::flag("release").short('r'))
    .run(|m| {
        let _ = m.flag("release");
    });

Implementations§

Source§

impl Command

Source

pub fn new(name: impl Into<String>) -> Command

Create a command with the given invocation name.

§Examples
use cli_forge::Command;
let cmd = Command::new("init");
Source

pub fn alias(self, alias: impl Into<String>) -> Command

Add an alternative name that also invokes this command. Chain it to add several. Aliases are shown alongside the name in help.

§Examples
use cli_forge::Command;
let cmd = Command::new("remove").alias("rm").alias("del");
Source

pub fn aliases<I, S>(self, aliases: I) -> Command
where I: IntoIterator<Item = S>, S: Into<String>,

Add several alternative names at once.

§Examples
use cli_forge::Command;
let cmd = Command::new("remove").aliases(["rm", "del"]);
Source

pub fn about(self, text: impl Into<String>) -> Command

Set the one-line description shown in help.

§Examples
use cli_forge::Command;
let cmd = Command::new("init").about("bootstrap a new project");
Source

pub fn arg(self, arg: Arg) -> Command

Accept an argument. Add as many as the command needs; positionals are filled in the order they are added.

§Examples
use cli_forge::{Arg, Command};
let cmd = Command::new("copy")
    .arg(Arg::positional("from").required(true))
    .arg(Arg::positional("to").required(true))
    .arg(Arg::flag("force").short('f'));
Source

pub fn subcommand(self, cmd: Command) -> Command

Nest a subcommand. Subcommands compose recursively to any depth.

§Examples
use cli_forge::Command;
let remote = Command::new("remote")
    .subcommand(Command::new("add"))
    .subcommand(Command::new("remove"));
Source

pub fn hidden(self, yes: bool) -> Command

Hide the command from generated help while leaving it invokable.

§Examples
use cli_forge::Command;
let cmd = Command::new("debug-dump").hidden(true);
Source

pub fn requires_auth(self, yes: bool) -> Command

Mark the command as requiring authentication.

With the auth feature enabled, the command runs — and appears in help — only when the app’s App::auth hook authorizes it; otherwise invoking it yields ParseError::Unauthorized. Without the auth feature the flag is inert (the command runs and shows normally).

§Examples
use cli_forge::Command;
let cmd = Command::new("publish").requires_auth(true);
Source

pub fn run(self, handler: impl Fn(&Matches) + 'static) -> Command

Attach the handler run when this command is selected. It receives the Matches parsed for this command’s level.

§Examples
use cli_forge::{out, Command};
let cmd = Command::new("hello").run(|_| out("hello"));

Trait Implementations§

Source§

impl Debug for Command

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.