Add explicit Subcommand enum (#484)

This commit is contained in:
Casey Rodarmor 2019-10-07 04:04:39 -07:00 committed by GitHub
parent 2938ab1561
commit ab11740104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 19 deletions

View File

@ -44,9 +44,9 @@ pub(crate) use crate::{
lexer::Lexer, output_error::OutputError, parameter::Parameter, parser::Parser, lexer::Lexer, output_error::OutputError, parameter::Parameter, parser::Parser,
platform::Platform, position::Position, recipe::Recipe, recipe_context::RecipeContext, platform::Platform, position::Position, recipe::Recipe, recipe_context::RecipeContext,
recipe_resolver::RecipeResolver, runtime_error::RuntimeError, search_error::SearchError, recipe_resolver::RecipeResolver, runtime_error::RuntimeError, search_error::SearchError,
shebang::Shebang, state::State, string_literal::StringLiteral, token::Token, shebang::Shebang, state::State, string_literal::StringLiteral, subcommand::Subcommand,
token_kind::TokenKind, use_color::UseColor, variables::Variables, verbosity::Verbosity, token::Token, token_kind::TokenKind, use_color::UseColor, variables::Variables,
warning::Warning, verbosity::Verbosity, warning::Warning,
}; };
pub(crate) type CompilationResult<'a, T> = Result<T, CompilationError<'a>>; pub(crate) type CompilationResult<'a, T> = Result<T, CompilationError<'a>>;

View File

