taskwarrior/cli/src/cmd/mod.rs
Simon Fraser ee341ac7d5 subcommands: prepend and append to description
This currently uses format!() which may not be the best
option. Fixes #88
2020-12-17 12:59:50 +00:00

73 lines
1.7 KiB
Rust

use clap::{App, ArgMatches};
use failure::{Error, Fallible};
#[macro_use]
mod macros;
mod shared;
mod add;
mod append;
mod debug;
mod delete;
mod done;
mod gc;
mod info;
mod list;
mod modify;
mod pending;
mod prepend;
mod start;
mod stop;
mod sync;
/// Get a list of all subcommands in this crate
pub(crate) fn subcommands() -> Vec<Box<dyn SubCommand>> {
vec![
add::cmd(),
append::cmd(),
debug::cmd(),
delete::cmd(),
done::cmd(),
gc::cmd(),
info::cmd(),
list::cmd(),
modify::cmd(),
pending::cmd(),
prepend::cmd(),
start::cmd(),
stop::cmd(),
sync::cmd(),
]
}
/// The result of a [`crate::cmd::SubCommand::arg_match`] call
pub(crate) enum ArgMatchResult {
/// No match
None,
/// A good match
Ok(Box<dyn SubCommandInvocation>),
/// A match, but an issue with the command line
Err(Error),
}
/// A subcommand represents a defined subcommand, and is typically a singleton.
pub(crate) trait SubCommand {
/// Decorate the given [`clap::App`] appropriately for this subcommand
fn decorate_app<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a>;
/// If this ArgMatches is for this command, return an appropriate invocation.
fn arg_match<'a>(&self, matches: &ArgMatches<'a>) -> ArgMatchResult;
}
/// A subcommand invocation is specialized to a subcommand
pub(crate) trait SubCommandInvocation: std::fmt::Debug {
fn run(&self, command: &CommandInvocation) -> Fallible<()>;
// tests use downcasting, which requires a function to cast to Any
#[cfg(test)]
fn as_any(&self) -> &dyn std::any::Any;
}
pub use shared::CommandInvocation;