Skip to content

Commit

Permalink
Refactor of argument structures
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 7, 2019
1 parent b71a6cf commit b8805b4
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 310 deletions.
21 changes: 5 additions & 16 deletions src/bin/hal/cmd/address/create.rs
Expand Up @@ -5,22 +5,11 @@ use cmd;
use hal;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("create")
.about("create addresses")
.args(&cmd::args_networks())
.arg(cmd::arg_yaml())
.args(&[
clap::Arg::with_name("pubkey")
.long("pubkey")
.help("a public key in hex")
.takes_value(true)
.required(false),
clap::Arg::with_name("script")
.long("script")
.help("a script in hex")
.takes_value(true)
.required(false),
])
cmd::subcommand("create", "create addresses").args(&cmd::opts_networks()).args(&[
cmd::opt_yaml(),
cmd::opt("pubkey", "a public key in hex").takes_value(true).required(false),
cmd::opt("script", "a script in hex").takes_value(true).required(false),
])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
8 changes: 4 additions & 4 deletions src/bin/hal/cmd/address/inspect.rs
Expand Up @@ -6,10 +6,10 @@ use cmd;
use hal;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("inspect")
.about("inspect addresses")
.arg(cmd::arg_yaml())
.arg(clap::Arg::with_name("address").help("the address").takes_value(true).required(true))
cmd::subcommand("inspect", "inspect addresses").args(&[
cmd::opt_yaml(),
cmd::opt("address", "the address").takes_value(true).required(true),
])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
3 changes: 1 addition & 2 deletions src/bin/hal/cmd/address/mod.rs
Expand Up @@ -6,8 +6,7 @@ mod create;
mod inspect;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
cmd::new_subcommand("address")
.about("work with addresses")
cmd::subcommand_group("address", "work with addresses")
.subcommand(create::subcommand())
.subcommand(inspect::subcommand())
}
Expand Down
17 changes: 4 additions & 13 deletions src/bin/hal/cmd/bip32/derive.rs
Expand Up @@ -7,19 +7,10 @@ use std::str::FromStr;
use cmd;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("derive")
.about("derive keys from an extended key")
.arg(cmd::arg_yaml())
.args(&[
clap::Arg::with_name("ext-key")
.help("extended public or private key")
.takes_value(true)
.required(true),
clap::Arg::with_name("derivation-path")
.help("the derivation path")
.takes_value(true)
.required(true),
])
cmd::subcommand("derive", "derive keys from an extended key").arg(cmd::opt_yaml()).args(&[
cmd::opt("ext-key", "extended public or private key").takes_value(true).required(true),
cmd::opt("derivation-path", "the derivation path").takes_value(true).required(true),
])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/hal/cmd/bip32/mod.rs
Expand Up @@ -5,7 +5,7 @@ use cmd;
mod derive;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
cmd::new_subcommand("bip32").about("BIP-32 key derivation").subcommand(derive::subcommand())
cmd::subcommand_group("bip32", "BIP-32 key derivation").subcommand(derive::subcommand())
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
13 changes: 3 additions & 10 deletions src/bin/hal/cmd/ln/invoice/decode.rs
Expand Up @@ -5,16 +5,9 @@ use cmd;
use hal;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("decode")
.about("decode Lightning invoices")
.args(&cmd::args_networks())
.arg(cmd::arg_yaml())
.arg(
clap::Arg::with_name("invoice")
.help("the invoice in bech32")
.takes_value(true)
.required(true),
)
cmd::subcommand("decode", "decode Lightning invoices")
.args(&cmd::opts_networks())
.args(&[cmd::opt_yaml(), cmd::arg("invoice", "the invoice in bech32").required(true)])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
4 changes: 1 addition & 3 deletions src/bin/hal/cmd/ln/invoice/mod.rs
Expand Up @@ -5,9 +5,7 @@ use cmd;
mod decode;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
cmd::new_subcommand("invoice")
.about("handle Lightning invoices")
.subcommand(decode::subcommand())
cmd::subcommand_group("invoice", "handle Lightning invoices").subcommand(decode::subcommand())
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/hal/cmd/ln/mod.rs
Expand Up @@ -5,7 +5,7 @@ use cmd;
mod invoice;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
cmd::new_subcommand("ln").about("everything Lightning").subcommand(invoice::subcommand())
cmd::subcommand_group("ln", "everything Lightning").subcommand(invoice::subcommand())
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
44 changes: 39 additions & 5 deletions src/bin/hal/cmd/mod.rs
Expand Up @@ -7,15 +7,49 @@ pub mod tx;

use bitcoin::Network;

