update to clap v4

pull/304/head
Lucas Nogueira 2 years ago
parent 1fd768e240
commit 955245ec01
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

247
Cargo.lock generated

@ -105,6 +105,55 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is-terminal",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d"
[[package]]
name = "anstyle-parse"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "anstyle-wincon"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd"
dependencies = [
"anstyle",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.69" version = "1.0.69"
@ -581,27 +630,31 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.2.23" version = "4.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a"
dependencies = [ dependencies = [
"atty", "clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6"
dependencies = [
"anstream",
"anstyle",
"bitflags", "bitflags",
"clap_lex", "clap_lex",
"indexmap",
"strsim", "strsim",
"termcolor",
"textwrap",
] ]
[[package]] [[package]]
name = "clap_lex" name = "clap_lex"
version = "0.2.4" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1"
dependencies = [
"os_str_bytes",
]
[[package]] [[package]]
name = "cocoa" name = "cocoa"
@ -650,6 +703,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "colored" name = "colored"
version = "1.9.3" version = "1.9.3"
@ -1176,6 +1235,27 @@ dependencies = [
"regex", "regex",
] ]
[[package]]
name = "errno"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "event-listener" name = "event-listener"
version = "2.5.3" version = "2.5.3"
@ -1790,6 +1870,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]] [[package]]
name = "hex" name = "hex"
version = "0.4.3" version = "0.4.3"
@ -2034,6 +2120,17 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "io-lifetimes"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220"
dependencies = [
"hermit-abi 0.3.1",
"libc",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "iota-crypto" name = "iota-crypto"
version = "0.15.3" version = "0.15.3"
@ -2093,6 +2190,18 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146" checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "is-terminal"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f"
dependencies = [
"hermit-abi 0.3.1",
"io-lifetimes",
"rustix",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"
@ -2326,6 +2435,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "linux-raw-sys"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.9" version = "0.4.9"
@ -2810,12 +2925,6 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
] ]
[[package]]
name = "os_str_bytes"
version = "6.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267"
[[package]] [[package]]
name = "overload" name = "overload"
version = "0.1.1" version = "0.1.1"
@ -3514,6 +3623,20 @@ dependencies = [
"semver 1.0.16", "semver 1.0.16",
] ]
[[package]]
name = "rustix"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b24138615de35e32031d041a09032ef3487a616d901ca4db224e7d557efae2"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys 0.45.0",
]
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.8" version = "0.20.8"
@ -4723,12 +4846,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[package]] [[package]]
name = "thin-slice" name = "thin-slice"
version = "0.1.1" version = "0.1.1"
@ -5144,6 +5261,12 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.3.0" version = "1.3.0"
@ -5515,7 +5638,7 @@ checksum = "9e745dab35a0c4c77aa3ce42d595e13d2003d6902d6b08c9ef5fc326d08da12b"
dependencies = [ dependencies = [
"windows-implement", "windows-implement",
"windows-interface", "windows-interface",
"windows-targets", "windows-targets 0.42.1",
] ]
[[package]] [[package]]
@ -5562,12 +5685,12 @@ version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.42.1",
"windows_aarch64_msvc 0.42.1", "windows_aarch64_msvc 0.42.1",
"windows_i686_gnu 0.42.1", "windows_i686_gnu 0.42.1",
"windows_i686_msvc 0.42.1", "windows_i686_msvc 0.42.1",
"windows_x86_64_gnu 0.42.1", "windows_x86_64_gnu 0.42.1",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.42.1",
"windows_x86_64_msvc 0.42.1", "windows_x86_64_msvc 0.42.1",
] ]
@ -5577,7 +5700,16 @@ version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.42.1",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.0",
] ]
[[package]] [[package]]
@ -5586,15 +5718,30 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.42.1",
"windows_aarch64_msvc 0.42.1", "windows_aarch64_msvc 0.42.1",
"windows_i686_gnu 0.42.1", "windows_i686_gnu 0.42.1",
"windows_i686_msvc 0.42.1", "windows_i686_msvc 0.42.1",
"windows_x86_64_gnu 0.42.1", "windows_x86_64_gnu 0.42.1",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.42.1",
"windows_x86_64_msvc 0.42.1", "windows_x86_64_msvc 0.42.1",
] ]
[[package]]
name = "windows-targets"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
dependencies = [
"windows_aarch64_gnullvm 0.48.0",
"windows_aarch64_msvc 0.48.0",
"windows_i686_gnu 0.48.0",
"windows_i686_msvc 0.48.0",
"windows_x86_64_gnu 0.48.0",
"windows_x86_64_gnullvm 0.48.0",
"windows_x86_64_msvc 0.48.0",
]
[[package]] [[package]]
name = "windows-tokens" name = "windows-tokens"
version = "0.44.0" version = "0.44.0"
@ -5607,6 +5754,12 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.36.1" version = "0.36.1"
@ -5625,6 +5778,12 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.36.1" version = "0.36.1"
@ -5643,6 +5802,12 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.36.1" version = "0.36.1"
@ -5661,6 +5826,12 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.36.1" version = "0.36.1"
@ -5679,12 +5850,24 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.1" version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.36.1" version = "0.36.1"
@ -5703,6 +5886,12 @@ version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
[[package]] [[package]]
name = "winreg" name = "winreg"
version = "0.10.1" version = "0.10.1"

