Browse Source

Refactored the command line interface

master
Julio Biason 3 years ago
parent
commit
fdf274fb45
  1. 98
      src/args/mod.rs
  2. 6
      src/main.rs

98
src/args/mod.rs

@ -18,17 +18,18 @@
use clap::App; use clap::App;
use clap::Arg; use clap::Arg;
use clap::ArgMatches;
use clap::SubCommand; use clap::SubCommand;
/// Possible commands /// Possible commands
pub enum Command { pub enum Command {
/// Got a command that we don't know /// Got a command that we don't know
Unknown, Unknown,
/// Fetch all new favourites /// Fetch favourites from all accounts
Fetch, FetchAll,
/// Fetch favourites from a specific account
Fetch(String),
/// Add a new account with the specified name /// Add a new account with the specified name
AddAccount(String), CreateAccount(String),
/// Remove the account with the specified name /// Remove the account with the specified name
RemoveAccount(String), RemoveAccount(String),
} }
@ -39,73 +40,42 @@ pub fn parse() -> Command {
.version(clap::crate_version!()) .version(clap::crate_version!())
.author(clap::crate_authors!()) .author(clap::crate_authors!())
.about(clap::crate_description!()) .about(clap::crate_description!())
.arg(Arg::with_name("account").help("Account alias"))
.subcommand( .subcommand(
SubCommand::with_name("fetch").about("Fetch the new favourites"), SubCommand::with_name("create").about("Create the account"),
) )
.subcommand( .subcommand(
SubCommand::with_name("account") SubCommand::with_name("remove").about("Remove the account"),
.about("Manage Mastodon accounts") )
.subcommand( .subcommand(
SubCommand::with_name("add") SubCommand::with_name("fetch")
.about("Add a new account") .about("Fetch new favourites from this account only")
.arg(
Arg::with_name("name")
.required(true)
.takes_value(true)
.help("Account name"),
),
)
.subcommand(
SubCommand::with_name("remove")
.about("Remove an account")
.arg(
Arg::with_name("name")
.required(true)
.takes_value(true)
.help("Name of the account to be removed"),
),
),
) )
.subcommand( .subcommand(
SubCommand::with_name("storage") SubCommand::with_name("storage")
.about("Storage management") .about("Account storage")
.arg( .subcommand(SubCommand::with_name("add")
Arg::with_name("account") .about("Add a new storage for the account")
.required(true) .arg(Arg::with_name("type")
.takes_value(true) .help("Storage type; valid types are: \"filesystem\"")
.help("Account name"), .takes_value(true)
) .required(true)))
.subcommand( .subcommand(SubCommand::with_name("remove")
SubCommand::with_name("add") .about("Remove a storage from the account")
.about("Add a storage for an account") .arg(Arg::with_name("type")
.subcommand(SubCommand::with_name("filesystem") .help("Storage type to be removed")
.about("Stores toots in the filesystem") .takes_value(true)
.arg(Arg::with_name("path") .required(true))));
.required(true)
.takes_value(true)
.help("Path where store toots in the filesystem")))));
let matches = parser.get_matches(); let matches = parser.get_matches();
match matches.subcommand() { if let Some(account) = matches.value_of("account") {
("", _) => Command::Fetch, match matches.subcommand() {
("fetch", _) => Command::Fetch, ("fetch", _) => Command::Fetch(account.into()),
("account", Some(arguments)) => parse_account(arguments), ("create", _) => Command::CreateAccount(account.into()),
_ => Command::Unknown, ("remove", _) => Command::RemoveAccount(account.into()),
} _ => Command::Unknown,
}
fn parse_account(arguments: &ArgMatches) -> Command {
log::debug!("Parsing accounts");
match arguments.subcommand() {
("add", Some(argument)) => {
log::debug!("Must add new account");
let name = argument.value_of("name").unwrap();
Command::AddAccount(name.into())
}
("remove", Some(argument)) => {
log::debug!("Must remove account");
let name = argument.value_of("name").unwrap();
Command::RemoveAccount(name.into())
} }
_ => Command::Unknown, } else {
log::debug!("No account provided, assuming fetch");
Command::FetchAll
} }
} }

6
src/main.rs

@ -36,8 +36,10 @@ fn main() {
env_logger::init(); env_logger::init();
match args::parse() { match args::parse() {
args::Command::Fetch => fetch_favourites(), args::Command::FetchAll => fetch_favourites(),
args::Command::AddAccount(account_name) => add_account(&account_name), args::Command::CreateAccount(account_name) => {
add_account(&account_name)
}
_ => println!("Unknown command"), _ => println!("Unknown command"),
} }
} }

Loading…
Cancel
Save