Repository navigation
feat(clap_usage)!: expose spec(), publish against usage-lib 4 #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,22 @@ | ||
| //! Build [`usage`] specs from [`clap`] commands. | ||
| //! | ||
| //! [`generate`] writes a spec straight out; [`spec`] hands you the [`usage::Spec`] | ||
| //! first so you can set things clap cannot express. | ||
| //! | ||
| //! The `usage` crate is re-exported, so depending on `clap_usage` alone is | ||
| //! enough to name the spec types you get back: | ||
| //! | ||
| //! ```no_run | ||
| //! use clap_usage::usage::SpecCommandEffect; | ||
| //! ``` | ||
|
|
||
| mod generate; | ||
|
|
||
| pub use crate::generate::generate; | ||
| /// The [`usage`] crate, re-exported. | ||
| /// | ||
| /// [`spec`] returns `usage` types, so consumers need to name them. Re-exporting | ||
| /// the whole crate means `clap_usage` on its own is a sufficient dependency, | ||
| /// and nothing here goes stale as `usage` grows. | ||
| pub use usage; | ||
|
|
||
| pub use crate::generate::{generate, spec}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| use clap::Command; | ||
| // Deliberately reached through clap_usage, proving a consumer needs no | ||
| // direct usage-lib dependency to annotate the spec it gets back. | ||
| use clap_usage::usage::SpecCommandEffect; | ||
|
|
||
| /// The reason `spec` exists: `generate` writes straight to a writer, so there | ||
| /// is no way to set something clap cannot express, such as `effect=`. | ||
| #[test] | ||
| fn spec_can_be_annotated_before_rendering() { | ||
| let mut cmd = Command::new("mycli") | ||
| .subcommand(Command::new("ls").about("List things")) | ||
| .subcommand(Command::new("rm").about("Remove things")); | ||
|
|
||
| let mut spec = clap_usage::spec(&mut cmd, "mycli"); | ||
| assert_eq!(spec.bin, "mycli"); | ||
|
|
||
| spec.cmd.subcommands.get_mut("ls").unwrap().effect = Some(SpecCommandEffect::Read); | ||
| spec.cmd.subcommands.get_mut("rm").unwrap().effect = Some(SpecCommandEffect::Destructive); | ||
|
|
||
| let rendered = spec.to_string(); | ||
| assert!(rendered.contains("cmd ls"), "{rendered}"); | ||
| assert!(rendered.contains("effect=read"), "{rendered}"); | ||
| assert!(rendered.contains("effect=destructive"), "{rendered}"); | ||
| } | ||
|
|
||
| /// `generate` must keep producing exactly what it always did. | ||
| #[test] | ||
| fn generate_still_writes_the_header_and_spec() { | ||
| let mut cmd = Command::new("mycli").subcommand(Command::new("ls")); | ||
| let mut buf = vec![]; | ||
| clap_usage::generate(&mut cmd, "mycli", &mut buf); | ||
| let out = String::from_utf8(buf).unwrap(); | ||
| assert!(out.starts_with("// @generated by usage-cli from clap metadata\n")); | ||
| assert!(out.contains("bin mycli")); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.