@ -11,4 +11,4 @@ serde_json.workspace = true
tauri.workspace = true tauri.workspace = true
log.workspace = true log.workspace = true
thiserror.workspace = true thiserror.workspace = true
clap = "3" clap = { version = "4", features = ["string"] }

@ -34,13 +34,6 @@ pub struct Arg {
/// NOTE: Setting this requires `takes_value` to be set to true. /// NOTE: Setting this requires `takes_value` to be set to true.
#[serde(default)] #[serde(default)]
pub multiple: bool, pub multiple: bool,
/// Specifies that the argument may appear more than once.
/// For flags, this results in the number of occurrences of the flag being recorded. For example -ddd or -d -d -d would count as three occurrences.
/// For options or arguments that take a value, this does not affect how many values they can accept. (i.e. only one at a time is allowed)
///
/// For example, --opt val1 --opt val2 is allowed, but --opt val1 val2 is not.
#[serde(default, alias = "multiple-occurrences")]
pub multiple_occurrences: bool,
/// Specifies how many values are required to satisfy this argument. For example, if you had a /// Specifies how many values are required to satisfy this argument. For example, if you had a
/// `-f <file>` argument where you wanted exactly 3 'files' you would set /// `-f <file>` argument where you wanted exactly 3 'files' you would set
/// `number_of_values = 3`, and this argument wouldn't be satisfied unless the user provided /// `number_of_values = 3`, and this argument wouldn't be satisfied unless the user provided
@ -102,11 +95,11 @@ pub struct Arg {
/// Allows a conditional requirement with the signature [arg, value] /// Allows a conditional requirement with the signature [arg, value]
/// the requirement will only become valid if `arg`'s value equals `${value}`. /// the requirement will only become valid if `arg`'s value equals `${value}`.
#[serde(alias = "requires-if")] #[serde(alias = "requires-if")]
pub requires_if: Option<Vec<String>>, pub requires_if: Option<(String, String)>,
/// Allows specifying that an argument is required conditionally with the signature [arg, value] /// Allows specifying that an argument is required conditionally with the signature [arg, value]
/// the requirement will only become valid if the `arg`'s value equals `${value}`. /// the requirement will only become valid if the `arg`'s value equals `${value}`.
#[serde(alias = "requires-if-eq")] #[serde(alias = "required-if-eq")]
pub required_if_eq: Option<Vec<String>>, pub required_if_eq: Option<(String, String)>,
/// Requires that options use the --option=val syntax /// Requires that options use the --option=val syntax
/// i.e. an equals between the option and associated value. /// i.e. an equals between the option and associated value.
#[serde(alias = "requires-equals")] #[serde(alias = "requires-equals")]

@ -2,10 +2,11 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
//! Types and functions related to CLI arguments. use clap::{
builder::{PossibleValue, PossibleValuesParser},
use clap::{Arg as ClapArg, ArgMatches, ErrorKind}; error::ErrorKind,
use clapfix::{ClapCommand as App, ErrorExt}; Arg as ClapArg, ArgAction, ArgMatches, Command,
};
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use tauri::PackageInfo; use tauri::PackageInfo;
@ -17,23 +18,6 @@ use std::collections::HashMap;
#[macro_use] #[macro_use]
mod macros; mod macros;
mod clapfix {
//! Compatibility between `clap` 3.0 and 3.1+ without deprecation errors.
#![allow(deprecated)]
pub type ClapCommand<'help> = clap::App<'help>;
pub trait ErrorExt {
fn kind(&self) -> clap::ErrorKind;
}
impl ErrorExt for clap::Error {
fn kind(&self) -> clap::ErrorKind {
self.kind
}
}
}
/// The resolution of a argument match. /// The resolution of a argument match.
#[derive(Default, Debug, Serialize)] #[derive(Default, Debug, Serialize)]
#[non_exhaustive] #[non_exhaustive]
@ -45,7 +29,7 @@ pub struct ArgData {
pub value: Value, pub value: Value,
/// The number of occurrences of the argument. /// The number of occurrences of the argument.
/// e.g. `./app --arg 1 --arg 2 --arg 2 3 4` results in three occurrences. /// e.g. `./app --arg 1 --arg 2 --arg 2 3 4` results in three occurrences.
pub occurrences: u64, pub occurrences: u8,
} }
/// The matched subcommand. /// The matched subcommand.
@ -100,11 +84,17 @@ pub fn get_matches(cli: &Config, package_info: &PackageInfo) -> crate::Result<Ma
.description() .description()
.unwrap_or(&package_info.description.to_string()) .unwrap_or(&package_info.description.to_string())
.to_string(); .to_string();
let version = &*package_info.version.to_string(); let version = package_info.version.to_string();
let app = get_app(package_info, version, &package_info.name, Some(&about), cli); let app = get_app(
package_info,
version,
package_info.name.clone(),
Some(&about),
cli,
);
match app.try_get_matches() { match app.try_get_matches() {
Ok(matches) => Ok(get_matches_internal(cli, &matches)), Ok(matches) => Ok(get_matches_internal(cli, &matches)),
Err(e) => match ErrorExt::kind(&e) { Err(e) => match e.kind() {
ErrorKind::DisplayHelp => { ErrorKind::DisplayHelp => {
let mut matches = Matches::default(); let mut matches = Matches::default();
let help_text = e.to_string(); let help_text = e.to_string();
@ -152,28 +142,27 @@ fn get_matches_internal(config: &Config, matches: &ArgMatches) -> Matches {
fn map_matches(config: &Config, matches: &ArgMatches, cli_matches: &mut Matches) { fn map_matches(config: &Config, matches: &ArgMatches, cli_matches: &mut Matches) {
if let Some(args) = config.args() { if let Some(args) = config.args() {
for arg in args { for arg in args {
#[allow(deprecated)] let (occurrences, value) = if arg.takes_value {
let occurrences = matches.occurrences_of(arg.name.clone()); if arg.multiple {
let value = if occurrences == 0 || !arg.takes_value {
Value::Bool(occurrences > 0)
} else if arg.multiple {
#[allow(deprecated)]
matches matches
.values_of(arg.name.clone()) .get_many::<String>(&arg.name)
.map(|v| { .map(|v| {
let mut values = Vec::new(); let mut values = Vec::new();
for value in v { for value in v {
values.push(Value::String(value.to_string())); values.push(Value::String(value.into()));
} }
Value::Array(values) (values.len() as u8, Value::Array(values))
}) })
.unwrap_or(Value::Null) .unwrap_or((0, Value::Null))
} else { } else {
#[allow(deprecated)]
matches matches
.value_of(arg.name.clone()) .get_one::<String>(&arg.name)
.map(|v| Value::String(v.to_string())) .map(|v| (1, Value::String(v.clone())))
.unwrap_or(Value::Null) .unwrap_or((0, Value::Null))
}
} else {
let occurrences = matches.get_count(&arg.name);
(occurrences, Value::Bool(occurrences > 0))
}; };
cli_matches.set_arg(arg.name.clone(), ArgData { value, occurrences }); cli_matches.set_arg(arg.name.clone(), ArgData { value, occurrences });
@ -181,34 +170,33 @@ fn map_matches(config: &Config, matches: &ArgMatches, cli_matches: &mut Matches)
} }
} }
fn get_app<'a>( fn get_app(
package_info: &'a PackageInfo, package_info: &PackageInfo,
version: &'a str, version: String,
command_name: &'a str, command_name: String,
about: Option<&'a String>, about: Option<&String>,
config: &'a Config, config: &Config,
) -> App<'a> { ) -> Command {
let mut app = App::new(command_name) let mut app = Command::new(command_name)
.author(package_info.authors) .author(package_info.authors)
.version(version); .version(version.clone());
if let Some(about) = about { if let Some(about) = about {
app = app.about(&**about); app = app.about(about);
} }
if let Some(long_description) = config.long_description() { if let Some(long_description) = config.long_description() {
app = app.long_about(&**long_description); app = app.long_about(long_description);
} }
if let Some(before_help) = config.before_help() { if let Some(before_help) = config.before_help() {
app = app.before_help(&**before_help); app = app.before_help(before_help);
} }
if let Some(after_help) = config.after_help() { if let Some(after_help) = config.after_help() {
app = app.after_help(&**after_help); app = app.after_help(after_help);
} }
if let Some(args) = config.args() { if let Some(args) = config.args() {
for arg in args { for arg in args {
let arg_name = arg.name.as_ref(); app = app.arg(get_arg(arg.name.clone(), arg));
app = app.arg(get_arg(arg_name, arg));
} }
} }
@ -216,8 +204,8 @@ fn get_app<'a>(
for (subcommand_name, subcommand) in subcommands { for (subcommand_name, subcommand) in subcommands {
let clap_subcommand = get_app( let clap_subcommand = get_app(
package_info, package_info,
version, version.clone(),
subcommand_name, subcommand_name.to_string(),
subcommand.description(), subcommand.description(),
subcommand, subcommand,
); );
@ -228,8 +216,8 @@ fn get_app<'a>(
app app
} }
fn get_arg<'a>(arg_name: &'a str, arg: &'a Arg) -> ClapArg<'a> { fn get_arg(arg_name: String, arg: &Arg) -> ClapArg {
let mut clap_arg = ClapArg::new(arg_name); let mut clap_arg = ClapArg::new(arg_name.clone());
if arg.index.is_none() { if arg.index.is_none() {
clap_arg = clap_arg.long(arg_name); clap_arg = clap_arg.long(arg_name);
@ -240,19 +228,34 @@ fn get_arg<'a>(arg_name: &'a str, arg: &'a Arg) -> ClapArg<'a> {
clap_arg = bind_string_arg!(arg, clap_arg, description, help); clap_arg = bind_string_arg!(arg, clap_arg, description, help);
clap_arg = bind_string_arg!(arg, clap_arg, long_description, long_help); clap_arg = bind_string_arg!(arg, clap_arg, long_description, long_help);
clap_arg = clap_arg.takes_value(arg.takes_value);
clap_arg = clap_arg.multiple_values(arg.multiple); let action = if arg.multiple {
#[allow(deprecated)] ArgAction::Append
{ } else if arg.takes_value {
clap_arg = clap_arg.multiple_occurrences(arg.multiple_occurrences); ArgAction::Set
} } else {
ArgAction::Count
};
clap_arg = clap_arg.action(action);
clap_arg = bind_value_arg!(arg, clap_arg, number_of_values); clap_arg = bind_value_arg!(arg, clap_arg, number_of_values);
#[allow(deprecated)]
{ if let Some(values) = &arg.possible_values {
clap_arg = bind_string_slice_arg!(arg, clap_arg, possible_values); clap_arg = clap_arg.value_parser(PossibleValuesParser::new(
values
.iter()
.map(PossibleValue::new)
.collect::<Vec<PossibleValue>>(),
));
} }
clap_arg = bind_value_arg!(arg, clap_arg, min_values);
clap_arg = bind_value_arg!(arg, clap_arg, max_values); clap_arg = match (arg.min_values, arg.max_values) {
(Some(min), Some(max)) => clap_arg.num_args(min..=max),
(Some(min), None) => clap_arg.num_args(min..),
(None, Some(max)) => clap_arg.num_args(0..max),
(None, None) => clap_arg,
};
clap_arg = clap_arg.required(arg.required); clap_arg = clap_arg.required(arg.required);
clap_arg = bind_string_arg!( clap_arg = bind_string_arg!(
arg, arg,
@ -264,13 +267,11 @@ fn get_arg<'a>(arg_name: &'a str, arg: &'a Arg) -> ClapArg<'a> {
clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_present_any); clap_arg = bind_string_slice_arg!(arg, clap_arg, required_unless_present_any);
clap_arg = bind_string_arg!(arg, clap_arg, conflicts_with, conflicts_with); clap_arg = bind_string_arg!(arg, clap_arg, conflicts_with, conflicts_with);
if let Some(value) = &arg.conflicts_with_all { if let Some(value) = &arg.conflicts_with_all {
let v: Vec<&str> = value.iter().map(|x| &**x).collect(); clap_arg = clap_arg.conflicts_with_all(value);
clap_arg = clap_arg.conflicts_with_all(&v);
} }
clap_arg = bind_string_arg!(arg, clap_arg, requires, requires); clap_arg = bind_string_arg!(arg, clap_arg, requires, requires);
if let Some(value) = &arg.requires_all { if let Some(value) = &arg.requires_all {
let v: Vec<&str> = value.iter().map(|x| &**x).collect(); clap_arg = clap_arg.requires_all(value);
clap_arg = clap_arg.requires_all(&v);
} }
clap_arg = bind_if_arg!(arg, clap_arg, requires_if); clap_arg = bind_if_arg!(arg, clap_arg, requires_if);
clap_arg = bind_if_arg!(arg, clap_arg, required_if_eq); clap_arg = bind_if_arg!(arg, clap_arg, required_if_eq);

@ -7,7 +7,7 @@ macro_rules! bind_string_arg {
let arg = $arg; let arg = $arg;
let mut clap_arg = $clap_arg; let mut clap_arg = $clap_arg;
if let Some(value) = &arg.$arg_name { if let Some(value) = &arg.$arg_name {
clap_arg = clap_arg.$clap_field(value.as_str()); clap_arg = clap_arg.$clap_field(value);
} }
clap_arg clap_arg
}}; }};
@ -29,8 +29,7 @@ macro_rules! bind_string_slice_arg {
let arg = $arg; let arg = $arg;
let mut clap_arg = $clap_arg; let mut clap_arg = $clap_arg;
if let Some(value) = &arg.$field { if let Some(value) = &arg.$field {
let v: Vec<&str> = value.iter().map(|x| &**x).collect(); clap_arg = clap_arg.$field(value);
clap_arg = clap_arg.$field(v);
} }
clap_arg clap_arg
}}; }};
@ -40,9 +39,8 @@ macro_rules! bind_if_arg {
($arg:expr, $clap_arg:expr, $field:ident) => {{ ($arg:expr, $clap_arg:expr, $field:ident) => {{
let arg = $arg; let arg = $arg;
let mut clap_arg = $clap_arg; let mut clap_arg = $clap_arg;
if let Some(value) = &arg.$field { if let Some((value, arg)) = &arg.$field {
let v: Vec<&str> = value.iter().map(|x| &**x).collect(); clap_arg = clap_arg.$field(value, arg);
clap_arg = clap_arg.$field(&v[0], &v[1]);
} }
clap_arg clap_arg
}}; }};

Loading…
Cancel
Save