Friday, 24 November 2017

In Praise Of Rust's structopt For Command Line Parsing

I've built a lot of command-line tools over the years, mostly in C or C++, but also in Java, Python, Rust, Turbo Pascal, etc. I mostly rolled my own command-line parsing because I did not find getopt very useful, and in most of those languages importing third-party libraries is more effort than reward for such a relatively simple task. In Rust I used the clap library because it provides more functionality (e.g. useful help text) at a lower cost (Rust makes managing library dependencies so easy). I recently discovered structopt and tried it out. Finally, the one true way to parse command-line options!

With structopt you provide a Rust data structure defining a simple AST for your command-line options. You mark it up with structopt-specific attributes describing more details of the syntax, e.g. option names. Markup is minimized using sensible defaults and leveraging Rust conventions: doc-comments are used as help text, Option makes arguments optional, subcommands use enums, value are parsed using the FromStr trait, etc. Then structopt generates code (using Rust's "custom derive") that parses the command line options into your data structure. It uses clap underneath so you get helpful error messages, support for generating shell completion scripts, etc. But unlike using clap directly, you get the final parsed results which you can use from Rust code in a completely natural way, e.g. using match on subcommand enums. It's beautiful. It also has practical advantages for maintenance: for example, unused arguments are unused fields and produce compiler warnings. Take a look at the examples.

This is a special case of deserialization. A lot of programming is serializing and deserializing, and we have also used Rust's serde library to great effect. There must be more opportunities to use Rust custom-derive to implement DSLs for specialized serialization and deserialization problems.

Monday, 20 November 2017

Tararua Southern Crossing

I spent three days last week hiking the Tararua Southern Crossing in the Tararua Ranges north of Wellington with one of my children, and had a great time!

On the first day we took the train from Wellington city up the coast to Waikanae on the Kapiti Coast, then were driven east into the ranges, to Otaki Forks where the track starts. It took about five hours to walk to Kime Hut, at about 1400m — an altitude gain of about 1200m. From there we watched a spectacular sunset over the northern end of the South Island, which was wholly visible from the Marlborough Sounds in the west to the Kaikoura Ranges in the east. Kime has no heating and has a reputation for being cold, but it was upgraded in 2014 and although there was a hard frost outside, we were fine sleeping in our clothes inside our sleeping bags.

The next day we hiked over "the tops" — semi-alpine terrain above the tree line. This area is notoriously windy and potentially dangerous in bad weather, but the weather was fine and the wind tolerable. The DoC office had warned us not to take the route if the wind forecast for Powell Hut further north was 50km/h or higher, and I was able to download a fresh forecast in the morning from near Kime Hut which forecast 35km/h. In some places, notably the south side of Mount Hector, the wind was a little unnerving, but it was never a real problem. In places the track is narrow with steep drops on both sides, and although I'm generally not good with heights, I was fine just keeping my eyes on the track; wielding a couple of hiking poles helped. We were cautious and it took us about five hours to reach that day's destination, Alpha Hut. Along the way we had spectacular views from the Kapiti Coast in the west, around to the Hutt Valley, Wellington, and snow-capped peaks in the South Island to the south, and over to the Wairarapa valley in the east. It was a wonderful day.

The final day was a long walk through the bush along the Marchant Ridge down to rural Kaitoke. It took us about eight hours of walking, after which we had a taxi take us to Upper Hutt and then a train back to Wellington. It's a bit of a slog, but has more good views and generally fun. I tripped on a root near the end and banged my knee, which is still sore a few days later, but so it goes.

I'd never been tramping in the Tararuas before, and from the visitor books it looked like most trampers in this area are locals. I'm glad I got a chance to try it. We were lucky that the weather was excellent; it's often wet and/or foggy. Although the area between Kime and Alpha is classed as a "route" and most of the rest is "tramping track" grade, we found almost all the track well-defined and in good condition, though a lot of stepping up and down rocks and tree roots is required!

After the tramp we met up with the rest of the family for a couple of days in Wellington, in particular to visit the World War I exhibitions at Te Papa museum and the Dominion Museum. They are excellent, though required a significant amount of more walking which probably didn't help my knee.