122 lines
4.1 KiB
Rust
122 lines
4.1 KiB
Rust
//! Command-line interface for sovereign_browser
|
|
//!
|
|
//! Port of `cli.c` / `cli.h` from the C project.
|
|
|
|
use crate::key_store::KeyStoreMethod;
|
|
use crate::version;
|
|
|
|
/// Parsed CLI arguments.
|
|
#[derive(Debug, Default)]
|
|
pub struct CliArgs {
|
|
pub urls: Vec<String>,
|
|
pub login_method: Option<KeyStoreMethod>,
|
|
pub privkey: Option<String>,
|
|
pub pubkey: Option<String>,
|
|
pub mnemonic: Option<String>,
|
|
pub bunker_url: Option<String>,
|
|
pub signer_transport: Option<String>,
|
|
pub signer_device: Option<String>,
|
|
pub signer_index: Option<i32>,
|
|
pub profile: Option<String>,
|
|
pub no_agent: bool,
|
|
pub no_session: bool,
|
|
pub no_login: bool,
|
|
pub help: bool,
|
|
pub version: bool,
|
|
}
|
|
|
|
/// Parse command-line arguments.
|
|
pub fn cli_parse() -> CliArgs {
|
|
let args: Vec<String> = std::env::args().collect();
|
|
let mut cli = CliArgs::default();
|
|
let mut i = 1;
|
|
|
|
while i < args.len() {
|
|
match args[i].as_str() {
|
|
"--help" | "-h" => cli.help = true,
|
|
"--version" | "-v" => cli.version = true,
|
|
"--local" | "-l" => {
|
|
cli.login_method = Some(KeyStoreMethod::Local);
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.privkey = Some(args[i].clone());
|
|
}
|
|
}
|
|
"--pubkey" | "-p" => {
|
|
cli.login_method = Some(KeyStoreMethod::Readonly);
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.pubkey = Some(args[i].clone());
|
|
}
|
|
}
|
|
"--seed" | "-s" => {
|
|
cli.login_method = Some(KeyStoreMethod::Seed);
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.mnemonic = Some(args[i].clone());
|
|
}
|
|
}
|
|
"--bunker" | "-b" => {
|
|
cli.login_method = Some(KeyStoreMethod::Nip46);
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.bunker_url = Some(args[i].clone());
|
|
}
|
|
}
|
|
"--signer" => {
|
|
cli.login_method = Some(KeyStoreMethod::Signer);
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.signer_device = Some(args[i].clone());
|
|
}
|
|
if i + 1 < args.len() && !args[i + 1].starts_with('-') {
|
|
i += 1;
|
|
cli.signer_index = Some(args[i].parse().unwrap_or(0));
|
|
}
|
|
}
|
|
"--profile" => {
|
|
if i + 1 < args.len() {
|
|
i += 1;
|
|
cli.profile = Some(args[i].clone());
|
|
}
|
|
}
|
|
"--no-agent" => cli.no_agent = true,
|
|
"--no-session" => cli.no_session = true,
|
|
"--no-login" => cli.no_login = true,
|
|
_ => {
|
|
if !args[i].starts_with('-') {
|
|
cli.urls.push(args[i].clone());
|
|
}
|
|
}
|
|
}
|
|
i += 1;
|
|
}
|
|
|
|
cli
|
|
}
|
|
|
|
/// Print help message.
|
|
pub fn cli_print_help() {
|
|
println!("sovereign_browser — WebKitGTK + Rust");
|
|
println!();
|
|
println!("USAGE:");
|
|
println!(" sovereign_browser [OPTIONS] [URLS...]");
|
|
println!();
|
|
println!("OPTIONS:");
|
|
println!(" -h, --help Show this help message");
|
|
println!(" -v, --version Show version");
|
|
println!(" -l, --local [KEY] Login with local nsec key");
|
|
println!(" -p, --pubkey [KEY] Read-only mode with npub");
|
|
println!(" -s, --seed [PHRASE] Login with BIP-39 seed phrase");
|
|
println!(" -b, --bunker [URL] Login with NIP-46 bunker URL");
|
|
println!(" --signer [DEVICE] [IDX] Login with remote signer");
|
|
println!(" --profile [PUBKEY] Use specific profile");
|
|
println!(" --no-agent Disable agent server");
|
|
println!(" --no-session Disable session restore");
|
|
}
|
|
|
|
/// Print version.
|
|
pub fn cli_print_version() {
|
|
println!("sovereign_browser {}", version::VERSION);
|
|
}
|