-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
74 lines (65 loc) · 1.92 KB
/
Copy pathmain.rs
File metadata and controls
74 lines (65 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::env;
use anyhow::Result;
use clap::{ArgAction, Parser, Subcommand};
use simplelog::*;
mod fmt;
/// CSV utility tools
#[derive(Debug, Parser)]
#[command(name = "csv")]
#[command(about = "A collection of CSV tools", long_about = None, version)]
#[command(propagate_version = true)]
struct Cli {
/// Turn debugging information on
#[arg(long, action=ArgAction::SetTrue)]
#[clap(global = true, long)]
debug: bool,
/// Choose the operation you want to perform. The default is "fmt"
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Debug, Subcommand)]
enum Commands {
// Format the file or STDIN by justifying all the columns. This is the default option which
// will be run when no subcommands is given
#[command(name = "fmt")]
Fmt(fmt::FmtArgs),
}
fn help_and_exit() -> ! {
use clap::CommandFactory;
Cli::command().print_help().expect("Unable to write help");
std::process::exit(1)
}
fn main() -> Result<()> {
let mut env_args: Vec<String> = env::args().collect();
if env_args.len() < 2
|| (env_args.len() == 2 && ["-h", "--help"].contains(&env_args[1].as_str()))
{
help_and_exit()
}
// No subcommand given, or the subommand is one of the known ones
if env_args[1].starts_with("-") || !["fmt", "help"].contains(&env_args[1].as_str()) {
env_args.insert(1, "fmt".to_string());
}
let args = Cli::parse_from(env_args);
if args.debug {
TermLogger::init(
LevelFilter::Debug,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
)?
} else {
TermLogger::init(
LevelFilter::Warn,
Config::default(),
TerminalMode::Mixed,
ColorChoice::Auto,
)?
}
debug!("{:#?}", &args);
match args.command {
Some(Commands::Fmt(fmt_args)) => fmt::run(fmt_args)?,
None => {}
}
Ok(())
}