Skip to content

Commit 95db925

Browse files
committed
Generate completion-files during build
1 parent 9972142 commit 95db925

File tree

4 files changed

+107
-84
lines changed

4 files changed

+107
-84
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ readme = "README.md"
99
license = "MIT"
1010

1111
edition = "2018"
12+
build = "build.rs"
1213

1314
[dependencies]
1415
clap = "2.33.0"
1516
indoc = "0.3.1"
1617
nix = "0.13.0"
18+
19+
[build-dependencies]
20+
clap = "2.33.0"
21+
indoc = "0.3.1"

build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use std::env;
2+
use clap::Shell;
3+
4+
include!("src/cli.rs");
5+
6+
7+
fn main() {
8+
let outdir = env::var_os("CARGO_TARGET_DIR")
9+
.or_else(|| env::var_os("OUT_DIR"))
10+
.unwrap();
11+
12+
let mut app = app();
13+
app.gen_completions("surface", Shell::Bash, &outdir);
14+
app.gen_completions("surface", Shell::Zsh, &outdir);
15+
app.gen_completions("surface", Shell::Fish, &outdir);
16+
}

src/cli.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use clap;
2+
use clap::{App, AppSettings, Arg, SubCommand};
3+
use indoc::indoc;
4+
5+
6+
pub fn app() -> App<'static, 'static> {
7+
let settings = [
8+
AppSettings::InferSubcommands,
9+
AppSettings::VersionlessSubcommands,
10+
];
11+
12+
let status = SubCommand::with_name("status")
13+
.about("Query the current system status");
14+
15+
let perf = SubCommand::with_name("performance")
16+
.about("Control or query the current performance-mode")
17+
.long_about(indoc!("
18+
Control or query the current performance-mode
19+
20+
Supported performance-mode values are:
21+
22+
Value Name
23+
---------------------------
24+
1 Normal (Default)
25+
2 Battery Saver
26+
3 Better Performance
27+
4 Best Performance
28+
"))
29+
.setting(AppSettings::SubcommandRequiredElseHelp)
30+
.subcommand(SubCommand::with_name("set")
31+
.about("Set the current performance-mode")
32+
.arg(Arg::with_name("mode")
33+
.possible_values(&["1", "2", "3", "4"])
34+
.required(true)
35+
.index(1)))
36+
.subcommand(SubCommand::with_name("get")
37+
.about("Get the current performance-mode"));
38+
39+
let dgpu = SubCommand::with_name("dgpu")
40+
.about("Control or query the dGPU power state")
41+
.long_about(indoc!("
42+
Control or query the dGPU power state
43+
44+
Supported values are: 'on', 'off'.
45+
"))
46+
.setting(AppSettings::SubcommandRequiredElseHelp)
47+
.subcommand(SubCommand::with_name("set")
48+
.about("Set the current dGPU power state")
49+
.arg(Arg::with_name("state")
50+
.help("The power-state to be set")
51+
.possible_values(&["on", "off", "1", "0"])
52+
.required(true)
53+
.index(1)))
54+
.subcommand(SubCommand::with_name("get")
55+
.about("Get the current dGPU power state"));
56+
57+
let latch = SubCommand::with_name("latch")
58+
.about("Control the latch/dtx-system on the Surface Book 2")
59+
.setting(AppSettings::SubcommandRequiredElseHelp)
60+
.subcommand(SubCommand::with_name("lock")
61+
.about("Lock the latch")
62+
.display_order(1))
63+
.subcommand(SubCommand::with_name("unlock")
64+
.about("Unlock the latch")
65+
.display_order(2))
66+
.subcommand(SubCommand::with_name("request")
67+
.about("Request latch-open or abort if already in progress")
68+
.display_order(3))
69+
.subcommand(SubCommand::with_name("get-opmode")
70+
.about("Query the current device operation mode")
71+
.display_order(4));
72+
73+
App::new(clap::crate_name!())
74+
.version(clap::crate_version!())
75+
.author(clap::crate_authors!("\n"))
76+
.about("Control various aspects of Microsoft Surface devices")
77+
.setting(AppSettings::SubcommandRequiredElseHelp)
78+
.global_settings(&settings)
79+
.subcommand(status)
80+
.subcommand(perf)
81+
.subcommand(dgpu)
82+
.subcommand(latch)
83+
}

src/main.rs

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,14 @@
11
use std::io::Result;
2-
use clap;
3-
use indoc::indoc;
2+
3+
mod cli;
44

55
mod dgpu;
66
mod perf;
77
mod latch;
88

99

10-
fn app() -> clap::App<'static, 'static> {
11-
use clap::{App, AppSettings, Arg, SubCommand};
12-
13-
let settings = [
14-
AppSettings::InferSubcommands,
15-
AppSettings::VersionlessSubcommands,
16-
];
17-
18-
let status = SubCommand::with_name("status")
19-
.about("Query the current system status");
20-
21-
let perf = SubCommand::with_name("performance")
22-
.about("Control or query the current performance-mode")
23-
.long_about(indoc!("
24-
Control or query the current performance-mode
25-
26-
Supported performance-mode values are:
27-
28-
Value Name
29-
---------------------------
30-
1 Normal (Default)
31-
2 Battery Saver
32-
3 Better Performance
33-
4 Best Performance
34-
"))
35-
.setting(AppSettings::SubcommandRequiredElseHelp)
36-
.subcommand(SubCommand::with_name("set")
37-
.about("Set the current performance-mode")
38-
.arg(Arg::with_name("mode")
39-
.possible_values(&["1", "2", "3", "4"])
40-
.required(true)
41-
.index(1)))
42-
.subcommand(SubCommand::with_name("get")
43-
.about("Get the current performance-mode"));
44-
45-
let dgpu = SubCommand::with_name("dgpu")
46-
.about("Control or query the dGPU power state")
47-
.long_about(indoc!("
48-
Control or query the dGPU power state
49-
50-
Supported values are: 'on', 'off'.
51-
"))
52-
.setting(AppSettings::SubcommandRequiredElseHelp)
53-
.subcommand(SubCommand::with_name("set")
54-
.about("Set the current dGPU power state")
55-
.arg(Arg::with_name("state")
56-
.help("The power-state to be set")
57-
.possible_values(&["on", "off", "1", "0"])
58-
.required(true)
59-
.index(1)))
60-
.subcommand(SubCommand::with_name("get")
61-
.about("Get the current dGPU power state"));
62-
63-
let latch = SubCommand::with_name("latch")
64-
.about("Control the latch/dtx-system on the Surface Book 2")
65-
.setting(AppSettings::SubcommandRequiredElseHelp)
66-
.subcommand(SubCommand::with_name("lock")
67-
.about("Lock the latch")
68-
.display_order(1))
69-
.subcommand(SubCommand::with_name("unlock")
70-
.about("Unlock the latch")
71-
.display_order(2))
72-
.subcommand(SubCommand::with_name("request")
73-
.about("Request latch-open or abort if already in progress")
74-
.display_order(3))
75-
.subcommand(SubCommand::with_name("get-opmode")
76-
.about("Query the current device operation mode")
77-
.display_order(4));
78-
79-
App::new(clap::crate_name!())
80-
.version(clap::crate_version!())
81-
.author(clap::crate_authors!("\n"))
82-
.about("Control various aspects of Microsoft Surface devices")
83-
.setting(AppSettings::SubcommandRequiredElseHelp)
84-
.global_settings(&settings)
85-
.subcommand(status)
86-
.subcommand(perf)
87-
.subcommand(dgpu)
88-
.subcommand(latch)
89-
}
90-
9110
fn main() {
92-
let matches = app().get_matches();
11+
let matches = cli::app().get_matches();
9312

9413
let result = match matches.subcommand() {
9514
("status", Some(m)) => cmd_status(m),

0 commit comments

Comments
 (0)