Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> This looks like a hefty amount of boilerplate to achieve practically nothing.

I have yet to find a good way to avoid boilerplate in handling command line input on my console applications.

My naïve self feels that it should be standard, and yet every console application operates with different nuances. It's also a part of code that I see a disproportionate amount of bugs/feedback on (for sufficiently complicated programs). I see developers consistently underestimate the work required on handling command-line input.

I don't think this guys solution is a silver bullet but I'm happy to see the methods.



Nobody should be writing one-off command line parsing code. If you don't want something fairly heavy like Spectre, then just pull in a lighweight library like CommandLineParser.


One trick you can use if you are super lazy is to just use a config.json file that always resides next to your exe. JSON files are very easy to edit/share, and can be trivially serialized in and out of your models for convenience (1 liner with either Newtonsoft or built-in serializer). JSON is also a good interchange format between systems, so it can quickly integrate into a bigger system without much hassle.


The optparse-generic library in Haskell generates a CLI directly from a struct.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: