Skip to content

Commit 0dd169b

Browse files
authored
Merge pull request #3328 from epage/panic
Clarify some panics
2 parents ddad3a7 + afb2a3d commit 0dd169b

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

clap_complete/src/generator/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use clap::App;
1414
pub trait Generator {
1515
/// Returns the file name that is created when this generator is called during compile time.
1616
///
17+
/// # Panics
18+
///
19+
/// May panic when called outside of the context of [`generate`] or [`generate_to`]
20+
///
1721
/// # Examples
1822
///
1923
/// ```
@@ -34,6 +38,10 @@ pub trait Generator {
3438

3539
/// Generates output out of [`clap::App`](App).
3640
///
41+
/// # Panics
42+
///
43+
/// May panic when called outside of the context of [`generate`] or [`generate_to`]
44+
///
3745
/// # Examples
3846
///
3947
/// The following example generator displays the [`clap::App`](App)

clap_complete/src/shells/bash.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ impl Generator for Bash {
1414
}
1515

1616
fn generate(&self, app: &App, buf: &mut dyn Write) {
17-
let bin_name = app.get_bin_name().unwrap();
17+
let bin_name = app
18+
.get_bin_name()
19+
.expect("crate::generate should have set the bin_name");
1820

1921
w!(
2022
buf,

clap_complete/src/shells/elvish.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ impl Generator for Elvish {
1515
}
1616

1717
fn generate(&self, app: &App, buf: &mut dyn Write) {
18-
let bin_name = app.get_bin_name().unwrap();
18+
let bin_name = app
19+
.get_bin_name()
20+
.expect("crate::generate should have set the bin_name");
1921

2022
let mut names = vec![];
2123
let subcommands_cases = generate_inner(app, "", &mut names);

clap_complete/src/shells/fish.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ impl Generator for Fish {
1616
}
1717

1818
fn generate(&self, app: &App, buf: &mut dyn Write) {
19-
let command = app.get_bin_name().unwrap();
20-
let mut buffer = String::new();
19+
let bin_name = app
20+
.get_bin_name()
21+
.expect("crate::generate should have set the bin_name");
2122

22-
gen_fish_inner(command, &[], app, &mut buffer);
23+
let mut buffer = String::new();
24+
gen_fish_inner(bin_name, &[], app, &mut buffer);
2325
w!(buf, buffer.as_bytes());
2426
}
2527
}

clap_complete/src/shells/powershell.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ impl Generator for PowerShell {
1515
}
1616

1717
fn generate(&self, app: &App, buf: &mut dyn Write) {
18-
let bin_name = app.get_bin_name().unwrap();
18+
let bin_name = app
19+
.get_bin_name()
20+
.expect("crate::generate should have set the bin_name");
1921

2022
let mut names = vec![];
2123
let subcommands_cases = generate_inner(app, "", &mut names);

clap_complete/src/shells/zsh.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ impl Generator for Zsh {
1515
}
1616

1717
fn generate(&self, app: &App, buf: &mut dyn Write) {
18+
let bin_name = app
19+
.get_bin_name()
20+
.expect("crate::generate should have set the bin_name");
21+
1822
w!(
1923
buf,
2024
format!(
@@ -41,7 +45,7 @@ _{name}() {{
4145
4246
_{name} \"$@\"
4347
",
44-
name = app.get_bin_name().unwrap(),
48+
name = bin_name,
4549
initial_args = get_args_of(app, None),
4650
subcommands = get_subcommands_of(app),
4751
subcommand_details = subcommand_details(app)
@@ -81,7 +85,9 @@ _{name} \"$@\"
8185
fn subcommand_details(p: &App) -> String {
8286
debug!("subcommand_details");
8387

84-
let name = p.get_bin_name().unwrap();
88+
let bin_name = p
89+
.get_bin_name()
90+
.expect("crate::generate should have set the bin_name");
8591

8692
let mut ret = vec![];
8793

@@ -93,8 +99,8 @@ _{bin_name_underscore}_commands() {{
9399
local commands; commands=({subcommands_and_args})
94100
_describe -t commands '{bin_name} commands' commands \"$@\"
95101
}}",
96-
bin_name_underscore = name.replace(' ', "__"),
97-
bin_name = name,
102+
bin_name_underscore = bin_name.replace(' ', "__"),
103+
bin_name = bin_name,
98104
subcommands_and_args = subcommands_of(p)
99105
);
100106
ret.push(parent_text);
@@ -247,6 +253,10 @@ fn get_subcommands_of(parent: &App) -> String {
247253
all_subcommands.push(segments.join("\n"));
248254
}
249255

256+
let parent_bin_name = parent
257+
.get_bin_name()
258+
.expect("crate::generate should have set the bin_name");
259+
250260
format!(
251261
"
252262
case $state in
@@ -260,7 +270,7 @@ fn get_subcommands_of(parent: &App) -> String {
260270
;;
261271
esac",
262272
name = parent.get_name(),
263-
name_hyphen = parent.get_bin_name().unwrap().replace(' ', "-"),
273+
name_hyphen = parent_bin_name.replace(' ', "-"),
264274
subcommands = all_subcommands.join("\n"),
265275
pos = parent.get_positionals().count() + 1
266276
)
@@ -327,9 +337,12 @@ fn get_args_of(parent: &App, p_global: Option<&App>) -> String {
327337
}
328338

329339
if parent.has_subcommands() {
340+
let parent_bin_name = parent
341+
.get_bin_name()
342+
.expect("crate::generate should have set the bin_name");
330343
let subcommand_bin_name = format!(
331344
"\":: :_{name}_commands\" \\",
332-
name = parent.get_bin_name().as_ref().unwrap().replace(' ', "__")
345+
name = parent_bin_name.replace(' ', "__")
333346
);
334347
segments.push(subcommand_bin_name);
335348

0 commit comments

Comments
 (0)