Treat any bare word in the command line as a report name
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
use super::args::*;
|
||||
use super::{
|
||||
ArgList, Column, DescriptionMod, Filter, Modification, Property, Report, Sort, SortBy,
|
||||
};
|
||||
use super::{ArgList, DescriptionMod, Filter, Modification};
|
||||
use crate::usage;
|
||||
use nom::{branch::alt, combinator::*, sequence::*, IResult};
|
||||
use taskchampion::Status;
|
||||
@@ -39,8 +37,12 @@ pub(crate) enum Subcommand {
|
||||
},
|
||||
|
||||
/// Lists (reports)
|
||||
List {
|
||||
report: Report,
|
||||
Report {
|
||||
/// The name of the report to show
|
||||
report_name: String,
|
||||
|
||||
/// Additional filter terms beyond those in the report
|
||||
filter: Filter,
|
||||
},
|
||||
|
||||
/// Per-task information (typically one task)
|
||||
@@ -56,16 +58,17 @@ pub(crate) enum Subcommand {
|
||||
|
||||
impl Subcommand {
|
||||
pub(super) fn parse(input: ArgList) -> IResult<ArgList, Subcommand> {
|
||||
alt((
|
||||
all_consuming(alt((
|
||||
Version::parse,
|
||||
Help::parse,
|
||||
Add::parse,
|
||||
Modify::parse,
|
||||
List::parse,
|
||||
Info::parse,
|
||||
Gc::parse,
|
||||
Sync::parse,
|
||||
))(input)
|
||||
// This must come last since it accepts arbitrary report names
|
||||
Report::parse,
|
||||
)))(input)
|
||||
}
|
||||
|
||||
pub(super) fn get_usage(u: &mut usage::Usage) {
|
||||
@@ -73,10 +76,10 @@ impl Subcommand {
|
||||
Help::get_usage(u);
|
||||
Add::get_usage(u);
|
||||
Modify::get_usage(u);
|
||||
List::get_usage(u);
|
||||
Info::get_usage(u);
|
||||
Gc::get_usage(u);
|
||||
Sync::get_usage(u);
|
||||
Report::get_usage(u);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,59 +254,43 @@ impl Modify {
|
||||
}
|
||||
}
|
||||
|
||||
struct List;
|
||||
|
||||
impl List {
|
||||
// temporary
|
||||
fn default_report() -> Report {
|
||||
Report {
|
||||
columns: vec![
|
||||
Column {
|
||||
label: "Id".to_owned(),
|
||||
property: Property::Id,
|
||||
},
|
||||
Column {
|
||||
label: "Description".to_owned(),
|
||||
property: Property::Description,
|
||||
},
|
||||
Column {
|
||||
label: "Active".to_owned(),
|
||||
property: Property::Active,
|
||||
},
|
||||
Column {
|
||||
label: "Tags".to_owned(),
|
||||
property: Property::Tags,
|
||||
},
|
||||
],
|
||||
sort: vec![Sort {
|
||||
ascending: false,
|
||||
sort_by: SortBy::Uuid,
|
||||
}],
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
struct Report;
|
||||
|
||||
impl Report {
|
||||
fn parse(input: ArgList) -> IResult<ArgList, Subcommand> {
|
||||
fn to_subcommand(input: (Filter, &str)) -> Result<Subcommand, ()> {
|
||||
let report = Report {
|
||||
filter: input.0,
|
||||
..List::default_report()
|
||||
};
|
||||
Ok(Subcommand::List { report })
|
||||
fn to_subcommand(filter: Filter, report_name: &str) -> Result<Subcommand, ()> {
|
||||
Ok(Subcommand::Report {
|
||||
filter,
|
||||
report_name: report_name.to_owned(),
|
||||
})
|
||||
}
|
||||
map_res(
|
||||
pair(Filter::parse, arg_matching(literal("list"))),
|
||||
to_subcommand,
|
||||
)(input)
|
||||
// allow the filter expression before or after the report name
|
||||
alt((
|
||||
map_res(pair(arg_matching(report_name), Filter::parse), |input| {
|
||||
to_subcommand(input.1, input.0)
|
||||
}),
|
||||
map_res(pair(Filter::parse, arg_matching(report_name)), |input| {
|
||||
to_subcommand(input.0, input.1)
|
||||
}),
|
||||
// default to a "next" report
|
||||
map_res(Filter::parse, |input| to_subcommand(input, "next")),
|
||||
))(input)
|
||||
}
|
||||
|
||||
fn get_usage(u: &mut usage::Usage) {
|
||||
u.subcommands.push(usage::Subcommand {
|
||||
name: "list",
|
||||
syntax: "[filter] list",
|
||||
summary: "List tasks",
|
||||
name: "report",
|
||||
syntax: "[filter] [report-name] *or* [report-name] [filter]",
|
||||
summary: "Show a report",
|
||||
description: "
|
||||
Show a list of the tasks matching the filter",
|
||||
Show the named report, including only tasks matching the filter",
|
||||
});
|
||||
u.subcommands.push(usage::Subcommand {
|
||||
name: "next",
|
||||
syntax: "[filter]",
|
||||
summary: "Show the 'next' report",
|
||||
description: "
|
||||
Show the report named 'next', including only tasks matching the filter",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -634,31 +621,72 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list() {
|
||||
let subcommand = Subcommand::List {
|
||||
report: Report {
|
||||
..List::default_report()
|
||||
},
|
||||
fn test_report() {
|
||||
let subcommand = Subcommand::Report {
|
||||
filter: Default::default(),
|
||||
report_name: "myreport".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv!["list"]).unwrap(),
|
||||
Subcommand::parse(argv!["myreport"]).unwrap(),
|
||||
(&EMPTY[..], subcommand)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_filter() {
|
||||
let subcommand = Subcommand::List {
|
||||
report: Report {
|
||||
filter: Filter {
|
||||
universe: Universe::for_ids(vec![12, 13]),
|
||||
..Default::default()
|
||||
},
|
||||
..List::default_report()
|
||||
fn test_report_filter_before() {
|
||||
let subcommand = Subcommand::Report {
|
||||
filter: Filter {
|
||||
universe: Universe::for_ids(vec![12, 13]),
|
||||
..Default::default()
|
||||
},
|
||||
report_name: "foo".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv!["12,13", "list"]).unwrap(),
|
||||
Subcommand::parse(argv!["12,13", "foo"]).unwrap(),
|
||||
(&EMPTY[..], subcommand)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_filter_after() {
|
||||
let subcommand = Subcommand::Report {
|
||||
filter: Filter {
|
||||
universe: Universe::for_ids(vec![12, 13]),
|
||||
..Default::default()
|
||||
},
|
||||
report_name: "foo".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv!["foo", "12,13"]).unwrap(),
|
||||
(&EMPTY[..], subcommand)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_filter_next() {
|
||||
let subcommand = Subcommand::Report {
|
||||
filter: Filter {
|
||||
universe: Universe::for_ids(vec![12, 13]),
|
||||
..Default::default()
|
||||
},
|
||||
report_name: "next".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv!["12,13"]).unwrap(),
|
||||
(&EMPTY[..], subcommand)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_report_next() {
|
||||
let subcommand = Subcommand::Report {
|
||||
filter: Filter {
|
||||
..Default::default()
|
||||
},
|
||||
report_name: "next".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv![]).unwrap(),
|
||||
(&EMPTY[..], subcommand)
|
||||
);
|
||||
}
|
||||
@@ -704,11 +732,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_gc_extra_args() {
|
||||
let subcommand = Subcommand::Gc;
|
||||
assert_eq!(
|
||||
Subcommand::parse(argv!["gc", "foo"]).unwrap(),
|
||||
(&vec!["foo"][..], subcommand)
|
||||
);
|
||||
assert!(Subcommand::parse(argv!["gc", "foo"]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user