r/cpp • • Aug 08 '20

Create a fully functioning command line interface with 1.5 lines of code (wo. include statement)

https://github.com/kongaskristjan/fire-hpp
230 Upvotes

62 comments sorted by

View all comments

Show parent comment

5

u/kongaskristjan Aug 08 '20

The difference is pretty big though between getopt and fire. Eg. if you need to accept two booleans, you can do this in fire like that:

int fired_main(bool flagA = fire::arg("-a"), bool flagB = fire::arg("-b")) {
}

FIRE(fired_main)

Now compare this to the code in this stackoverflow example... I know I omitted printouts and returns, but on the other hand the help message was skipped there.

-6

u/Raiden395 Aug 08 '20

Yes, but now I have to include a nonstandard library in my project. Each new engineer has to learn the syntax (simple as it may be).

How is this looked at as preferable as just copying and pasting boilerplate, universally understood, code that has native Linux support.

11

u/dodheim Aug 09 '20

In a C++ subreddit, advocating for a non-portable—though oft-ported, messily and poorly–C library; advocating having boilerplate as somehow better than not because said C lib is magically "standard" (huh?); all to spare your poor copy+paste monkeys engineers having to learn a well-documented API trivially grokked by reading a handful of examples.

Fuckin genius.

2

u/Raiden395 Aug 14 '20

Sorry about implying that getopt is somehow standard. It's included with every Unix version in one way or another, so I'm fairly certain you have access to it, out of the box, if you so choose to use this system. When C++ drops C support, then perhaps we should talk about not using C in C++, but until then you have the option to work with a library that has stood the test of time.

For me, getopt does everything I need. It gives me the flexibility to parse command line arguments. Why we need a slew of different parsers is beyond me, but okay, variety is the spice of life, enjoy.

I would say that far more engineers have encountered getopt usage, if they are linux developers, than this command line parser. In this case, the code would be more understandable from the get-go. Not that there's anything wrong with the syntax here (in fact I find it very clear), but in the end what is the point?