Use clap-derive

This commit is contained in:
Jamie Quigley 2022-04-22 12:53:23 +01:00
parent 13102d58c2
commit d02894b669
No known key found for this signature in database
GPG Key ID: 8E8FF66E2AE8D970
3 changed files with 112 additions and 30 deletions

89
Cargo.lock generated
View File

@ -27,12 +27,13 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "3.1.9"
version = "3.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6aad2534fad53df1cc12519c5cda696dd3e20e6118a027e24054aea14a0bdcbe"
checksum = "423af4bd829996d7de0b8bf0279f5b91a20306937be514e972a95c3d3ab13f33"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
@ -41,6 +42,19 @@ dependencies = [
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.1.1"
@ -63,6 +77,12 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -100,12 +120,65 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.3"
@ -121,6 +194,18 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -9,4 +9,4 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "3.1.9", features = ["env", "cargo"]}
clap = { version = "3.1.11", features = ["derive", "cargo", "env"] }

View File

@ -5,7 +5,8 @@ use std::{
process::{exit, Command, Stdio},
};
use clap::{arg, Arg, crate_version};
use clap::crate_version;
use clap::Parser;
fn pick(picker: &str, derivations: Vec<&str>) -> String {
let mut picker_process = Command::new(&picker)
@ -52,29 +53,9 @@ fn run_command(use_channel: bool, choice: &str, command: &str, trail: Vec<&str>)
}
fn main() {
let matches = clap::Command::new("comma")
.about("runs programs without installing them")
.version(crate_version!())
.arg(
Arg::new("install")
.short('i')
.long("install")
.takes_value(false)
.help("install the derivation containing the executable"),
)
.arg(Arg::new("picker")
.long("picker")
.env("COMMA_PICKER")
.takes_value(true)
.default_value("fzy")
)
.trailing_var_arg(true)
.arg(arg!(<cmd> ... "command to run"))
.get_matches();
let args = Opt::parse();
let install = matches.is_present("install");
let mut trail: Vec<&str> = matches.values_of("cmd").unwrap().collect();
let mut trail: Vec<&str> = args.cmd.iter().map(|x| &**x).collect();
let command: String = trail.remove(0).to_string();
let attrs = Command::new("nix-locate")
@ -96,18 +77,18 @@ fn main() {
.collect();
let choice = if attrs.len() != 1 {
pick(matches.value_of("picker").unwrap(), attrs)
pick(&args.picker, attrs)
} else {
attrs.first().unwrap().trim().to_string()
};
let use_channel = (match env::var("NIX_PATH") {
let use_channel = match env::var("NIX_PATH") {
Ok(val) => val,
Err(_) => "".to_string(),
})
}
.contains("nixpkgs");
if install {
if args.install {
Command::new("nix-env")
.args(["-f", "<nixpkgs>", "-iA", choice.rsplit('.').last().unwrap()])
.exec();
@ -115,3 +96,19 @@ fn main() {
run_command(use_channel, &choice, &command, trail)
}
}
/// Runs programs without installing them
#[derive(Parser)]
#[clap(version = crate_version!(), trailing_var_arg = true)]
struct Opt {
/// Install the derivation containing the executable
#[clap(short, long)]
install: bool,
#[clap(long, env = "COMMA_PICKER", default_value = "fzy")]
picker: String,
/// Command to run
#[clap(required = true, name = "cmd")]
cmd: Vec<String>,
}