feat: reorganize workbook into 3 tracks (core, engineering-2026, ecosystems)
This commit is contained in:
1 parent
82677455b2
commit
2e002b6176
64 files changed
+2295
-196
No files matched your search
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "eco01-cli-clap"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
@@ -0,0 +1,18 @@
|
||||
# 01 — Parsowanie argumentów CLI z `clap`
|
||||
|
||||
`clap` (Command Line Argument Parser) to absolutny standard w ekosystemie Rusta do budowy narzędzi CLI.
|
||||
|
||||
## Zakres
|
||||
* Deklaratywne makra `#[derive(Parser)]` i `#[derive(Subcommand)]`
|
||||
* Flagi (`--verbose` / `-v`), opcje z wartościami domyślnymi, argumenty pozycyjne i podkomendy (`add`, `list`, `done`)
|
||||
* Testowanie parsera bez uruchamiania procesu: `Cli::try_parse_from(...)`
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
1. Uzupełnij atrybuty `clap` w strukturze `Cli` i enumie `Command` (np. `#[arg(short, long)]`, `#[command(subcommand)]`).
|
||||
2. Zaimplementuj `parse_cli(args: &[&str]) -> Result<Cli, clap::Error>` używając `Cli::try_parse_from(args)`.
|
||||
3. Zaimplementuj `describe_command(cli: &Cli) -> String`, aby zamienić sparsowaną komendę na czytelny opis (w prawdziwym CLI byłoby tu wywołanie logiki biznesowej).
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=eco01-cli-clap
|
||||
```
|
||||
@@ -0,0 +1,78 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Debug, Parser, PartialEq, Eq)]
|
||||
#[command(name = "todoctl", about = "Mini CLI do nauki clap")]
|
||||
pub struct Cli {
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
pub verbose: bool,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand, PartialEq, Eq)]
|
||||
pub enum Command {
|
||||
/// Dodaj nowe zadanie
|
||||
Add {
|
||||
/// Tytuł zadania
|
||||
title: String,
|
||||
|
||||
/// Priorytet (1-5)
|
||||
#[arg(short, long, default_value_t = 3)]
|
||||
priority: u8,
|
||||
},
|
||||
/// Wyświetl listę zadań
|
||||
List {
|
||||
/// Pokaż również ukończone
|
||||
#[arg(long, default_value_t = false)]
|
||||
all: bool,
|
||||
},
|
||||
/// Oznacz zadanie jako ukończone
|
||||
Done { id: u64 },
|
||||
}
|
||||
|
||||
pub fn parse_cli(args: &[&str]) -> Result<Cli, clap::Error> {
|
||||
todo!("Użyj Cli::try_parse_from(args)")
|
||||
}
|
||||
|
||||
pub fn describe_command(cli: &Cli) -> String {
|
||||
todo!(
|
||||
"Zwróć: Add -> \"add:<title>:p<priority>\", List -> \"list:all=<bool>\", Done -> \"done:<id>\""
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_add_with_defaults_and_flags() {
|
||||
let cli = parse_cli(&["todoctl", "-v", "add", "Learn clap"]).unwrap();
|
||||
assert!(cli.verbose);
|
||||
assert_eq!(
|
||||
cli.command,
|
||||
Command::Add {
|
||||
title: String::from("Learn clap"),
|
||||
priority: 3
|
||||
}
|
||||
);
|
||||
assert_eq!(describe_command(&cli), "add:Learn clap:p3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_list_and_done_subcommands() {
|
||||
let list_cli = parse_cli(&["todoctl", "list", "--all"]).unwrap();
|
||||
assert_eq!(describe_command(&list_cli), "list:all=true");
|
||||
|
||||
let done_cli = parse_cli(&["todoctl", "done", "42"]).unwrap();
|
||||
assert_eq!(describe_command(&done_cli), "done:42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_arguments() {
|
||||
assert!(parse_cli(&["todoctl", "done", "not-a-number"]).is_err());
|
||||
assert!(parse_cli(&["todoctl", "unknown"]).is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user