Skip to content
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

Work around clap's implementation of subcommand version #189

Merged
merged 1 commit into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::opts::Coloring::*;
use crate::opts::{Coloring, Expand, Subcommand};
use crate::unparse::unparse_maximal;
use bat::{PagingMode, PrettyPrinter};
use clap::{Parser, ValueEnum};
use clap::{CommandFactory, Parser, ValueEnum};
use quote::quote;
use std::env;
use std::ffi::OsString;
Expand Down Expand Up @@ -62,6 +62,13 @@ fn cargo_binary() -> OsString {

fn cargo_expand() -> Result<i32> {
let Subcommand::Expand(args) = Subcommand::parse();

if args.version {
let mut stdout = io::stdout();
let _ = stdout.write_all(Subcommand::command().render_version().as_bytes());
return Ok(0);
}

let config = config::deserialize();

if args.themes {
Expand Down
6 changes: 5 additions & 1 deletion src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version"));
#[command(bin_name = "cargo", version = VERSION, author, disable_help_subcommand = true)]
pub enum Subcommand {
/// Show the result of macro expansion.
#[command(name = "expand", version = VERSION, author)]
#[command(name = "expand", version = VERSION, author, disable_version_flag = true)]
Expand(Expand),
}

Expand Down Expand Up @@ -118,6 +118,10 @@ pub struct Expand {
/// Local path to module or other named item to expand, e.g. os::unix::ffi
#[arg(value_name = "ITEM", value_parser = parse_selector)]
pub item: Option<Selector>,

/// Print version
#[arg(long)]
pub version: bool,
}

#[derive(ValueEnum, Debug, Clone, Copy)]
Expand Down