Tato, proszę… nie przychodź dzisiaj do szkoły, dobrze?

Tato, proszę… nie przychodź dzisiaj do szkoły, dobrze?
Dlaczego, Kinga? Nie chcesz zobaczyć, jak odbierasz nagrodę?
Nie, tato. Przyjdą moi koledzy i ich rodzice, a ty…
Co ja?
Jesteś cały w kurzu, tato. Znowu przyszedłeś prosto z budowy.

Mężczyzna stał nieruchomo. W dłoni trzymał zwiędły kwiatek, który zerwał przy drodze.
Tak jest powiedział łagodnie. Przyszedłem od razu, bo nie zdążyłem się przebrać. Nie chciałem się spóźnić.
To nie ma znaczenia, tato! Mówiłam, że nie chcę tego! krzyknęła. Wszyscy się ze mnie będą śmiać!

Ojciec pokręcił głową, nie odzywając się.
Dobrze, Kinga. Nie przyjdę.

A ona odwróciła się powoli, trzymając kwiat w dłoni.

Kinga dorastała w małym domku z blachy. Matka odeszła, gdy miała pięć lat.
Ojciec, Jan, pracował dzień w dzień, w deszcz i mróz, by kupić jej książki, ubrania wszystko, co mógł.
Tato, nie mamy lodówki!
Nic nie szkodzi, córeczko, zostawimy jedzenie na balkonie tam jest zimniej.

Lata mijały. Kinga dostała stypendium, potem poszła na studia do Warszawy.
Ojciec oddał jej ostatnie grosze.
Trzymaj się, dziewczyno, dbaj o swoje życie.
Tato, a ty z czym zostaniesz?
Cieszę się, że widzę cię jako silną kobietę.
Wrócę, obiecuję. I zabiorę cię ze sobą powiedziała, obejmując go.
Uśmiechnął się naprawdę.
Nie musisz mnie wozić, córeczko. Mi tu dobrze, z moimi kurami.

Minęły dwa lata.
Ojciec często dzwonił, ale Kinga rzadko odbierała.
Tato, jestem zajęta, mam pracę, mam zajęcia…
Rozumiem, córeczko. Nie zapomnij zjeść, dobrze?
Tak, tato, pa!

Pewnego dnia niespodziewanie przyjechał do miasta, by przynieść jej pierogi i placek.
Dotarł pod jej blok, ale zat# The `Command` trait

`Command` is the trait that should be implemented by any type that you want to use as a command. It defines the behavior of the command when it is executed and how it should be parsed from the command line.

Here is the definition of the `Command` trait (with some details omitted for simplicity):

„`rust
pub trait Command: Sized {
/// The name of the command, used in help messages.
const NAME: &’static str;

/// The arguments of the command.
type Args: Args;

/// The help message for the command.
fn help() -> Help;

/// Parse the command from the command line arguments.
fn parse(matches: &ArgMatches) -> Result;

/// Run the command.
fn run(self) -> Result<(), Error>;
}
„`

## Example

Let’s see how to implement a simple command that prints a greeting message. The command will have a single argument, the name of the person to greet.

„`rust
use clap::{ArgMatches, Args, Command as ClapCommand, FromArgMatches};
use std::error::Error;

#[derive(Debug)]
struct GreetCommand {
name: String,
}

impl Command for GreetCommand {
const NAME: &’static str = „greet”;

type Args = GreetArgs;

fn help() -> Help {
Help::new(Self::NAME, „Prints a greeting message”)
.arg(Arg::new(„name”, „The name of the person to greet”).required(true))
}

fn parse(matches: &ArgMatches) -> Result {
let args = GreetArgs::from_arg_matches(matches)?;
Ok(Self { name: args.name })
}

fn run(self) -> Result<(), Error> {
println!(„Hello, {}!”, self.name);
Ok(())
}
}

#[derive(Debug, Args)]
struct GreetArgs {
name: String,
}
„`

## The `help` method

The `help` method returns a `Help` object that describes the command and its arguments. The `Help` object is used to generate the help message for the command.

## The `parse` method

The `parse` method is responsible for parsing the command from the command line arguments. It takes an `ArgMatches` object as input and returns a `Result` with the parsed command or an error. The `ArgMatches` object contains the parsed arguments from the command line.

## The `run` method

The `run` method is where the command’s logic is implemented. It takes the parsed command as input and returns a `Result` indicating whether the command was executed successfully or not. In this example, the `run` method simply prints a greeting message to the console.

## Using the command

Once you’ve implemented the `Command` trait for your type, you can use it with the `clap` command-line parser. Here’s an example of how to create a CLI application with the `greet` command:

„`rust
use clap::Parser;

#[derive(Parser)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}

#[derive(clap::Subcommand)]
enum Commands {
Greet(GreetCommand),
}

fn main() -> Result<(), Box> {
let cli = Cli::parse();

match cli.command {
Commands::Greet(cmd) => cmd.run()?,
}

Ok(())
}
„`

When you run the program with the `greet` command, it will print the greeting message:

„`sh
$ myapp greet –name Alice
Hello, Alice!
„`

## Conclusion

The `Command` trait provides a flexible and type-safe way to define CLI commands in Rust. By implementing this trait for your types, you can easily integrate them with the `clap` command-line parser and build powerful CLI applications.

Oceń artykuł
Dodaj komentarze

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!:

osiemnaście − 12 =

Tato, proszę… nie przychodź dzisiaj do szkoły, dobrze?