task_manager_command_line/app/cli.rs
1//! Defines the command-line interface (CLI) structure using `clap`.
2//!
3//! This module specifies the application's commands, subcommands, and arguments,
4//! allowing `clap` to parse user input from the terminal.
5
6use chrono::NaiveDate;
7use clap::{Parser, Subcommand};
8
9/// This struct uses `clap`'s `Parser` trait to automatically parse command-line arguments.
10#[derive(Parser, Debug)]
11#[command(
12 author = "0xgsvs",
13 version = "0.1.0",
14 about = "A simple command-line task manager written in Rust.",
15 long_about = "Organize your tasks efficiently from the terminal. Add, list, complete, and remove tasks with ease."
16)]
17pub struct Cli {
18 #[command(subcommand)]
19 pub command: Commands,
20}
21
22/// Defines the available commands for the task manager.
23///
24/// Each variant represents a distinct action (e.g., adding a task, listing tasks).
25#[derive(Subcommand, Debug)]
26pub enum Commands {
27 /// Add a new task.
28 ///
29 /// The task description is required. An optional due date can be specified.
30 Add {
31 /// The description of the task to add.
32 description: String,
33 /// Optional due date for the task (format: YYYY-MM-DD).
34 #[arg(short, long, value_parser = parse_due_date)]
35 due: Option<NaiveDate>,
36 },
37 /// List all tasks.
38 ///
39 /// By default, only incomplete tasks are shown. Use the --all flag to see all tasks.
40 List {
41 /// Show all tasks, including completed ones.
42 #[arg(short, long)]
43 all: bool,
44 },
45 /// Mark a task as complete.
46 ///
47 /// Requires the ID of the task to mark.
48 Complete {
49 /// The ID of the task to mark as complete.
50 id: u32,
51 },
52 /// Mark a task as incomplete.
53 ///
54 /// Requires the ID of the task to mark.
55 Undone {
56 /// The ID of the task to mark as incomplete.
57 id: u32,
58 },
59 /// Remove a task.
60 ///
61 /// Requires the ID of the task to remove.
62 Remove {
63 /// The ID of the task to remove.
64 id: u32,
65 },
66 /// Remove all tasks.
67 ///
68 /// Requires confirmation to prevent accidental data loss.
69 Clear {
70 /// Confirm removal of all tasks.
71 #[arg(short, long)]
72 yes: bool,
73 },
74}
75
76/// Helper function to parse a string into a `NaiveDate`.
77///
78/// Used by `clap`'s `value_parser` to validate and convert the `due` argument.
79/// Returns a `Result` indicating success or failure of parsing.
80fn parse_due_date(s: &str) -> Result<NaiveDate, String> {
81 NaiveDate::parse_from_str(s, "%Y-%m-%d")
82 .map_err(|_| format!("Date format must be YYYY-MM-DD. Failed to parse: '{}'", s))
83}