Fix parsing usage for commands defined in CRLF (windows) files

Fixes nushell/nushell#13207

Before this commit, if the command was defined in a file that uses CR+LF
line endings, nu would fail to split the docblock into `usage` and
`extra_usage` (this also affected `std` that is baked into nu binary).

Now we first try to split the docblock by CRLF, and then by LF, thus
supporting both line ending styles in an OS-agnostic way.
This commit is contained in:
Bruce Weirdan 2024-06-23 23:27:23 +02:00
parent 9b7f899410
commit 932d434195
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 19 additions and 1 deletions

View File

@ -81,7 +81,9 @@ pub(super) fn build_usage(comment_lines: &[&[u8]]) -> (String, String) {
usage.push_str(&comment_line); usage.push_str(&comment_line);
} }
if let Some((brief_usage, extra_usage)) = usage.split_once("\n\n") { if let Some((brief_usage, extra_usage)) = usage.split_once("\r\n\r\n") {
(brief_usage.to_string(), extra_usage.to_string())
} else if let Some((brief_usage, extra_usage)) = usage.split_once("\n\n") {
(brief_usage.to_string(), extra_usage.to_string()) (brief_usage.to_string(), extra_usage.to_string())
} else { } else {
(usage, String::default()) (usage, String::default())

View File

@ -261,6 +261,22 @@ fn commands_have_usage() -> TestResult {
) )
} }
#[test]
fn commands_from_crlf_source_have_short_usage() -> TestResult {
run_test_contains(
"# This is a test\r\n#\r\n# To see if I have cool usage\r\ndef foo [] {}\r\nscope commands | where name == foo | get usage.0",
"This is a test",
)
}
#[test]
fn commands_from_crlf_source_have_extra_usage() -> TestResult {
run_test_contains(
"# This is a test\r\n#\r\n# To see if I have cool usage\r\ndef foo [] {}\r\nscope commands | where name == foo | get extra_usage.0",
"To see if I have cool usage",
)
}
#[test] #[test]
fn equals_separates_long_flag() -> TestResult { fn equals_separates_long_flag() -> TestResult {
run_test( run_test(