Add explicit Subcommand
enum (#484)
This commit is contained in:
parent
2938ab1561
commit
ab11740104
@ -44,9 +44,9 @@ pub(crate) use crate::{
|
||||
lexer::Lexer, output_error::OutputError, parameter::Parameter, parser::Parser,
|
||||
platform::Platform, position::Position, recipe::Recipe, recipe_context::RecipeContext,
|
||||
recipe_resolver::RecipeResolver, runtime_error::RuntimeError, search_error::SearchError,
|
||||
shebang::Shebang, state::State, string_literal::StringLiteral, token::Token,
|
||||
token_kind::TokenKind, use_color::UseColor, variables::Variables, verbosity::Verbosity,
|
||||
warning::Warning,
|
||||
shebang::Shebang, state::State, string_literal::StringLiteral, subcommand::Subcommand,
|
||||
token::Token, token_kind::TokenKind, use_color::UseColor, variables::Variables,
|
||||
verbosity::Verbosity, warning::Warning,
|
||||
};
|
||||
|
||||
pub(crate) type CompilationResult<'a, T> = Result<T, CompilationError<'a>>;
|
||||
|
@ -5,6 +5,7 @@ use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches};
|
||||
pub(crate) const DEFAULT_SHELL: &str = "sh";
|
||||
|
||||
pub(crate) struct Config<'a> {
|
||||
pub(crate) subcommand: Subcommand<'a>,
|
||||
pub(crate) dry_run: bool,
|
||||
pub(crate) evaluate: bool,
|
||||
pub(crate) highlight: bool,
|
||||
@ -16,6 +17,14 @@ pub(crate) struct Config<'a> {
|
||||
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> {
|
||||
pub(crate) fn app() -> App<'static, 'static> {
|
||||
let app = App::new(env!("CARGO_PKG_NAME"))
|
||||
@ -43,12 +52,12 @@ impl<'a> Config<'a> {
|
||||
.conflicts_with("QUIET"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("DUMP")
|
||||
Arg::with_name(arg::DUMP)
|
||||
.long("dump")
|
||||
.help("Print entire justfile"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("EDIT")
|
||||
Arg::with_name(arg::EDIT)
|
||||
.short("e")
|
||||
.long("edit")
|
||||
.help("Open justfile with $EDITOR"),
|
||||
@ -71,7 +80,7 @@ impl<'a> Config<'a> {
|
||||
.help("Use <JUSTFILE> as justfile."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("LIST")
|
||||
Arg::with_name(arg::LIST)
|
||||
.short("l")
|
||||
.long("list")
|
||||
.help("List available recipes and their arguments"),
|
||||
@ -100,7 +109,7 @@ impl<'a> Config<'a> {
|
||||
.help("Invoke <SHELL> to run recipes"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("SHOW")
|
||||
Arg::with_name(arg::SHOW)
|
||||
.short("s")
|
||||
.long("show")
|
||||
.takes_value(true)
|
||||
@ -108,7 +117,7 @@ impl<'a> Config<'a> {
|
||||
.help("Show information about <RECIPE>"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("SUMMARY")
|
||||
Arg::with_name(arg::SUMMARY)
|
||||
.long("summary")
|
||||
.help("List names of available recipes"),
|
||||
)
|
||||
@ -128,11 +137,11 @@ impl<'a> Config<'a> {
|
||||
.requires("JUSTFILE"),
|
||||
)
|
||||
.group(ArgGroup::with_name("EARLY-EXIT").args(&[
|
||||
"DUMP",
|
||||
"EDIT",
|
||||
"LIST",
|
||||
"SHOW",
|
||||
"SUMMARY",
|
||||
arg::DUMP,
|
||||
arg::EDIT,
|
||||
arg::LIST,
|
||||
arg::SHOW,
|
||||
arg::SUMMARY,
|
||||
"ARGUMENTS",
|
||||
"EVALUATE",
|
||||
]));
|
||||
@ -229,12 +238,27 @@ impl<'a> Config<'a> {
|
||||
})
|
||||
.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 {
|
||||
dry_run: matches.is_present("DRY-RUN"),
|
||||
evaluate: matches.is_present("EVALUATE"),
|
||||
highlight: matches.is_present("HIGHLIGHT"),
|
||||
quiet: matches.is_present("QUIET"),
|
||||
shell: matches.value_of("SHELL").unwrap(),
|
||||
subcommand,
|
||||
verbosity,
|
||||
color,
|
||||
overrides,
|
||||
@ -246,6 +270,7 @@ impl<'a> Config<'a> {
|
||||
impl<'a> Default for Config<'a> {
|
||||
fn default() -> Config<'static> {
|
||||
Config {
|
||||
subcommand: Subcommand::Run,
|
||||
dry_run: false,
|
||||
evaluate: false,
|
||||
highlight: false,
|
||||
|
@ -51,6 +51,7 @@ mod search_error;
|
||||
mod shebang;
|
||||
mod state;
|
||||
mod string_literal;
|
||||
mod subcommand;
|
||||
mod token;
|
||||
mod token_kind;
|
||||
mod use_color;
|
||||
|
12
src/run.rs
12
src/run.rs
@ -64,7 +64,7 @@ pub fn run() {
|
||||
|
||||
let text;
|
||||
if let (Some(justfile), Some(directory)) = (justfile, working_directory) {
|
||||
if matches.is_present("EDIT") {
|
||||
if config.subcommand == Subcommand::Edit {
|
||||
edit(justfile);
|
||||
}
|
||||
|
||||
@ -85,7 +85,7 @@ pub fn run() {
|
||||
};
|
||||
match search::justfile(¤t_dir) {
|
||||
Ok(name) => {
|
||||
if matches.is_present("EDIT") {
|
||||
if config.subcommand == Subcommand::Edit {
|
||||
edit(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 {
|
||||
eprintln!("Justfile contains no recipes.");
|
||||
} else {
|
||||
@ -138,12 +138,12 @@ pub fn run() {
|
||||
process::exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if matches.is_present("DUMP") {
|
||||
if config.subcommand == Subcommand::Dump {
|
||||
println!("{}", justfile);
|
||||
process::exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if matches.is_present("LIST") {
|
||||
if config.subcommand == Subcommand::List {
|
||||
// Construct a target to alias map.
|
||||
let mut recipe_aliases: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
|
||||
for alias in justfile.aliases.values() {
|
||||
@ -230,7 +230,7 @@ pub fn run() {
|
||||
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) {
|
||||
let recipe = justfile.get_recipe(alias.target).unwrap();
|
||||
println!("{}", alias);
|
||||
|
9
src/subcommand.rs
Normal file
9
src/subcommand.rs
Normal file
@ -0,0 +1,9 @@
|
||||
#[derive(PartialEq)]
|
||||
pub(crate) enum Subcommand<'a> {
|
||||
Edit,
|
||||
Summary,
|
||||
Dump,
|
||||
List,
|
||||
Show { name: &'a str },
|
||||
Run,
|
||||
}
|
Loading…
Reference in New Issue
Block a user