cal: Add the -y option to show the current year

Without `-y`, to show the current full year you'd have to specify which
one: `cal 2023`. Adding `-y` makes it possible to see the full current
year without remembering what year we are in.

This option is also stolen from FreeBSD :^)

Additionally, validate args: prevent passing both -3 and -y at the
same time. Passing both `--three-month-mode` and `--year` to `cal`
doesn't make sense. You'd either want the one or the other.
This commit is contained in:
Karol Baraniecki 2023-03-04 14:03:05 +01:00 committed by Andrew Kaster
parent dab82e1531
commit 190a6650bd
2 changed files with 11 additions and 2 deletions

View file

@ -5,7 +5,7 @@ cal - Display a calendar
## Synopsis
```**sh
$ cal [--starting-day weekday] [--three-month-view] [[month] year]
$ cal [--starting-day weekday] [--three-month-view] [--year] [[month] year]
```
## Description
@ -24,6 +24,7 @@ Days, months and years are specified with numbers. Week starts at Sunday.
* `-s`, `--starting-day`: Specify which day should start the week. Accepts either short or long weekday names or indexes (0 being Sunday).
* `-3`, `--three-month-view`: Display the previous, current, and next months side-by-side.
* `-y`, `--year`: Display an entire year by laying out months on a grid. If no year number is specified, the current year is used as a default.
## Examples

View file

@ -169,6 +169,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int year = 0;
StringView week_start_day_name {};
bool three_month_mode = false;
bool year_mode = false;
Core::ArgsParser args_parser;
args_parser.set_general_help("Display a nice overview of a month or year, defaulting to the current month.");
@ -176,9 +177,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No);
args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No);
args_parser.add_option(week_start_day_name, "Day that starts the week", "starting-day", 's', "day");
args_parser.add_option(year_mode, "Show the whole year at once", "year", 'y');
args_parser.add_option(three_month_mode, "Show the previous and next month beside the current one", "three-month-view", '3');
args_parser.parse(arguments);
if (three_month_mode && year_mode) {
warnln("cal: Cannot specify both --year and --three-month-mode at the same time");
return 1;
}
time_t now = time(nullptr);
auto* tm = localtime(&now);
current_year = tm->tm_year + 1900;
@ -191,7 +198,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
month = 0;
}
bool year_mode = !month && year;
if (!month && year)
year_mode = true;
int week_start_day;
if (week_start_day_name.is_empty())