GeezerVison is showing github.com — fix(spec): accept package metadata expressions · jdx/usage@8ac994b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cli/assets/usage.1 +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -518,3 +518,5 @@ Arguments to pass to script | 518 | 518 |
| 519 | 519 | Anything `usage` does not recognise is a value rather than a mistake, which is what | 520 | 520 | lets a shebang script take flags of its own. | | 521 | +.SH SOURCE | | 522 | +https://github.com/jdx/usage |
Collapse file Copy file name to clipboardExpand all lines: derive/src/codegen.rs +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -196,6 +196,9 @@ pub fn emit(cli: &Cli) -> TokenStream { | 196 | 196 | Some(tokens) => quote!(::core::option::Option::Some(#tokens)), | 197 | 197 | None => quote!(::core::option::Option::None), | 198 | 198 | }; | | 199 | + let author = option_expr(cli.author.as_ref()); | | 200 | + let license = option_expr(cli.license.as_ref()); | | 201 | + let repository = option_expr(cli.repository.as_ref()); | 199 | 202 | let about = cli | 200 | 203 | .about_attr | 201 | 204 | .as_ref() | |
Collapse file Copy file name to clipboardExpand all lines: derive/src/model.rs +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,6 +91,13 @@ pub struct Cli { | 91 | 91 | /// From the struct's doc comment: first paragraph, and the whole thing. | 92 | 92 | pub about: Option<String>, | 93 | 93 | pub long_about: Option<String>, | | 94 | + /// Package metadata expressions whose results must be usable as `&'static str`. | | 95 | + /// | | 96 | + /// Keeping their tokens lets the Cargo-provided `env!("CARGO_PKG_…")` values remain the | | 97 | + /// source of truth instead of making adopters repeat those strings in the derive attribute. | | 98 | + pub author: Option<proc_macro2::TokenStream>, | | 99 | + pub license: Option<proc_macro2::TokenStream>, | | 100 | + pub repository: Option<proc_macro2::TokenStream>, | 94 | 101 | /// Whether a flag-like token that names no flag is a value or an error. Unset | 95 | 102 | /// means the spec's default, which is `value`. | 96 | 103 | pub unknown_flags: Option<String>, | @@ -683,6 +690,9 @@ impl Cli { | 683 | 690 | // declared. | 684 | 691 | "about" => cli.about_attr = Some(metadata_expr(&meta)?), | 685 | 692 | "long_about" => cli.long_about_attr = Some(metadata_expr(&meta)?), | | 693 | + "author" => cli.author = Some(metadata_expr(&meta)?), | | 694 | + "license" => cli.license = Some(metadata_expr(&meta)?), | | 695 | + "repository" => cli.repository = Some(metadata_expr(&meta)?), | 686 | 696 | "before_help" => cli.before_help = Some(metadata_expr(&meta)?), | 687 | 697 | "next_help_heading" => cli.next_help_heading = Some(string_value(&meta)?), | 688 | 698 | "before_long_help" => cli.before_long_help = Some(metadata_expr(&meta)?), | |
Collapse file Copy file name to clipboardExpand all lines: docs/cli/reference/index.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 |
| 7 | 7 | **Version**: 5.1.0 | 8 | 8 |
| | 9 | +**Repository**: https://github.com/jdx/usage | | 10 | + | 9 | 11 | - **Usage**: `usage [--completions <COMPLETIONS>] [--usage-spec] <SUBCOMMAND>` | 10 | 12 |
| 11 | 13 | ## Flags | |
Collapse file Copy file name to clipboardExpand all lines: usage-rs/tests/facade.rs +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,43 @@ enum Command { | 23 | 23 | Version, | 24 | 24 | } | 25 | 25 |
| | 26 | +#[derive(Cli)] | | 27 | +#[usage( | | 28 | + bin = "metadata", | | 29 | + author = env!("CARGO_PKG_AUTHORS"), | | 30 | + license = env!("CARGO_PKG_LICENSE"), | | 31 | + repository = env!("CARGO_PKG_REPOSITORY") | | 32 | +)] | | 33 | +struct PackageMetadata; | | 34 | + | | 35 | +#[test] | | 36 | +fn package_metadata_survives_spec_emission() { | | 37 | + let kdl = PackageMetadata::to_kdl(); | | 38 | + assert!(kdl.contains("author \"Jeff Dickey @jdx\""), "{kdl}"); | | 39 | + assert!(kdl.contains("license MIT"), "{kdl}"); | | 40 | + assert!( | | 41 | + kdl.contains("repository \"https://github.com/jdx/usage\""), | | 42 | + "{kdl}" | | 43 | + ); | | 44 | + | | 45 | + let spec: usage_parser::Spec = kdl.parse().expect("usage-lib should read derive output"); | | 46 | + assert_eq!(spec.author.as_deref(), Some("Jeff Dickey @jdx")); | | 47 | + assert_eq!(spec.license.as_deref(), Some("MIT")); | | 48 | + assert_eq!( | | 49 | + spec.repository.as_deref(), | | 50 | + Some("https://github.com/jdx/usage") | | 51 | + ); | | 52 | +} | | 53 | + | | 54 | +#[test] | | 55 | +fn required_root_subcommand_survives_spec_emission() { | | 56 | + let kdl = Ex::to_kdl(); | | 57 | + assert!(kdl.contains("subcommand_required #true"), "{kdl}"); | | 58 | + | | 59 | + let spec: usage_parser::Spec = kdl.parse().expect("usage-lib should read derive output"); | | 60 | + assert!(spec.cmd.subcommand_required); | | 61 | +} | | 62 | + | 26 | 63 | #[derive(Cli)] | 27 | 64 | #[command(bin = "negated-requirements", subcommand_negates_reqs)] | 28 | 65 | #[allow(dead_code)] | |
|