@ -5,6 +5,7 @@ use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches};
pub(crate) const DEFAULT_SHELL: &str = "sh"; pub(crate) const DEFAULT_SHELL: &str = "sh";
pub(crate) struct Config<'a> { pub(crate) struct Config<'a> {
pub(crate) subcommand: Subcommand<'a>,
pub(crate) dry_run: bool, pub(crate) dry_run: bool,
pub(crate) evaluate: bool, pub(crate) evaluate: bool,
pub(crate) highlight: bool, pub(crate) highlight: bool,
@ -16,6 +17,14 @@ pub(crate) struct Config<'a> {
pub(crate) arguments: Vec<&'a str>, pub(crate) arguments: Vec<&'a str>,
} }
mod arg {
pub(crate) const EDIT: &str = "EDIT";
pub(crate) const SUMMARY: &str = "SUMMARY";
pub(crate) const DUMP: &str = "DUMP";
pub(crate) const LIST: &str = "LIST";
pub(crate) const SHOW: &str = "SHOW";
}
impl<'a> Config<'a> { impl<'a> Config<'a> {
pub(crate) fn app() -> App<'static, 'static> { pub(crate) fn app() -> App<'static, 'static> {
let app = App::new(env!("CARGO_PKG_NAME")) let app = App::new(env!("CARGO_PKG_NAME"))
@ -43,12 +52,12 @@ impl<'a> Config<'a> {
.conflicts_with("QUIET"), .conflicts_with("QUIET"),
) )
.arg( .arg(
Arg::with_name("DUMP") Arg::with_name(arg::DUMP)
.long("dump") .long("dump")
.help("Print entire justfile"), .help("Print entire justfile"),
) )
.arg( .arg(
Arg::with_name("EDIT") Arg::with_name(arg::EDIT)
.short("e") .short("e")
.long("edit") .long("edit")
.help("Open justfile with $EDITOR"), .help("Open justfile with $EDITOR"),
@ -71,7 +80,7 @@ impl<'a> Config<'a> {
.help("Use <JUSTFILE> as justfile."), .help("Use <JUSTFILE> as justfile."),
) )
.arg( .arg(
Arg::with_name("LIST") Arg::with_name(arg::LIST)
.short("l") .short("l")
.long("list") .long("list")
.help("List available recipes and their arguments"), .help("List available recipes and their arguments"),
@ -100,7 +109,7 @@ impl<'a> Config<'a> {
.help("Invoke <SHELL> to run recipes"), .help("Invoke <SHELL> to run recipes"),
) )
.arg( .arg(
Arg::with_name("SHOW") Arg::with_name(arg::SHOW)
.short("s") .short("s")
.long("show") .long("show")
.takes_value(true) .takes_value(true)
@ -108,7 +117,7 @@ impl<'a> Config<'a> {
.help("Show information about <RECIPE>"), .help("Show information about <RECIPE>"),
) )
.arg( .arg(
Arg::with_name("SUMMARY") Arg::with_name(arg::SUMMARY)
.long("summary") .long("summary")
.help("List names of available recipes"), .help("List names of available recipes"),
) )
@ -128,11 +137,11 @@ impl<'a> Config<'a> {
.requires("JUSTFILE"), .requires("JUSTFILE"),
) )
.group(ArgGroup::with_name("EARLY-EXIT").args(&[ .group(ArgGroup::with_name("EARLY-EXIT").args(&[
"DUMP", arg::DUMP,
"EDIT", arg::EDIT,
"LIST", arg::LIST,
"SHOW", arg::SHOW,
"SUMMARY", arg::SUMMARY,
"ARGUMENTS", "ARGUMENTS",
"EVALUATE", "EVALUATE",
])); ]));
@ -229,12 +238,27 @@ impl<'a> Config<'a> {
}) })
.collect::<Vec<&str>>(); .collect::<Vec<&str>>();
let subcommand = if matches.is_present(arg::EDIT) {
Subcommand::Edit
} else if matches.is_present(arg::SUMMARY) {
Subcommand::Summary
} else if matches.is_present(arg::DUMP) {
Subcommand::Dump
} else if matches.is_present(arg::LIST) {
Subcommand::List
} else if let Some(name) = matches.value_of(arg::SHOW) {
Subcommand::Show { name }
} else {
Subcommand::Run
};
Config { Config {
dry_run: matches.is_present("DRY-RUN"), dry_run: matches.is_present("DRY-RUN"),
evaluate: matches.is_present("EVALUATE"), evaluate: matches.is_present("EVALUATE"),
highlight: matches.is_present("HIGHLIGHT"), highlight: matches.is_present("HIGHLIGHT"),
quiet: matches.is_present("QUIET"), quiet: matches.is_present("QUIET"),
shell: matches.value_of("SHELL").unwrap(), shell: matches.value_of("SHELL").unwrap(),
subcommand,
verbosity, verbosity,
color, color,
overrides, overrides,
@ -246,6 +270,7 @@ impl<'a> Config<'a> {
impl<'a> Default for Config<'a> { impl<'a> Default for Config<'a> {
fn default() -> Config<'static> { fn default() -> Config<'static> {
Config { Config {
subcommand: Subcommand::Run,
dry_run: false, dry_run: false,
evaluate: false, evaluate: false,
highlight: false, highlight: false,

View File

@ -51,6 +51,7 @@ mod search_error;
mod shebang; mod shebang;
mod state; mod state;
mod string_literal; mod string_literal;
mod subcommand;
mod token; mod token;
mod token_kind; mod token_kind;
mod use_color; mod use_color;

View File

@ -64,7 +64,7 @@ pub fn run() {
let text; let text;
if let (Some(justfile), Some(directory)) = (justfile, working_directory) { if let (Some(justfile), Some(directory)) = (justfile, working_directory) {
if matches.is_present("EDIT") { if config.subcommand == Subcommand::Edit {
edit(justfile); edit(justfile);
} }
@ -85,7 +85,7 @@ pub fn run() {
}; };
match search::justfile(&current_dir) { match search::justfile(&current_dir) {
Ok(name) => { Ok(name) => {
if matches.is_present("EDIT") { if config.subcommand == Subcommand::Edit {
edit(name); edit(name);
} }
text = fs::read_to_string(&name) text = fs::read_to_string(&name)
@ -121,7 +121,7 @@ pub fn run() {
} }
} }
if matches.is_present("SUMMARY") { if config.subcommand == Subcommand::Summary {
if justfile.count() == 0 { if justfile.count() == 0 {
eprintln!("Justfile contains no recipes."); eprintln!("Justfile contains no recipes.");
} else { } else {
@ -138,12 +138,12 @@ pub fn run() {
process::exit(EXIT_SUCCESS); process::exit(EXIT_SUCCESS);
} }
if matches.is_present("DUMP") { if config.subcommand == Subcommand::Dump {
println!("{}", justfile); println!("{}", justfile);
process::exit(EXIT_SUCCESS); process::exit(EXIT_SUCCESS);
} }
if matches.is_present("LIST") { if config.subcommand == Subcommand::List {
// Construct a target to alias map. // Construct a target to alias map.
let mut recipe_aliases: BTreeMap<&str, Vec<&str>> = BTreeMap::new(); let mut recipe_aliases: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
for alias in justfile.aliases.values() { for alias in justfile.aliases.values() {
@ -230,7 +230,7 @@ pub fn run() {
process::exit(EXIT_SUCCESS); process::exit(EXIT_SUCCESS);
} }
if let Some(name) = matches.value_of("SHOW") { if let Subcommand::Show { name } = config.subcommand {
if let Some(alias) = justfile.get_alias(name) { if let Some(alias) = justfile.get_alias(name) {
let recipe = justfile.get_recipe(alias.target).unwrap(); let recipe = justfile.get_recipe(alias.target).unwrap();
println!("{}", alias); println!("{}", alias);

9
src/subcommand.rs Normal file
View File

@ -0,0 +1,9 @@
#[derive(PartialEq)]
pub(crate) enum Subcommand<'a> {
Edit,
Summary,
Dump,
List,
Show { name: &'a str },
Run,
}