Skip to content

Commit

Permalink
feat: Rewrite help for file commands using clap
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed May 15, 2024
1 parent 83a133b commit 31f2df9
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 66 deletions.
46 changes: 28 additions & 18 deletions integration/help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ Available subcommands:
run main --help no-doc

assert_success
assert_output "Usage: main no-doc [args]..."
assert_output "Usage: main no-doc [args]...
Arguments:
[args]...
Options:
-h, --help Print help"
}

@test "help: displays help for a subcommand" {
Expand All @@ -47,26 +53,18 @@ Available subcommands:
run main --help with-help

assert_success
assert_output "Usage: main with-help
assert_output "Command with complete help
Command with complete help
Usage: main with-help
Options:
-h, --help Print help
This is a complete test script with documentation.
The help section can span multiple lines."
}

@test "help: displays summary for subcommand if help is not available" {
fixture "project"

run main --help only-summary

assert_success
assert_output "Usage: main only-summary [args]...
Return with error 4"
}

@test "help: fails gracefully when requested command doesn't exist" {
fixture "project"

Expand Down Expand Up @@ -101,9 +99,15 @@ Available subcommands:
run main --help directory with-help

assert_success
assert_output "Usage: main directory with-help [args]...
assert_output "Help 2
Usage: main directory with-help [args]...
Help 2
Arguments:
[args]...
Options:
-h, --help Print help
This is a complete test script with documentation.
Expand Down Expand Up @@ -134,9 +138,15 @@ Available subcommands:
run main --help directory double with-help

assert_success
assert_output "Usage: main directory double with-help [args]...
assert_output "Help 3
Usage: main directory double with-help [args]...
Arguments:
[args]...
Help 3
Options:
-h, --help Print help
This is a complete test script with documentation.
Expand Down
51 changes: 49 additions & 2 deletions src/commands/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> DirectoryCommand<'a> {
}

if let Some(description) = docs.description {
command = command.long_about(description);
command = command.after_help(description);
}
}

Expand Down Expand Up @@ -58,7 +58,54 @@ impl<'a> Command for DirectoryCommand<'a> {
}

fn description(&self) -> String {
self.usage.command().get_long_about().map(|s| s.ansi().to_string()).unwrap_or_default()
self.usage.command().get_after_help().map(|s| s.ansi().to_string()).unwrap_or_default()
}

fn help(&self) -> String {
let mut help = String::new();

let usage = self.usage();
if !usage.is_empty() {
help.push_str(&usage);
help.push_str("\n\n");
}

let summary = self.summary();
if !summary.is_empty() {
help.push_str(&summary);
help.push_str("\n\n");
}

let description = self.description();
if !description.is_empty() {
help.push_str(&description);
help.push_str("\n\n");
}

let subcommands = self.subcommands();
if !subcommands.is_empty() {
help.push_str("Available subcommands:\n");

let max_width = subcommands
.iter()
.map(|subcommand| subcommand.name())
.map(|name| name.len())
.max()
.unwrap();

let width = max_width + 4;

for subcommand in subcommands {
help.push_str(&format!(
" {:width$}{}\n",
subcommand.name(),
subcommand.summary(),
width = width
));
}
}

help
}

fn subcommands(&self) -> Vec<Box<dyn Command + '_>> {
Expand Down
6 changes: 5 additions & 1 deletion src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ impl<'a> Command for FileCommand<'a> {
}

fn description(&self) -> String {
self.usage.command().get_long_about().map(|s| s.ansi().to_string()).unwrap_or_default()
self.usage.command().get_after_help().map(|s| s.ansi().to_string()).unwrap_or_default()
}

fn help(&self) -> String {
self.usage.command().render_help().ansi().to_string()
}

fn subcommands(&self) -> Vec<Box<dyn Command + '_>> {
Expand Down
42 changes: 1 addition & 41 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,7 @@ pub trait Command {
fn subcommands(&self) -> Vec<Box<dyn Command + '_>>;
fn completions(&self) -> Result<i32>;
fn invoke(&self) -> Result<i32>;

fn help(&self) -> String {
let mut help = String::new();

let usage = self.usage();
if !usage.is_empty() {
help.push_str(&usage);
help.push_str("\n\n");
}

let summary = self.summary();
if !summary.is_empty() {
help.push_str(&summary);
help.push_str("\n\n");
}

let description = self.description();
if !description.is_empty() {
help.push_str(&description);
}

let subcommands = self.subcommands();
if !subcommands.is_empty() {
help.push_str("\n\nAvailable subcommands:\n");

let max_width = subcommands
.iter()
.map(|subcommand| subcommand.name())
.map(|name| name.len())
.max()
.unwrap();

let width = max_width + 4;

for subcommand in subcommands {
help.push_str(&format!(" {:width$}{}\n", subcommand.name(), subcommand.summary(), width = width));
}
}

help
}
fn help(&self) -> String;
}

pub fn subcommand(config: &Config, mut cliargs: Vec<String>) -> Result<Box<dyn Command + '_>> {
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn extract_docs(path: &Path) -> Docs {

if let Some(caps) = SUMMARY_RE.captures(&line) {
if let Some(m) = caps.get(1) {
summary = Some(m.as_str().to_owned());
summary = Some(m.as_str().trim().to_owned());
continue;
}
}
Expand All @@ -71,7 +71,7 @@ pub fn extract_docs(path: &Path) -> Docs {

if let Some(caps) = EXTENDED_RE.captures(&line) {
if let Some(m) = caps.get(1) {
description.push(m.as_str().to_owned());
description.push(m.as_str().trim().to_owned());
mode = Mode::Description;
continue;
}
Expand All @@ -86,7 +86,7 @@ pub fn extract_docs(path: &Path) -> Docs {

if let Some(caps) = EXTENDED_RE.captures(&line) {
if let Some(m) = caps.get(1) {
description.push(m.as_str().to_owned());
description.push(m.as_str().trim().to_owned());
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn extract_usage(config: &Config, path: &Path, cmd: &str) -> Result<Usage> {
}

if let Some(description) = docs.description {
command = command.long_about(description);
command = command.after_help(description);
}

if let Some(line) = docs.usage {
Expand Down

0 comments on commit 31f2df9

Please sign in to comment.