Skip to content

Commit 8368b06

Browse files
committed
Use clap derive API
1 parent fca8ea1 commit 8368b06

File tree

7 files changed

+199
-204
lines changed

7 files changed

+199
-204
lines changed

Cargo.lock

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rust-version = "1.58.1"
1616

1717
[dependencies]
1818
anyhow = "1.0.56"
19-
clap = "4.0.7"
19+
clap = { version = "4.0.7", features = ["derive"] }
2020
clap_complete = "4.0.2"
2121
crossterm = "0.25.0"
2222
indicatif = "0.17.1"
@@ -41,5 +41,5 @@ assert_cmd = "2.0.4"
4141
predicates = "2.1.1"
4242

4343
[build-dependencies]
44-
clap = "4.0.7"
44+
clap = { version = "4.0.7", features = ["derive"] }
4545
clap_complete = "4.0.2"

build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use clap::CommandFactory;
12
use clap_complete::Shell;
23

34
include!("src/cli.rs");
45

56
fn main() {
67
let outdir = std::env::var("OUT_DIR").unwrap();
7-
let mut app = build_cli();
8+
let mut app = Cli::command();
89
clap_complete::generate_to(Shell::Bash, &mut app, "tmc", &outdir).unwrap();
910
clap_complete::generate_to(Shell::PowerShell, &mut app, "tmc", &outdir).unwrap();
1011
clap_complete::generate_to(Shell::Zsh, &mut app, "tmc", &outdir).unwrap();

src/cli.rs

+88-125
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,90 @@
1-
use clap::{Arg, Command};
1+
use clap::{Parser, Subcommand, ValueEnum};
22

3-
pub fn build_cli() -> Command {
4-
Command::new(env!("CARGO_PKG_NAME"))
5-
.version(env!("CARGO_PKG_VERSION"))
6-
.about(env!("CARGO_PKG_DESCRIPTION"))
7-
.arg_required_else_help(true)
8-
.subcommand(Command::new("courses").about("List the available courses"))
9-
.subcommand(
10-
Command::new("download")
11-
.about("Downloads course exercises")
12-
.arg(
13-
Arg::new("course")
14-
.short('c')
15-
.long("course")
16-
.value_name("course name")
17-
.required(false),
18-
)
19-
.arg(
20-
Arg::new("currentdir")
21-
.short('d')
22-
.long("currentdir")
23-
.required(false),
24-
),
25-
)
26-
.subcommand(
27-
Command::new("exercises")
28-
.about("List the exercises for a specific course")
29-
.arg(Arg::new("course").value_name("course").required(true)),
30-
)
31-
.subcommand(
32-
Command::new("login").about("Login to TMC server").arg(
33-
Arg::new("non-interactive")
34-
.short('n')
35-
.help("Initiates the non-interactive mode.")
36-
.long("non-interactive"),
37-
),
38-
)
39-
.subcommand(Command::new("logout").about("Logout from TMC server"))
40-
.subcommand(
41-
Command::new("organization")
42-
.about("Change organization")
43-
.arg(
44-
Arg::new("non-interactive")
45-
.short('n')
46-
.help("Initiates the non-interactive mode.")
47-
.long("non-interactive"),
48-
),
49-
)
50-
.subcommand(
51-
Command::new("paste")
52-
.about("Submit exercise to TMC pastebin")
53-
.arg(Arg::new("exercise").value_name("exercise").required(false)),
54-
)
55-
.subcommand(
56-
Command::new("submit")
57-
.about("Submit exercises to TMC server")
58-
.arg(Arg::new("exercise").value_name("exercise").required(false)),
59-
)
60-
.subcommand(
61-
Command::new("test")
62-
.about("Run local exercise tests")
63-
.arg(Arg::new("exercise").value_name("exercise").required(false)),
64-
)
65-
.subcommand(
66-
Command::new("fetchupdate")
67-
.hide(true)
68-
.about("Finishes the autoupdater. Administator rights needed."),
69-
)
70-
.subcommand(
71-
Command::new("cleartemp")
72-
.hide(true)
73-
.about("Removes tempfiles. Administator rights needed."),
74-
)
75-
.subcommand(
76-
Command::new("elevateddownload")
77-
.hide(true)
78-
.about("Downloads course from the tempfile. Administator rights needed."),
79-
)
80-
.subcommand(
81-
Command::new("elevatedupdate")
82-
.hide(true)
83-
.about("updates course from the tempfile. Administator rights needed."),
84-
)
85-
.subcommand(
86-
Command::new("update")
87-
.about("Updates course exercises")
88-
.arg(
89-
Arg::new("currentdir")
90-
.short('d')
91-
.long("currentdir")
92-
.required(false),
93-
),
94-
)
95-
.arg(
96-
Arg::new("no-update")
97-
.short('d')
98-
.long("no-update")
99-
.help("Disable auto update temporarily")
100-
.hide(!cfg!(windows)), // hide on non-windows platforms
101-
)
102-
.arg(
103-
Arg::new("force-update")
104-
.short('u')
105-
.long("force-update")
106-
.help("Force auto update to run")
107-
.hide(!cfg!(windows)), // hide on non-windows platforms
108-
)
109-
.arg(
110-
Arg::new("testmode")
111-
.long("testmode")
112-
.help("Only for internal testing, disables server connection")
113-
.hide(true),
114-
)
115-
.subcommand(
116-
Command::new("generate-completions")
117-
.override_usage(
118-
"tmc generate_completions --[your shell] > /path/to/your/completions/folder",
119-
)
120-
.about("Generate completion scripts for command line usage.")
121-
.disable_version_flag(true)
122-
.hide(true)
123-
.arg(Arg::new("bash").short('b').long("bash"))
124-
.arg(Arg::new("zsh").short('z').long("zsh"))
125-
.arg(Arg::new("powershell").short('p').long("powershell")),
126-
)
3+
#[derive(Parser, Debug)]
4+
#[command(
5+
name = env!("CARGO_PKG_NAME"),
6+
version,
7+
author,
8+
about,
9+
subcommand_required(true),
10+
arg_required_else_help(true)
11+
)]
12+
pub struct Cli {
13+
/// Disable auto update temporarily
14+
#[arg(short = 'd', long, hide = !cfg!(windows))]
15+
pub no_update: bool,
16+
/// Force auto update to run
17+
#[arg(short = 'u', long, hide = !cfg!(windows))]
18+
pub force_update: bool,
19+
/// Only for internal testing, disables server connection
20+
#[arg(long, hide = true)]
21+
pub testmode: bool,
22+
#[command(subcommand)]
23+
pub subcommand: Command,
24+
}
25+
26+
#[derive(Subcommand, Debug)]
27+
pub enum Command {
28+
/// List the available courses
29+
Courses,
30+
/// Downloads course exercises
31+
Download {
32+
#[arg(short, long, value_name = "course name")]
33+
course: Option<String>,
34+
#[arg(short = 'd', long)]
35+
currentdir: bool,
36+
},
37+
/// List the exercises for a specific course
38+
Exercises { course: String },
39+
/// Login to TMC server
40+
Login {
41+
/// Initiates the non-interactive mode.
42+
#[arg(short, long)]
43+
non_interactive: bool,
44+
},
45+
/// Logout from TMC server
46+
Logout,
47+
/// Change organization
48+
Organization {
49+
/// Initiates the non-interactive mode.
50+
#[arg(short, long)]
51+
non_interactive: bool,
52+
},
53+
/// Submit exercise to TMC pastebin
54+
Paste { exercise: Option<String> },
55+
/// Submit exercises to TMC server
56+
Submit { exercise: Option<String> },
57+
/// Run local exercise tests
58+
Test { exercise: Option<String> },
59+
/// Finishes the autoupdater. Administator rights needed.
60+
#[clap(hide = true)]
61+
Fetchupdate,
62+
/// Removes tempfiles. Administator rights needed.
63+
#[clap(hide = true)]
64+
Cleartemp,
65+
/// Downloads course from the tempfile. Administator rights needed.
66+
#[clap(hide = true)]
67+
Elevateddownload,
68+
/// updates course from the tempfile. Administator rights needed.
69+
#[clap(hide = true)]
70+
Elevatedupdate,
71+
/// Updates course exercises
72+
Update {
73+
#[arg(short = 'd', long)]
74+
currentdir: bool,
75+
},
76+
/// Generate completion scripts for command line usage.
77+
#[clap(
78+
hide = true,
79+
disable_version_flag = true,
80+
override_usage = "tmc generate_completions --[your shell] > /path/to/your/completions/folder"
81+
)]
82+
GenerateCompletions { shell: ShellArg },
83+
}
84+
85+
#[derive(Debug, Clone, Copy, ValueEnum)]
86+
pub enum ShellArg {
87+
Bash,
88+
Zsh,
89+
Powershell,
12790
}

0 commit comments

Comments
 (0)