I’m a big fan of Apache Groovy, it combines the best of Python and Java. This article series aims to show what it can do for you. (If you haven’t installed Groovy yet, please read the intro.)
You should also check out two of my previous articles – one on parsing command line arguments and the other on calculating date and time ranges.
Here I’ll explore command-line argument handling in more detail. The goal is to develop a script that can effectively perform date range calculations.
Reusable scripts that are generally useful should be able to process command-line options and arguments.
In Linux, options generally look like a hyphen followed by a single character, usually (but not always) a letter. They also might look like two hyphens together followed by a word.
For example, the famous ls
command takes either the argument -a
or --all
to list all files in a directory, rather than only the “non-hidden” ones.
Some options accept an argument. For example, imagine wanting to see the man page for the man
command in Spanish.
man --locale=es man
The man
command produces that. The argument to the option --locale
is es
, indicating the Spanish locale.
Groovy comes with the CliBuilder library built in. This helps facilitate complex command line processing in a very groovy way.
Here’s the outline of the script to handle calculating differences between two dates:
1 def cli = new CliBuilder(usage: "between.groovy -[hws] start-date end-date")
2 cli.with {
3 h longOpt: 'help', 'Show usage info'
4 w longOpt: 'weekdays', 'Count week days (not weekends)'
5 s longOpt: 'statutory', args: 1, argName: 'statutory holidays file', 'List of dates and names of statutory holidays'
6 }
7 def options = cli.parse args
8 if (options.h) {
9 cli.usage()
10 } else {
11 def onlyWeekdays = options.w
12 def statHolidayFileName = options.s
13 if (options.arguments()?.size() == 2) {
14 def (startDateString, endDateString) = options.arguments()
15 println "onlyWeekdays? $onlyWeekdays"
16 if (statHolidayFileName) {
17 println "statHolidayFileName $statHolidayFileName"
18 }
19 println "start-date $startDateString"
20 println "end-date $endDateString"
21 } else {
22 cli.usage()
23 System.err.println "you must supply a start date and end date"
24 }
25 }
Here is a review of the code:
Line one initializes the CliBuilder
instance, giving the usage string.
Lines two to six use the builder domain-specific language (DSL) defined by CliBuilder
to define the options:
-h
(short form) or--help
(long form) – print the help for this script-w
(short form) or--weekdays
(long form) – only count weekdays, not weekends, between dates-s
(short form) or--statutory
(long form) – takes an argument, to be interpreted as a file defining statutory holidays
Builders are really wonderful things that Groovy supports particularly well through a combination of closures, meta-programming, and other groovy stuff. Groovy comes ready to go with several useful builders, including XML, JSON, and Swing (Java’s platform independent GUI support). But I’m not here to talk about builders today.
Line seven parses the command line.
Lines eight and nine check for presence of -h
or --help
and print the usage command if found.
Lines 11-12 dig out the -w
or --weekdays
option and the -s
or --statutory
option and its argument, assign true
or false
to the onlyWeekdays
variable and if present the String
value of the argument to the statHolidaysFileName
variable.
Lines 13-20 handle the processing of two arguments, assumed to be start-date
and end-date
, are provided.
Line 14 assigns the start-date
argument to the variable startDateString
and the end-date
argument to the endDateString
variables respectively. This particular Groovy syntax allows the compact and readable definition and assignment of two variables from a list, without needing multiple statements nor unpacking the list into its elements.
Lines 15-20 take the place of doing any real work with the options and arguments provided, simply printing them out. Note on line 15 you use Groovy String interpolation (the $onlyWeekdays
within the string is replaced with the current value of the onlyWeekdays
variable when executed) similarly on lines 17, 19 and 20.
Lines 21-23 deal with the situation when no arguments are provided or when the number of arguments is not 2. The expression options.arguments()?.size()
yields false if there are no arguments and only calls the size() function if there are arguments.
Conclusion
You can see from this not-too-simple example that dealing with complicated command line options and arguments is pretty straightforward in Groovy, thanks to CliBuilder. There are other cool Groovy features like multiple assignments from lists, closures and domain-specific language support. It’s also worth mentioning that Groovy scripts tend to be compact and readable, which makes support easier in the long run.
This post is part of a series on Apache Groovy, stay tuned for more.
Photo by Jason Rosewell on Unsplash
You must log in to post a comment.