Skip to content

Commit

Permalink
feat: allow custom help subcommand (#19)
Browse files Browse the repository at this point in the history
disable help subcommand with `# @help false`
custom help subcommand message with `# @help Print help information`
  • Loading branch information
sigoden authored Mar 16, 2022
1 parent 819b389 commit 53212a4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Argc generates parsing rules and help documentation based on tags (fields marked
- [@describe](#describe)
- [@version](#version)
- [@author](#author)
- [@help](#help)
- [@cmd](#cmd)
- [@alias](#alias)
- [@option](#option)
Expand Down Expand Up @@ -88,6 +89,19 @@ Define version

Define author

### @help

```sh
@help [false|string]

# @help false
# @help Print help information
```
Customize help subcommand.

1. disable help subcommand with `# @help false`
2. custom help subcommand message with `# @help Print help information`

### @cmd

```sh
Expand Down
13 changes: 13 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct RootData<'a> {
author: Option<&'a str>,
version: Option<&'a str>,
names: HashMap<&'a str, Position>,
help: Option<&'a str>,
main: bool,
}

Expand Down Expand Up @@ -90,6 +91,11 @@ impl<'a> Cmd<'a> {
root_data.author = Some(*value);
}
}
EventData::Help(value) => {
if is_root_scope {
root_data.help = Some(*value);
}
}
EventData::Cmd(value) => {
is_root_scope = false;
let mut cmd = Cmd::default();
Expand Down Expand Up @@ -186,6 +192,13 @@ impl<'a> Cmd<'a> {
if !self.subcommands.is_empty() && !root_data.main {
cmd = cmd.subcommand_required(true).arg_required_else_help(true);
}
if let Some(help) = root_data.help {
if help == "false" {
cmd = cmd.disable_help_subcommand(true);
} else {
cmd = cmd.subcommand(Command::new("help").about(help))
}
}
}
if !self.aliases.is_empty() {
cmd = cmd.visible_aliases(&self.aliases);
Expand Down
11 changes: 10 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum EventData<'a> {
Version(&'a str),
/// Author info
Author(&'a str),
/// Help subcommand
Help(&'a str),
/// Define a subcommand, e.g. `@cmd A sub command`
Cmd(&'a str),
/// Define alias for a subcommand, e.g. `@alias t,tst`
Expand Down Expand Up @@ -106,13 +108,20 @@ fn parse_tag(input: &str) -> nom::IResult<&str, Option<EventData>> {
fn parse_tag_text(input: &str) -> nom::IResult<&str, Option<EventData>> {
map(
pair(
alt((tag("describe"), tag("version"), tag("author"), tag("cmd"))),
alt((
tag("describe"),
tag("version"),
tag("author"),
tag("cmd"),
tag("help"),
)),
parse_tail,
),
|(tag, text)| {
Some(match tag {
"describe" => EventData::Describe(text),
"version" => EventData::Version(text),
"help" => EventData::Help(text),
"author" => EventData::Author(text),
"cmd" => EventData::Cmd(text),
_ => unreachable!(),
Expand Down
60 changes: 60 additions & 0 deletions tests/help_tag_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#[test]
fn test_no_help_tag() {
let script = r###"
# @cmd
cmd() { :; }
"###;
plain!(script, &["prog"], stderr: r#"prog
USAGE:
prog <SUBCOMMAND>
OPTIONS:
-h, --help Print help information
SUBCOMMANDS:
cmd
help Print this message or the help of the given subcommand(s)
"#,);
}

#[test]
fn test_no_help_subcommand() {
let script = r###"
# @help false
# @cmd
cmd() { :; }
"###;
plain!(script, &["prog"], stderr: r#"prog
USAGE:
prog <SUBCOMMAND>
OPTIONS:
-h, --help Print help information
SUBCOMMANDS:
cmd
"#,);
}

#[test]
fn test_custom_help_subcommand_about() {
let script = r###"
# @help Print help information
# @cmd
cmd() { :; }
"###;
plain!(script, &["prog"], stderr: r#"prog
USAGE:
prog <SUBCOMMAND>
OPTIONS:
-h, --help Print help information
SUBCOMMANDS:
cmd
help Print help information
"#,);
}
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
mod macros;
mod escape_test;
mod fail_test;
mod help_tag_test;
mod main_fn_test;
mod spec_test;

0 comments on commit 53212a4

Please sign in to comment.