hawk_data/
arg.rs

1use std::path::PathBuf;
2
3use clap::Parser;
4
5use crate::Error;
6
7/// hawk - Modern data analysis tool for structured data (JSON, YAML, CSV)
8///
9/// hawk combines the simplicity of awk with the power of pandas for data exploration.
10/// Perfect for analyzing JSON APIs, YAML configs, and CSV datasets.
11#[derive(Debug, Parser)]
12#[command(name = "hawk")]
13#[command(version = "0.1.0")]
14#[command(about = "Modern data analysis tool for structured data")]
15#[command(long_about = "
16hawk is a command-line data analysis tool that brings pandas-like functionality
17to your terminal. It supports JSON, YAML, and CSV formats with automatic detection,
18powerful filtering, grouping, and aggregation capabilities.
19
20EXAMPLES:
21    # Basic field access
22    hawk '.users[0].name' data.json
23    hawk '.users.name' data.csv
24
25    # Filtering and aggregation
26    hawk '.users[] | select(.age > 30)' data.yaml
27    hawk '.sales | group_by(.region) | avg(.amount)' sales.csv
28
29    # Data exploration
30    hawk '. | info' data.json
31    hawk '.users | count' data.csv
32
33SUPPORTED FORMATS:
34    JSON, YAML, CSV (automatically detected)
35
36QUERY SYNTAX:
37    .field                    - Access field
38    .array[0]                 - Access array element
39    .array[]                  - Access all array elements
40    . | select(.field > 10)   - Filter data
41    . | group_by(.category)   - Group data
42    . | count/sum/avg/min/max - Aggregate functions
43")]
44pub struct Args {
45    /// JSONPath-style query to execute
46    ///
47    /// Examples:
48    ///   .users[0].name              - Get first user's name
49    ///   .users | select(.age > 30)  - Filter users by age
50    ///   . | group_by(.department)   - Group by department
51    pub query: String,
52
53    /// Input file path (JSON, YAML, or CSV)
54    ///
55    /// If not provided, reads from stdin.
56    /// File format is automatically detected.
57    pub path: Option<PathBuf>,
58
59    /// Output format
60    ///
61    /// auto: Smart detection (table for arrays, list for values, json for complex)
62    /// table: Force tabular output
63    /// json: Force JSON output
64    /// list: Force list output
65    #[arg(long, default_value = "auto")]
66    #[arg(value_parser = ["auto", "table", "json", "list"])]
67    pub format: String,
68}
69
70#[derive(Debug, Clone)]
71pub enum OutputFormat {
72    Auto,
73    Json,
74    Table,
75    List,
76    // Csv, // 将来追加
77}
78
79impl std::str::FromStr for OutputFormat {
80    type Err = Error;
81
82    fn from_str(s: &str) -> Result<Self, Self::Err> {
83        match s.to_lowercase().as_str() {
84            "auto" => Ok(OutputFormat::Auto),
85            "json" => Ok(OutputFormat::Json),
86            "table" => Ok(OutputFormat::Table),
87            "list" => Ok(OutputFormat::List),
88            // "csv" => Ok(OutputFormat::Csv),
89            _ => Err(Error::InvalidFormat(format!(
90                "Invalid format: {}. Valid options: auto, json, table, list",
91                s
92            ))),
93        }
94    }
95}