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

refactor: Upgrade kclvm/cmd clap version to 4.x #560

Merged
merged 1 commit into from May 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 91 additions & 5 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kclvm/cmd/Cargo.toml
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
anyhow = "1.0"
clap = "3.2.22"
clap = "4.3.0"
compiler_base_session = {path = "../../compiler_base/session"}

kclvm-api = {path = "../api"}
Expand Down
2 changes: 1 addition & 1 deletion kclvm/cmd/src/fmt.rs
Expand Up @@ -5,7 +5,7 @@ use kclvm_tools::format::{format, FormatOptions};

/// Run the KCL fmt command.
pub fn fmt_command(matches: &ArgMatches) -> Result<()> {
let input = matches.value_of("input");
let input = matches.get_one::<String>("input").map(|f| f.as_str());
match input {
Some(input) => {
format(
Expand Down
115 changes: 56 additions & 59 deletions kclvm/cmd/src/lib.rs
Expand Up @@ -13,6 +13,8 @@ pub mod vet;
#[cfg(test)]
mod tests;

use clap::{ArgAction, Command};

use std::io;

use anyhow::Result;
Expand All @@ -25,71 +27,66 @@ use vet::vet_command;
pub fn main(args: &[&str]) -> Result<()> {
let matches = app().arg_required_else_help(true).get_matches_from(args);
// Sub commands
if let Some(matches) = matches.subcommand_matches("run") {
run_command(matches, &mut io::stdout())
} else if let Some(matches) = matches.subcommand_matches("lint") {
lint_command(matches)
} else if let Some(matches) = matches.subcommand_matches("fmt") {
fmt_command(matches)
} else if let Some(matches) = matches.subcommand_matches("vet") {
vet_command(matches)
} else if matches.subcommand_matches("server").is_some() {
kclvm_api::service::jsonrpc::start_stdio_server()
} else if matches.subcommand_matches("version").is_some() {
println!("{}", kclvm_version::get_version_info());
Ok(())
} else {
Ok(())
match matches.subcommand() {
Some(("run", sub_matches)) => run_command(sub_matches, &mut io::stdout()),
Some(("lint", sub_matches)) => lint_command(sub_matches),
Some(("fmt", sub_matches)) => fmt_command(sub_matches),
Some(("vet", sub_matches)) => vet_command(sub_matches),
Some(("server", _)) => kclvm_api::service::jsonrpc::start_stdio_server(),
Some(("version", _)) => {
println!("{}", kclvm_version::get_version_info());
Ok(())
}
_ => Ok(()),
}
}

/// Get the KCLVM CLI application.
pub fn app() -> clap::App<'static> {
clap_app!(kclvm_cli =>
(version: kclvm_version::VERSION)
(about: "KCL main CLI")
(@subcommand run =>
(about: "Run KCL files")
(@arg input: ... "Specify the input files to run")
(@arg output: -o --output +takes_value "Specify the YAML output file path")
(@arg setting: ... -Y --setting +takes_value "Specify the input setting file")
(@arg verbose: -v --verbose "Print test information verbosely")
(@arg disable_none: -n --disable_none "Disable dumping None values")
(@arg strict_range_check: -r --strict_range_check "Do perform strict numeric range checks")
(@arg debug: -d --debug "Run in debug mode (for developers only)")
(@arg sort_keys: -k --sort_keys "Sort result keys")
(@arg arguments: ... -D --argument +takes_value "Specify the top-level argument")
(@arg path_selector: ... -S --path_selector "Specify the path selector")
(@arg overrides: ... -O --overrides +takes_value "Specify the configuration override path and value")
(@arg target: --target +takes_value "Specify the target type")
(@arg package_map: ... -E --external +takes_value "Mapping of package name and path where the package is located")
)
(@subcommand lint =>
(about: "Lint KCL files")
(@arg input: ... "Sets the input file to use")
(@arg setting: ... -Y --setting +takes_value "Sets the input file to use")
(@arg verbose: -v --verbose "Print test information verbosely")
(@arg emit_warning: --emit_warning "Emit warning message")
)
(@subcommand fmt =>
(about: "Format KCL files")
(@arg input: "Input file or path name for formatting")
(@arg recursive: -R --recursive "Iterate through subdirectories recursively")
(@arg std_output: -w --std_output "Whether to output format to stdout")
pub fn app() -> Command {
Command::new("kclvm_cli")
.version(kclvm_version::VERSION)
.about("KCL main CLI.")
.subcommand(
Command::new("run")
.about("run")
.arg(arg!([input] ... "Specify the input files to run").num_args(0..))
.arg(arg!(output: -o --output <output> "Specify the YAML output file path"))
.arg(arg!(setting: -Y --setting <setting> ... "Specify the input setting file").num_args(1..))
.arg(arg!(verbose: -v --verbose "Print test information verbosely").action(ArgAction::Count))
.arg(arg!(disable_none: -n --disable_none "Disable dumping None values"))
.arg(arg!(strict_range_check: -r --strict_range_check "Do perform strict numeric range checks"))
.arg(arg!(debug: -d --debug "Run in debug mode (for developers only)"))
.arg(arg!(sort_keys: -k --sort_keys "Sort result keys"))
.arg(arg!(arguments: -D --argument <arguments> ... "Specify the top-level argument").num_args(1..))
.arg(arg!(path_selector: -S --path_selector <path_selector> ... "Specify the path selector").num_args(1..))
.arg(arg!(overrides: -O --overrides <overrides> ... "Specify the configuration override path and value").num_args(1..))
.arg(arg!(target: --target <target> "Specify the target type"))
.arg(arg!(package_map: -E --external <package_map> ... "Mapping of package name and path where the package is located").num_args(1..)),
)
(@subcommand vet =>
(about: "Validate data files with KCL files")
(@arg data_file: "Validation data file")
(@arg kcl_file: "KCL file")
(@arg schema: -d --schema +takes_value "Iterate through subdirectories recursively")
(@arg attribute_name: -n --attribute_name +takes_value "The attribute name for the data loading")
(@arg format: --format +takes_value "Validation data file format, support YAML and JSON, default is JSON")
.subcommand(
Command::new("lint")
.about("lint")
.arg(arg!([input] ... "Sets the input file to use").num_args(0..))
.arg(arg!(setting: -Y --setting <setting> ... "Sets the input file to use").num_args(1..))
.arg(arg!(verbose: -v --verbose "Print test information verbosely").action(ArgAction::Count))
.arg(arg!(emit_warning: --emit_warning "Emit warning message")),
)
(@subcommand server =>
(about: "Start a rpc server for APIs")
.subcommand(
Command::new("fmt")
.about("Format KCL files")
.arg(arg!(<input> "Input file or path name for formatting"))
.arg(arg!(recursive: -R --recursive "Iterate through subdirectories recursively"))
.arg(arg!(std_output: -w --std_output "Whether to output format to stdout")),
)
(@subcommand version =>
(about: "Show the KCL version")
.subcommand(
Command::new("vet")
.about("Validate data files witch KCL files")
.arg(arg!(<data_file> "Validation data file"))
.arg(arg!(<kcl_file> "KCL file"))
.arg(arg!(schema: -d --schema <schema> "Iterate through subdirectories recursively").num_args(1..))
.arg(arg!(attribute_name: -n --attribute_name <attribute_name> "The attribute name for the data loading"))
.arg(arg!(format: --format <format> "Validation data file format, support YAML and JSON, default is JSON")),
)
)
.subcommand(Command::new("server").about("Start a rpc server for APIs"))
.subcommand(Command::new("version").about("Show the KCL version"))
}
6 changes: 3 additions & 3 deletions kclvm/cmd/src/lint.rs
Expand Up @@ -8,8 +8,8 @@ use crate::settings::must_build_settings;

/// Run the KCL lint command.
pub fn lint_command(matches: &ArgMatches) -> Result<()> {
let mut files: Vec<&str> = match matches.values_of("input") {
Some(files) => files.into_iter().collect::<Vec<&str>>(),
let mut files = match matches.get_many::<String>("input") {
Some(files) => files.into_iter().map(|f| f.as_str()).collect::<Vec<&str>>(),
None => vec![],
};
// Config settings building
Expand All @@ -24,7 +24,7 @@ pub fn lint_command(matches: &ArgMatches) -> Result<()> {
let (mut err_handler, mut warning_handler) = (Handler::default(), Handler::default());
(err_handler.diagnostics, warning_handler.diagnostics) =
lint_files(&files, Some(args.get_load_program_options()));
if matches.occurrences_of("emit_warning") > 0 {
if matches.get_count("emit_warning") > 0 {
warning_handler.emit()?;
}
err_handler.abort_if_any_errors();
Expand Down
11 changes: 6 additions & 5 deletions kclvm/cmd/src/settings.rs
Expand Up @@ -26,14 +26,15 @@ pub(crate) fn must_build_settings(matches: &ArgMatches) -> SettingsPathBuf {

/// Build settings from arg matches.
pub(crate) fn build_settings(matches: &ArgMatches) -> Result<SettingsPathBuf> {
let files: Vec<&str> = match matches.values_of("input") {
Some(files) => files.into_iter().collect::<Vec<&str>>(),
let files: Vec<&str> = match matches.get_many::<String>("input") {
Some(files) => files.into_iter().map(|f| f.as_str()).collect::<Vec<&str>>(),
None => vec![],
};

let setting_files = matches
.values_of("setting")
.map(|files| files.into_iter().collect::<Vec<&str>>());
.get_many::<String>("setting")
.map(|files| files.into_iter().map(|f| f.as_str()).collect::<Vec<&str>>());

let arguments = strings_from_matches(matches, "arguments");

let package_maps = hashmaps_from_matches(matches, "package_map").transpose()?;
Expand All @@ -43,7 +44,7 @@ pub(crate) fn build_settings(matches: &ArgMatches) -> Result<SettingsPathBuf> {
setting_files,
Some(SettingsFile {
kcl_cli_configs: Some(Config {
output: matches.value_of("output").map(|v| v.to_string()),
output: matches.get_one::<String>("output").map(|v| v.to_string()),
overrides: strings_from_matches(matches, "overrides"),
path_selector: strings_from_matches(matches, "path_selector"),
strict_range_check: bool_from_matches(matches, "strict_range_check"),
Expand Down
11 changes: 5 additions & 6 deletions kclvm/cmd/src/util.rs
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;

#[inline]
pub(crate) fn strings_from_matches(matches: &ArgMatches, key: &str) -> Option<Vec<String>> {
matches.values_of(key).map(|files| {
matches.get_many::<String>(key).map(|files| {
files
.into_iter()
.map(|v| v.to_string())
Expand All @@ -17,7 +17,7 @@ pub(crate) fn hashmaps_from_matches(
matches: &ArgMatches,
key: &str,
) -> Option<Result<HashMap<String, String>>> {
matches.values_of(key).map(|files| {
matches.get_many::<String>(key).map(|files| {
files
.into_iter()
.map(|s| {
Expand All @@ -37,13 +37,12 @@ pub(crate) fn hashmaps_from_matches(

#[inline]
pub(crate) fn string_from_matches(matches: &ArgMatches, key: &str) -> Option<String> {
matches.value_of(key).map(|v| v.to_string())
matches.get_one::<String>(key).map(|v| v.to_string())
}

#[inline]
pub(crate) fn bool_from_matches(matches: &ArgMatches, key: &str) -> Option<bool> {
let occurrences = matches.occurrences_of(key);
if occurrences > 0 {
if matches.get_flag(key) == true {
Some(true)
} else {
None
Expand All @@ -52,7 +51,7 @@ pub(crate) fn bool_from_matches(matches: &ArgMatches, key: &str) -> Option<bool>

#[inline]
pub(crate) fn u32_from_matches(matches: &ArgMatches, key: &str) -> Option<u32> {
let occurrences = matches.occurrences_of(key);
let occurrences = matches.get_count(key);
if occurrences > 0 {
Some(occurrences as u32)
} else {
Expand Down
4 changes: 2 additions & 2 deletions kclvm/cmd/src/vet.rs
Expand Up @@ -6,8 +6,8 @@ use crate::util::string_from_matches;

/// Run the KCL vet command.
pub fn vet_command(matches: &ArgMatches) -> Result<()> {
let data_file = matches.value_of("data_file");
let kcl_file = matches.value_of("kcl_file");
let data_file = matches.get_one::<String>("data_file").map(|f| f.as_str());
let kcl_file = matches.get_one::<String>("kcl_file").map(|f| f.as_str());
match (data_file, kcl_file) {
(Some(data_file), Some(kcl_file)) => {
validate(ValidateOption::new(
Expand Down