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 #8

Merged
merged 1 commit into from
Aug 5, 2023
Merged
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
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![allow(clippy::or_fun_call)]

use anyhow::{Context, Result};
use clap::Parser;
use clap::{CommandFactory, Parser};
use serde_derive::Deserialize;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::{self, Command, Stdio};

Expand All @@ -20,7 +21,12 @@ cargo_subcommand_metadata::description!("Remove Cargo.lock lockfile");
disable_help_subcommand = true
)]
enum Subcommand {
#[command(author, version, about = "Remove Cargo.lock lockfile")]
#[command(
author,
version,
about = "Remove Cargo.lock lockfile",
disable_version_flag = true
)]
Unlock(Unlock),
}

Expand All @@ -29,6 +35,10 @@ struct Unlock {
/// Path to Cargo.toml
#[arg(long, value_name = "PATH")]
manifest_path: Option<PathBuf>,

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

#[derive(Deserialize)]
Expand All @@ -39,6 +49,12 @@ struct Metadata {
fn main() -> Result<()> {
let Subcommand::Unlock(opts) = Subcommand::parse();

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

let cargo = env::var_os("CARGO").unwrap_or(OsString::from("cargo"));
let mut command = Command::new(cargo);
command.arg("metadata");
Expand Down