-
Notifications
You must be signed in to change notification settings - Fork 23
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
chore: bump up clap version #72
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#[macro_use] | ||
extern crate clap; | ||
|
||
use std::{convert::TryFrom, process, str}; | ||
|
@@ -21,17 +20,55 @@ pub(crate) mod config { | |
} | ||
|
||
pub(crate) fn build_commandline() -> AppConfig { | ||
let yaml = clap::load_yaml!("cli/compiler.yaml"); | ||
let matches = clap::App::from_yaml(yaml) | ||
//let yaml = clap::load_yaml!("cli/compiler.yaml"); | ||
let matches = clap::Command::new("Moleculec") | ||
.version(clap::crate_version!()) | ||
.about("Schema compiler for molecule.") | ||
.author("Nervos Core Dev <[email protected]>") | ||
.arg( | ||
clap::Arg::new("schema-file") | ||
.long("schema-file") | ||
.num_args(1) | ||
.help("Provide a schema file to compile.") | ||
.required(true) | ||
) | ||
.arg( | ||
clap::Arg::new("language") | ||
.long("language") | ||
.help("Specify a language, then generate source code for the specified language and output the generated code to the stdout. | ||
This parameter actually specifies a plugin to use. It should be a simple word, and the compiler will search for a plugin called \"moleculec-<language>\" in `$PATH`. | ||
If \"<language>\" is \"-\", the compiler will dump the intermediate data of schema to standard output.") | ||
.required(true) | ||
) | ||
.arg( | ||
clap::Arg::new("format") | ||
.long("format") | ||
.help("If \"<language>\" is \"-\", this parameter is used to specify a format for the intermediate data.") | ||
.value_parser( | ||
[ | ||
clap::builder::PossibleValue::new("json"), | ||
clap::builder::PossibleValue::new("yaml"), | ||
] | ||
) | ||
) | ||
.get_matches(); | ||
AppConfig::from(&matches) | ||
AppConfig::from(matches) | ||
} | ||
|
||
impl<'a> From<&'a clap::ArgMatches> for AppConfig { | ||
fn from(matches: &'a clap::ArgMatches) -> Self { | ||
let schema_file = value_t_or_exit!(matches, "schema-file", PathBuf); | ||
let language = value_t_or_exit!(matches, "language", String); | ||
impl From<clap::ArgMatches> for AppConfig { | ||
fn from(matches: clap::ArgMatches) -> Self { | ||
let schema_file = matches | ||
.get_one::<PathBuf>("schema-file") | ||
.unwrap_or_else(|| { | ||
eprintln!("Error: failed to get schema-file from command line args"); | ||
process::exit(1); | ||
}); | ||
|
||
let language = matches.get_one::<String>("language").unwrap_or_else(|| { | ||
eprintln!("Error: failed to get language from command line args"); | ||
process::exit(1); | ||
}); | ||
|
||
if !schema_file.as_path().is_file() { | ||
eprintln!( | ||
"Error: schema-file [{}] should be a file", | ||
|
@@ -40,7 +77,7 @@ pub(crate) mod config { | |
process::exit(1); | ||
} | ||
let output_config = if language == "-" { | ||
let format = value_t!(matches, "format", String).unwrap_or_else(|_| { | ||
let format = matches.get_one::<String>("format").unwrap_or_else(|| { | ||
eprintln!("Error: since language is \"-\", a format is required"); | ||
process::exit(1); | ||
}); | ||
|
@@ -52,7 +89,7 @@ pub(crate) mod config { | |
} | ||
} | ||
} else { | ||
if value_t!(matches, "format", String).is_ok() { | ||
if matches.get_one::<String>("format").is_none() { | ||
eprintln!("Error: since language is not \"-\", don't specify format"); | ||
process::exit(1); | ||
} | ||
|
@@ -65,7 +102,7 @@ pub(crate) mod config { | |
} | ||
}; | ||
Self { | ||
schema_file, | ||
schema_file: schema_file.to_path_buf(), | ||
output_config, | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,22 +16,27 @@ pub struct AppConfig { | |
format: IntermediateFormat, | ||
} | ||
|
||
type RawAppConfig<'a> = (Language, IntermediateFormat, &'a clap::ArgMatches); | ||
type RawAppConfig = (Language, IntermediateFormat, clap::ArgMatches); | ||
|
||
pub fn build_commandline(lang: Language, format: IntermediateFormat) -> AppConfig { | ||
let yaml = clap::load_yaml!("cli/compiler-plugin.yaml"); | ||
let matches = clap::App::from_yaml(yaml) | ||
.name(format!("Moleculec {} Plugin", lang)) | ||
//let yaml = clap::load_yaml!("cli/compiler-plugin.yaml"); | ||
let matches = clap::Command::new(format!("Moleculec {} Plugin", lang)) | ||
.author("Nervos Core Dev <[email protected]>") | ||
.about("Compiler plugin for molecule to generate code.") | ||
.version(clap::crate_version!()) | ||
.arg( | ||
clap::Arg::new("format") | ||
.long("format") | ||
.help("Output the supported format for the intermediate data."), | ||
) | ||
.get_matches(); | ||
AppConfig::from(&(lang, format, &matches)) | ||
AppConfig::from((lang, format, matches)) | ||
} | ||
|
||
impl<'a> From<&'a RawAppConfig<'a>> for AppConfig { | ||
fn from(input: &'a RawAppConfig<'a>) -> Self { | ||
impl From<RawAppConfig> for AppConfig { | ||
fn from(input: RawAppConfig) -> Self { | ||
let (lang, format, matches) = input; | ||
let action = if matches.is_present("format") { | ||
let action = if matches.get_flag("format") { | ||
AppAction::DisplayFormat | ||
} else { | ||
let mut input = Vec::new(); | ||
|
@@ -43,8 +48,8 @@ impl<'a> From<&'a RawAppConfig<'a>> for AppConfig { | |
}; | ||
Self { | ||
action, | ||
lang: *lang, | ||
format: *format, | ||
lang, | ||
format, | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MSRV in README is
1.41.01.56.1, we should set up matrix to test all minor versions since1.41.01.56.1, or update the MSRV in README