Skip to content

Commit

Permalink
fix: read choices from clap args (#136)
Browse files Browse the repository at this point in the history
Fixes #27
  • Loading branch information
jdx authored Oct 25, 2024
1 parent 37f5b53 commit 6a2fb88
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 165 deletions.
6 changes: 6 additions & 0 deletions cli/tests/complete_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ fn complete_word_kitchen_sink() {
assert_cmd("kitchen-sink.usage.kdl", &["--", "--shell", ""]).stdout("bash\nzsh\nfish\n");
}

#[test]
fn complete_word_choices() {
assert_cmd("mise.usage.kdl", &["--", "env", "--shell", ""])
.stdout("bash\nfish\nnu\nxonsh\nzsh\n");
}

#[test]
fn complete_word_shebang() {
assert_cmd("example.sh", &["--", "-"]).stdout("--bar\n--defaulted\n--foo\n");
Expand Down
618 changes: 487 additions & 131 deletions examples/mise.usage.kdl

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions lib/src/docs/markdown/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests {
#[test]
fn test_render_markdown_cmd() {
let ctx = MarkdownRenderer::new(&SPEC_KITCHEN_SINK).with_multi(true);
assert_snapshot!(ctx.render_cmd(&SPEC_KITCHEN_SINK.cmd).unwrap(), @r#####"
assert_snapshot!(ctx.render_cmd(&SPEC_KITCHEN_SINK.cmd).unwrap(), @r####"
# `mycli`
**Usage**: `mycli [FLAGS] <ARGS>… <SUBCOMMAND>`
Expand All @@ -34,15 +34,13 @@ mod tests {
arg2 description
#### Choices
**Choices:**
- `choice1`
- `choice2`
- `choice3`
#### Default
`default value`
**Default:** `default value`
### `<arg3>`
Expand All @@ -66,7 +64,7 @@ mod tests {
### `--shell <shell>`
#### Choices
**Choices:**
- `bash`
- `zsh`
Expand All @@ -75,6 +73,6 @@ mod tests {
## Subcommands
* [`mycli plugin <SUBCOMMAND>`](/plugin.md)
"#####);
"####);
}
}
8 changes: 3 additions & 5 deletions lib/src/docs/markdown/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ mod tests {
arg2 description
#### Choices
**Choices:**
- `choice1`
- `choice2`
- `choice3`
#### Default
`default value`
**Default:** `default value`
### `<arg3>`
Expand All @@ -72,7 +70,7 @@ mod tests {
### `--shell <shell>`
#### Choices
**Choices:**
- `bash`
- `zsh`
Expand Down
10 changes: 3 additions & 7 deletions lib/src/docs/markdown/templates/arg_template.md.tera
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
{%- endif %}
{%- if arg.choices %}

{{ "#" | repeat(count=header_level) }}### Choices
**Choices:**
{% for choice in arg.choices.choices %}
- `{{ choice }}`
{%- endfor %}
{%- endif %}
{%- if arg.default %}

{{ "#" | repeat(count=header_level) }}### Default

`{{ arg.default }}`
**Default:** `{{ arg.default }}`
{%- endif %}
{%- if arg.env %}

{{ "#" | repeat(count=header_level) }}### Environment Variable

`{{ arg.env }}`
**Environment Variable:** `{{ arg.env }}`
{%- endif -%}
10 changes: 3 additions & 7 deletions lib/src/docs/markdown/templates/flag_template.md.tera
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
{%- endif %}
{%- if flag.arg.choices %}

{{ "#" | repeat(count=header_level) }}### Choices
**Choices:**
{% for choice in flag.arg.choices.choices %}
- `{{ choice }}`
{%- endfor %}
{%- endif %}
{%- if flag.default %}

{{ "#" | repeat(count=header_level) }}### Default

`{{ flag.default }}`
**Default:** `{{ flag.default }}`
{%- endif %}
{%- if flag.env %}

{{ "#" | repeat(count=header_level) }}### Environment Variable

`{{ flag.env }}`
**Environment Variable:** `{{ flag.env }}`
{%- endif -%}
14 changes: 12 additions & 2 deletions lib/src/spec/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ impl From<&clap::Arg> for SpecArg {
arg.get_action(),
clap::ArgAction::Count | clap::ArgAction::Append
);
Self {
let choices = arg
.get_possible_values()
.iter()
.flat_map(|v| v.get_name_and_aliases().map(|s| s.to_string()))
.collect::<Vec<_>>();
let mut arg = Self {
name: arg
.get_value_names()
.unwrap_or_default()
Expand Down Expand Up @@ -183,8 +188,13 @@ impl From<&clap::Arg> for SpecArg {
.join("|"),
)
},
choices: None, // TODO: pull from clap
choices: None,
};
if !choices.is_empty() {
arg.choices = Some(SpecChoices { choices });
}

arg
}
}

Expand Down
22 changes: 16 additions & 6 deletions lib/src/spec/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,22 @@ impl From<&clap::Arg> for SpecFlag {
.collect::<Vec<_>>();
let name = get_name_from_short_and_long(&short, &long).unwrap_or_default();
let arg = if let clap::ArgAction::Set | clap::ArgAction::Append = c.get_action() {
let arg = c
.get_value_names()
.map(|s| s.iter().map(|s| s.to_string()).join(" "))
.unwrap_or(name.clone())
.as_str()
.into();
let mut arg = SpecArg::from(
c.get_value_names()
.map(|s| s.iter().map(|s| s.to_string()).join(" "))
.unwrap_or(name.clone())
.as_str(),
);

let choices = c
.get_possible_values()
.iter()
.flat_map(|v| v.get_name_and_aliases().map(|s| s.to_string()))
.collect::<Vec<_>>();
if !choices.is_empty() {
arg.choices = Some(SpecChoices { choices });
}

Some(arg)
} else {
None
Expand Down

0 comments on commit 6a2fb88

Please sign in to comment.