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

[apps/cli] set defaults for fee-amount, fee-token and gas-limit tx args #667

Merged
merged 1 commit into from
Dec 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,14 @@ pub mod args {
const DONT_ARCHIVE: ArgFlag = flag("dont-archive");
const DRY_RUN_TX: ArgFlag = flag("dry-run");
const EPOCH: ArgOpt<Epoch> = arg_opt("epoch");
const FEE_AMOUNT: Arg<token::Amount> = arg("fee-amount");
const FEE_TOKEN: Arg<WalletAddress> = arg("fee-token");
const FEE_AMOUNT: ArgDefault<token::Amount> =
arg_default("fee-amount", DefaultFn(|| token::Amount::from(0)));
const FEE_TOKEN: ArgDefaultFromCtx<WalletAddress> =
arg_default_from_ctx("fee-token", DefaultFn(|| "XAN".into()));
const FILTER_PATH: ArgOpt<PathBuf> = arg_opt("filter-path");
const FORCE: ArgFlag = flag("force");
const GAS_LIMIT: Arg<token::Amount> = arg("gas-limit");
const GAS_LIMIT: ArgDefault<token::Amount> =
arg_default("gas-limit", DefaultFn(|| token::Amount::from(0)));
const GENESIS_PATH: Arg<PathBuf> = arg("genesis-path");
const LEDGER_ADDRESS_ABOUT: &str =
"Address of a ledger node as \"{scheme}://{host}:{port}\". If the \
Expand Down
35 changes: 35 additions & 0 deletions apps/src/lib/cli/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ pub struct ArgDefault<T> {
pub r#type: PhantomData<T>,
}

pub struct ArgDefaultFromCtx<T> {
pub name: &'static str,
pub default: DefaultFn<String>,
pub r#type: PhantomData<T>,
}

/// This wrapper type is a workaround for "function pointers in const fn are
/// unstable", which allows us to use this type in a const fn, because the
/// type-checker doesn't inspect the wrapped type.
Expand Down Expand Up @@ -100,6 +106,17 @@ pub const fn arg_default<T>(
}
}

pub const fn arg_default_from_ctx<T>(
name: &'static str,
default: DefaultFn<String>,
) -> ArgDefaultFromCtx<T> {
ArgDefaultFromCtx {
name,
default,
r#type: PhantomData,
}
}

pub const fn flag(name: &'static str) -> ArgFlag {
ArgFlag { name }
}
Expand Down Expand Up @@ -201,6 +218,24 @@ where
}
}

impl<T> ArgDefaultFromCtx<FromContext<T>>
where
T: FromStr,
<T as FromStr>::Err: Debug,
{
pub fn def(&self) -> ClapArg {
ClapArg::new(self.name).long(self.name).takes_value(true)
}

pub fn parse(&self, matches: &ArgMatches) -> FromContext<T> {
let raw = parse_opt(matches, self.name).unwrap_or_else(|| {
let DefaultFn(default) = self.default;
default()
});
FromContext::new(raw)
}
}

impl ArgFlag {
pub fn def(&self) -> ClapArg {
ClapArg::new(self.name).long(self.name).takes_value(false)
Expand Down