GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(spec): accept package metadata expressions · jdx/usage@8ac994b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cli/assets
    • usage.1
  • derive/src
    • codegen.rs
    • model.rs
  • docs/cli/reference
    • index.md
  • usage-rs/tests
    • facade.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cli/assets/usage.1‎

Copy file name to clipboardExpand all lines: cli/assets/usage.1
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,5 @@ Arguments to pass to script
518518

519519
Anything `usage` does not recognise is a value rather than a mistake, which is what
520520
lets a shebang script take flags of its own.
521+
.SH SOURCE
522+
https://github.com/jdx/usage
Collapse file

‎derive/src/codegen.rs‎

Copy file name to clipboardExpand all lines: derive/src/codegen.rs
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ pub fn emit(cli: &Cli) -> TokenStream {
196196
Some(tokens) => quote!(::core::option::Option::Some(#tokens)),
197197
None => quote!(::core::option::Option::None),
198198
};
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());
199202
let about = cli
200203
.about_attr
201204
.as_ref()
Collapse file

‎derive/src/model.rs‎

Copy file name to clipboardExpand all lines: derive/src/model.rs
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ pub struct Cli {
9191
/// From the struct's doc comment: first paragraph, and the whole thing.
9292
pub about: Option<String>,
9393
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>,
94101
/// Whether a flag-like token that names no flag is a value or an error. Unset
95102
/// means the spec's default, which is `value`.
96103
pub unknown_flags: Option<String>,
@@ -683,6 +690,9 @@ impl Cli {
683690
// declared.
684691
"about" => cli.about_attr = Some(metadata_expr(&meta)?),
685692
"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)?),
686696
"before_help" => cli.before_help = Some(metadata_expr(&meta)?),
687697
"next_help_heading" => cli.next_help_heading = Some(string_value(&meta)?),
688698
"before_long_help" => cli.before_long_help = Some(metadata_expr(&meta)?),
Collapse file

‎docs/cli/reference/index.md‎

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 numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
**Version**: 5.1.0
88

9+
**Repository**: https://github.com/jdx/usage
10+
911
- **Usage**: `usage [--completions <COMPLETIONS>] [--usage-spec] <SUBCOMMAND>`
1012

1113
## Flags
Collapse file

‎usage-rs/tests/facade.rs‎

Copy file name to clipboardExpand all lines: usage-rs/tests/facade.rs
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,43 @@ enum Command {
2323
Version,
2424
}
2525

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+
2663
#[derive(Cli)]
2764
#[command(bin = "negated-requirements", subcommand_negates_reqs)]
2865
#[allow(dead_code)]

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.