/// Create a new subcommand using the template that sets all the common settings.
pub fn new_subcommand<'a>(name: &'static str) -> clap::App<'a, 'a> {
/// Build a list of all built-in subcommands.
pub fn subcommands<'a>() -> Vec<clap::App<'a, 'a>> {
vec![
address::subcommand(),
ln::subcommand(),
tx::subcommand(),
psbt::subcommand(),
script::subcommand(),
bip32::subcommand(),
]
}

/// Construct a new command option.
pub fn opt<'a>(name: &'static str, help: &'static str) -> clap::Arg<'a, 'a> {
clap::Arg::with_name(name).long(name).help(help)
}

/// Construct a new positional argument.
pub fn arg<'a>(name: &'static str, help: &'static str) -> clap::Arg<'a, 'a> {
clap::Arg::with_name(name).help(help).takes_value(true)
}

/// Create a new subcommand group using the template that sets all the common settings.
/// This is not intended for actual commands, but for subcommands that host a bunch of other
/// subcommands.
pub fn subcommand_group<'a>(name: &'static str, about: &'static str) -> clap::App<'a, 'a> {
clap::SubCommand::with_name(name)
.about(about)
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.setting(clap::AppSettings::AllowExternalSubcommands)
//.setting(clap::AppSettings::AllowExternalSubcommands)
.setting(clap::AppSettings::DisableHelpSubcommand)
.setting(clap::AppSettings::VersionlessSubcommands)
}

/// Create a new subcommand using the template that sets all the common settings.
pub fn subcommand<'a>(name: &'static str, about: &'static str) -> clap::App<'a, 'a> {
clap::SubCommand::with_name(name)
.about(about)
.setting(clap::AppSettings::ArgRequiredElseHelp)
.setting(clap::AppSettings::DisableHelpSubcommand)
}

pub fn args_networks<'a>() -> Vec<clap::Arg<'a, 'a>> {
pub fn opts_networks<'a>() -> Vec<clap::Arg<'a, 'a>> {
vec![
clap::Arg::with_name("testnet")
.long("testnet")
Expand All @@ -41,7 +75,7 @@ pub fn network<'a>(matches: &clap::ArgMatches<'a>) -> Network {
}
}

pub fn arg_yaml<'a>() -> clap::Arg<'a, 'a> {
pub fn opt_yaml<'a>() -> clap::Arg<'a, 'a> {
clap::Arg::with_name("yaml")
.long("yaml")
.short("y")
Expand Down
35 changes: 12 additions & 23 deletions src/bin/hal/cmd/psbt/create.rs
Expand Up @@ -9,30 +9,19 @@ use bitcoin::consensus::{deserialize, serialize};
use bitcoin::util::psbt;
use bitcoin::Transaction;

use cmd;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("create")
.about("create a PSBT from an unsigned raw transaction")
.arg(
clap::Arg::with_name("raw-tx")
.help("the raw transaction in hex")
.takes_value(true)
.required(true),
)
.arg(
clap::Arg::with_name("output")
.long("output")
.short("o")
.help("where to save the merged PSBT output")
.takes_value(true)
.required(false),
)
.arg(
clap::Arg::with_name("raw-stdout")
.long("raw")
.short("r")
.help("output the raw bytes of the result to stdout")
.required(false),
)
cmd::subcommand("create", "create a PSBT from an unsigned raw transaction").args(&[
cmd::arg("raw-tx", "the raw transaction in hex").required(true),
cmd::opt("output", "where to save the merged PSBT output")
.short("o")
.takes_value(true)
.required(false),
cmd::opt("raw-stdout", "output the raw bytes of the result to stdout")
.short("r")
.required(false),
])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down
16 changes: 6 additions & 10 deletions src/bin/hal/cmd/psbt/decode.rs
Expand Up @@ -6,16 +6,12 @@ use bitcoin::util::psbt;
use cmd;

pub fn subcommand<'a>() -> clap::App<'a, 'a> {
clap::SubCommand::with_name("decode")
.about("decode a PSBT to JSON")
.args(&cmd::args_networks())
.arg(cmd::arg_yaml())
.arg(
clap::Arg::with_name("psbt")
.help("the PSBT file or raw PSBT in hex")
.takes_value(true)
.required(true),
)
cmd::subcommand("decode", "decode a PSBT to JSON").args(&cmd::opts_networks()).args(&[
cmd::opt_yaml(),
cmd::opt("psbt", "the PSBT file or raw PSBT in base64/hex")
.takes_value(true)
.required(true),
])
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand Down

0 comments on commit b8805b4

Please sign in to comment.