One of the pieces of the Python standard library I tend to use the most is argparse. It is really convenient so I chose to implement that in Pystd. The conversion was fairly simple, with one exception. As C++ is not duck typed, adapting the behaviour to be strictly typed while still feeling "the same" took some thinking. Parsing command line options is also quite complicated and filled with weird edge cases. For example, if you have short options -a and -b, then according to some command line parsers (but not others) an argument of -ab is valid (but only sometimes).
I chose to ignore all the hard bits and instead focus on the core parts that I use most of the time, meaning:
- Short and long command line arguments
- Arguments can be of type bool, int, string or array of strings
- Full command line help
- Same actions as in argparse
The only cheat is that I did the column alignment by hand due to laziness. Using the new implementation resembles regular argparse (full code is here). First you create the parser and options:
Then you do the parse and use the results:
Compiling this test application and all support code from scratch takes approximately 0.2 seconds.
No comments:
Post a Comment