Browse Source

Command line to add and remove acccounts

master
Julio Biason 3 years ago
parent
commit
6a677f47a2
  1. 52
      src/args/mod.rs

52
src/args/mod.rs

@ -17,6 +17,8 @@
*/ */
use clap::App; use clap::App;
use clap::Arg;
use clap::ArgMatches;
use clap::SubCommand; use clap::SubCommand;
/// Possible commands /// Possible commands
@ -25,6 +27,10 @@ pub enum Command {
Unknown, Unknown,
/// Fetch all new favourites /// Fetch all new favourites
Fetch, Fetch,
/// Add a new account with the specified name
AddAccount(String),
/// Remove the account with the specified name
RemoveAccount(String),
} }
/// Parse the command line, returning the necessary command. /// Parse the command line, returning the necessary command.
@ -33,11 +39,55 @@ 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!())
.subcommand(SubCommand::with_name("fetch").about("Fetch the new favourites")); .subcommand(
SubCommand::with_name("fetch").about("Fetch the new favourites"),
)
.subcommand(
SubCommand::with_name("account")
.about("Manage Mastodon accounts")
.subcommand(
SubCommand::with_name("add")
.about("Add a new account")
.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"),
),
),
);
let matches = parser.get_matches(); let matches = parser.get_matches();
match matches.subcommand() { match matches.subcommand() {
("", _) => Command::Fetch, ("", _) => Command::Fetch,
("fetch", _) => Command::Fetch, ("fetch", _) => Command::Fetch,
("account", Some(arguments)) => parse_account(arguments),
_ => 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, _ => Command::Unknown,
} }
} }

Loading…
Cancel
Save