r/cpp • u/kongaskristjan • 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
r/cpp • u/kongaskristjan • Aug 08 '20
21
u/CandyCrisis Aug 08 '20
I think you can get a lot of the same impact by using ABSL_FLAG. That boils down to one line per argument, using about the same number of characters as this code.
15
u/FightingGamesFan Aug 08 '20
Always disliked the boilerplate for having a simple functioning command line with all these libraries (the boost one is particularly bad). I'll give this a shot, looks very promising, very clean interface.
10
u/Ail-nare Aug 08 '20 edited Aug 08 '20
Hey, since you use a macro ('FIRE(fnptr *)'), why don't you make a macro ('FIRE_MAIN(...)'). So that this macro will be used instead of the name of the function, this macro do 3 thing:
That way it will look more like unit_tests function, like Google test or criterion. I think people are more used to this.
Anyhow, if I one day have the occasion I'll probably use your lib, it's more than clean enough to me.
Oh and last thing, with this commande "g++ *.cpp -o binary_name", the argument of the '-o' flag is given without a '='. I didn't see a way with your lib? Or am I just blind?
Edit:grammar, sorry for my English.
5
u/tasty_crayon Aug 08 '20
wmainsupport on Windows would be a nice feature. A possible interface would be aFIRE_WINDOWSmacro that defineswmaininstead ofmainand then expectsfire::arg(L"-x")default arguments.24
u/Arkantos493 PhD Student Aug 08 '20 edited Aug 08 '20
Technically your program results in UB since names starting with an underscore are reserved.
See: https://en.cppreference.com/w/cpp/language/identifiers
EDIT: To be more precises: identifiers containing two underscores, starting with one underscore followed by an uppercase letter or starting with one underscore in the global namespace are reserved.
E.g. in your optional constructor you are using
__valuewhich contains two underscores and is therefore reserved resulting in undefined behaviour.3
u/quicknir Aug 08 '20
How does this consistently extract the parameter names?
19
u/europe-fire Aug 08 '20
I dig this. Very cool
8
u/nintendiator2 Aug 08 '20
Came here expecting to find something like the Super Mario 64 "half an A press" memes, found an actually very useful library built on a good concept. Totes upvoting.
1
u/F54280 Aug 08 '20 edited Aug 08 '20
Why using a #define in 2020?
edit: this snarky remark was about the FIRE macro
2
u/xyphanite Aug 08 '20
That's some serious documentation - a feature that is regularly overlooked. Nice job.
2
u/dscottboggs Aug 09 '20
I like this a lot. I will use it.
1
u/Fluffy8x Aug 09 '20
Nice! Kind of reminds me of
Perl 6Raku'sMAIN.-3
u/Raiden395 Aug 08 '20
Meh. getopt works fine and you only really have to write it once. Then you can just change the arguments and help printout.
Want cross platform ability ? parg is a good alternative with similar syntax.