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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use clap::{App, AppSettings, Arg, ArgMatches, ErrorKind, SubCommand};
use std::{env, process};
pub fn parse() -> ArgMatches<'static> {
let footnote = "Cargo commands (-x) are always executed before shell commands (-s).\n\nBy default, your entire project is watched, except for the target/ and .git/ folders, and your .gitignore files are used to filter paths.".to_owned();
#[cfg(windows)] let footnote = format!("{}\n\nOn Windows, patterns given to -i have forward slashes (/) automatically converted to backward ones (\\) to ease command portability.", footnote);
let mut app = App::new(env!("CARGO_PKG_NAME"))
.bin_name("cargo")
.version(env!("CARGO_PKG_VERSION"))
.help_message("")
.version_message("")
.setting(AppSettings::ArgsNegateSubcommands)
.setting(AppSettings::DisableHelpSubcommand)
.setting(AppSettings::DontCollapseArgsInUsage)
.setting(AppSettings::GlobalVersion)
.setting(AppSettings::StrictUtf8)
.setting(AppSettings::SubcommandRequired)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("watch")
.author(env!("CARGO_PKG_HOMEPAGE"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.usage("cargo watch [FLAGS] [OPTIONS]")
.help_message("Display this message")
.version_message("Display version information")
.arg(
Arg::with_name("once")
.long("testing-only--once")
.hidden(true),
)
.arg(
Arg::with_name("clear")
.short("c")
.long("clear")
.help("Clear the screen before each run"),
)
.arg(
Arg::with_name("debug")
.long("debug")
.help("Show debug output"),
)
.arg(
Arg::with_name("ignore-nothing")
.long("ignore-nothing")
.help("Ignore nothing, not even target/ and .git/"),
)
.arg(
Arg::with_name("no-gitignore")
.long("no-gitignore")
.help("Don’t use .gitignore files"),
)
.arg(
Arg::with_name("no-restart")
.long("no-restart")
.help("Don’t restart command while it’s still running"),
)
.arg(
Arg::with_name("packages:all")
.long("all")
.conflicts_with("packages:one")
.hidden(true)
.help("Reserved for workspace support"),
)
.arg(
Arg::with_name("poll")
.long("poll")
.help("Force use of polling for file changes"),
)
.arg(
Arg::with_name("postpone")
.long("postpone")
.help("Postpone first run until a file changes"),
)
.arg(
Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("Suppress output from cargo-watch itself"),
)
.arg(
Arg::with_name("cmd:cargo")
.short("x")
.long("exec")
.takes_value(true)
.value_name("cmd")
.multiple(true)
.empty_values(false)
.min_values(1)
.number_of_values(1)
.help("Cargo command(s) to execute on changes [default: check]"),
)
.arg(
Arg::with_name("cmd:shell")
.short("s")
.long("shell")
.takes_value(true)
.value_name("cmd")
.multiple(true)
.empty_values(false)
.min_values(1)
.number_of_values(1)
.help("Shell command(s) to execute on changes"),
)
.arg(
Arg::with_name("delay")
.short("d")
.long("delay")
.takes_value(true)
.empty_values(false)
.default_value("0.5")
.help("File updates debounce delay in seconds"),
)
.arg(
Arg::with_name("ignore")
.short("i")
.long("ignore")
.takes_value(true)
.value_name("pattern")
.multiple(true)
.empty_values(false)
.min_values(1)
.number_of_values(1)
.help("Ignore a glob/gitignore-style pattern"),
)
.arg(
Arg::with_name("packages:one")
.short("p")
.long("package")
.takes_value(true)
.value_name("spec")
.multiple(true)
.empty_values(false)
.min_values(1)
.hidden(true)
.help("Reserved for workspace support"),
)
.arg(
Arg::with_name("watch")
.short("w")
.long("watch")
.takes_value(true)
.multiple(true)
.empty_values(false)
.min_values(1)
.number_of_values(1)
.default_value(".")
.help("Watch specific file(s) or folder(s)"),
)
.after_help(footnote.as_str()),
);
// Allow invocation of cargo-watch with both `cargo-watch watch ARGS`
// (as invoked by cargo) and `cargo-watch ARGS`.
let mut args: Vec<String> = env::args().collect();
args.insert(1, "watch".into());
let matches = match app.get_matches_from_safe_borrow(args) {
Ok(matches) => matches,
Err(err) => {
match err.kind {
ErrorKind::HelpDisplayed => {
println!("{}", err);
process::exit(0);
}
ErrorKind::VersionDisplayed => {
// Unlike HelpDisplayed, VersionDisplayed emits the output
// by itself (clap-rs/clap#1390). It also does so without a
// trailing newline, so we print one ourselves.
println!();
process::exit(0);
}
_ => app.get_matches(),
}
}
};
matches.subcommand.unwrap().matches
}