-
Notifications
You must be signed in to change notification settings - Fork 117
feat(use-clap-for-cli): use clap to parse CLI-Args #2215 #2510
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
Changes from all commits
f56131c
7074d52
726791a
2d62cee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,10 +58,7 @@ use std::sync::atomic::Ordering; | |
| use gstuff::slurp; | ||
| use serde_json::{self as json, Value as Json}; | ||
|
|
||
| use std::env; | ||
| use std::ffi::OsString; | ||
| use std::process::exit; | ||
| use std::ptr::null; | ||
| use std::str; | ||
|
|
||
| pub use self::lp_native_dex::init_hw; | ||
|
|
@@ -84,11 +81,40 @@ pub mod rpc; | |
| mod swap_versioning; | ||
| #[cfg(all(target_arch = "wasm32", test))] mod wasm_tests; | ||
|
|
||
| use clap::Parser; | ||
|
|
||
| pub const PASSWORD_MAXIMUM_CONSECUTIVE_CHARACTERS: usize = 3; | ||
|
|
||
| #[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))] | ||
| const CUSTOM_PAYMENT_LOCKTIME_DEFAULT: u64 = 900; | ||
|
|
||
| const EXTRA_HELP_MESSAGE: &str = r#" | ||
| Environment variables: | ||
|
|
||
| MM_CONF_PATH .. File path. MM2 will try to load the JSON configuration from this file. | ||
| File must contain valid json with structure mentioned above. | ||
| Defaults to `MM2.json` | ||
| MM_COINS_PATH .. File path. MM2 will try to load coins data from this file. | ||
| File must contain valid json. | ||
| Recommended: https://github.com/komodoplatform/coins/blob/master/coins. | ||
| Defaults to `coins`. | ||
| MM_LOG .. File path. Must end with '.log'. MM will log to this file. | ||
|
|
||
| See also the online documentation at | ||
| https://komodoplatform.com/en/docs | ||
| "#; | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| #[command(about="Komodo DeFi Framework Daemon", long_about=None, after_help=EXTRA_HELP_MESSAGE)] | ||
| pub struct Cli { | ||
| /// JSON configuration string - will be used instead of the json config file | ||
| pub config: Option<String>, | ||
|
|
||
| /// Print version | ||
| #[clap(short, long)] | ||
| pub version: bool, | ||
| } | ||
|
|
||
| pub struct LpMainParams { | ||
| conf: Json, | ||
| filter: Option<LogLevel>, | ||
|
|
@@ -201,115 +227,32 @@ fn spawn_ctrl_c_handler(ctx: MmArc) { | |
| }); | ||
| } | ||
|
|
||
| fn help() { | ||
| const HELP_MSG: &str = r#"Command-line options. | ||
| The first command-line argument is special and designates the mode. | ||
|
|
||
| help .. Display this message. | ||
| btc2kmd {WIF or BTC} .. Convert a BTC WIF into a KMD WIF. | ||
| events .. Listen to a feed coming from a separate MM daemon and print it to stdout. | ||
| vanity {substring} .. Tries to find an address with the given substring. | ||
| update_config {SRC} {DST} .. Update the configuration of coins from the SRC config and save it to DST file. | ||
| {JSON configuration} .. Run the MarketMaker daemon. | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these modes or sub commands weren't handled any were except for passing a JSON configuration |
||
| Some (but not all) of the JSON configuration parameters (* - required): | ||
|
|
||
| NB: The 'coins' command-line configuration must have the lowercased coin names in the 'name' field, | ||
| {"coins": [{"name": "dash", "coin": "DASH", ...}, ...], ...}. | ||
| coins .. Information about the currencies: their ticker symbols, names, ports, addresses, etc. | ||
| If the field isn't present on the command line then we try loading it from the 'coins' file. | ||
| dbdir .. MM database path. 'DB' by default. | ||
| gui .. The information about GUI app using KDF instance. Included in swap statuses shared with network. | ||
| .. It's recommended to put essential info to this field (application name, OS, version, etc). | ||
| .. e.g. AtomicDEX iOS 1.0.1000. | ||
| myipaddr .. IP address to bind to for P2P networking. | ||
| netid .. Subnetwork. Affects ports and keys. | ||
| passphrase * .. Wallet seed. | ||
| Compressed WIFs and hexadecimal ECDSA keys (prefixed with 0x) are also accepted. | ||
| rpccors .. Access-Control-Allow-Origin header value to be used in all the RPC responses. | ||
| Default is currently 'http://localhost:3000' | ||
| rpcip .. IP address to bind to for RPC server. Overrides the 127.0.0.1 default | ||
| rpc_password .. RPC password used to authorize non-public RPC calls | ||
| MM generates password from passphrase if this field is not set | ||
| rpc_local_only .. MM forbids some RPC requests from not loopback (localhost) IPs as additional security measure. | ||
| Defaults to `true`, set `false` to disable. `Use with caution`. | ||
| rpcport .. If > 1000 overrides the 7783 default. | ||
| i_am_seed .. Activate the seed node mode (acting as a relay for kdf clients). | ||
| Defaults to `false`. | ||
| seednodes .. Seednode IPs that node will use. | ||
| At least one seed IP must be present if the node is not a seed itself. | ||
| wif .. `1` to add WIFs to the information we provide about a coin. | ||
|
|
||
| Environment variables: | ||
|
|
||
| MM_CONF_PATH .. File path. MM2 will try to load the JSON configuration from this file. | ||
| File must contain valid json with structure mentioned above. | ||
| Defaults to `MM2.json` | ||
| MM_COINS_PATH .. File path. MM2 will try to load coins data from this file. | ||
| File must contain valid json. | ||
| Recommended: https://github.com/komodoplatform/coins/blob/master/coins. | ||
| Defaults to `coins`. | ||
| MM_LOG .. File path. Must end with '.log'. MM will log to this file. | ||
|
|
||
| See also the online documentation at | ||
| https://komodoplatform.com/en/docs | ||
| "#; | ||
|
|
||
| println!("{}", HELP_MSG); | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| #[allow(dead_code)] // Not used by mm2_lib. | ||
| pub fn mm2_main(version: String, datetime: String) { | ||
| use libc::c_char; | ||
|
|
||
| init_crash_reports(); | ||
|
|
||
| // Temporarily simulate `argv[]` for the C version of the main method. | ||
| let args: Vec<String> = env::args() | ||
| .map(|mut arg| { | ||
| arg.push('\0'); | ||
| arg | ||
| }) | ||
| .collect(); | ||
| let mut args: Vec<*const c_char> = args.iter().map(|s| s.as_ptr() as *const c_char).collect(); | ||
| args.push(null()); | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wasn't really sure if that part still relevant, It might have been there for that "C version of the main method" but I couldn't find it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was migrated long time ago and it's not relevant anymore. |
||
| let args_os: Vec<OsString> = env::args_os().collect(); | ||
|
|
||
| // NB: The first argument is special, being used as the mode switcher. | ||
| // The other arguments might be used to pass the data to the various MM modes, | ||
| // we're not checking them for the mode switches in order not to risk [untrusted] data being mistaken for a mode switch. | ||
| let first_arg = args_os.get(1).and_then(|arg| arg.to_str()); | ||
|
|
||
| if first_arg == Some("--version") || first_arg == Some("-v") || first_arg == Some("version") { | ||
| let cli = Cli::parse(); | ||
| if cli.version { | ||
| println!("Komodo DeFi Framework: {version}"); | ||
| return; | ||
| } | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to keep the version command showing the git commit or KDF_VERSION as before, but couldn't do it through the derived #[cfg(not(target_arch = "wasm32"))]
const KDF_VERSION: &str = env!("KDF_VERSION");so i went for this approach instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in 726791a |
||
| if first_arg == Some("--help") || first_arg == Some("-h") || first_arg == Some("help") { | ||
| help(); | ||
| return; | ||
| } | ||
|
|
||
| if cfg!(windows) && first_arg == Some("/?") { | ||
| help(); | ||
| return; | ||
| } | ||
| let json_config = cli.config.as_deref(); | ||
|
|
||
| log!("Komodo DeFi Framework {} DT {}", version, datetime); | ||
|
|
||
| if let Err(err) = run_lp_main(first_arg, &|_| (), version, datetime) { | ||
| if let Err(err) = run_lp_main(json_config, &|_| (), version, datetime) { | ||
| log!("{}", err); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_arch = "wasm32"))] | ||
| /// Parses and returns the `first_arg` as JSON. | ||
| /// Attempts to load the config from `MM2.json` file if `first_arg` is None | ||
| pub fn get_mm2config(first_arg: Option<&str>) -> Result<Json, String> { | ||
| let conf = match first_arg { | ||
| /// Parses and returns the `json_config` as JSON. | ||
| /// Attempts to load the config from `MM2.json` file if `json_config` is None | ||
| pub fn get_mm2config(json_config: Option<&str>) -> Result<Json, String> { | ||
| let conf = match json_config { | ||
| Some(s) => s.to_owned(), | ||
| None => { | ||
| let conf_path = common::kdf_config_file().map_err(|e| e.to_string())?; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
I used
after_helphere to show the extra help message as it was before, but I suggest that we can use a flag like-hhso the user can control when to show these extra help messages