|
1 |
| -use clap::{Arg, Command}; |
| 1 | +use clap::{Parser, Subcommand, ValueEnum}; |
2 | 2 |
|
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, |
127 | 90 | }
|
0 commit comments