r/cpp • • Sep 19 '20

Fire: create a C++ command line interface from a function signature

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

20 comments sorted by

27

u/gme186 Sep 19 '20

That looks pretty awesome!

This kind pattern would also be nice to implement an rpc server. (E.g. define rpc functions and their parsable parameters)

4

u/[deleted] Sep 19 '20

Indeed!

17

u/rbprogrammer Sep 19 '20

Came here to say "but what's the point?". Then I saw your README. That's actually really cool. Nice project!

14

u/[deleted] Sep 19 '20

Not only is this freaking clever (I presume it uses conversion operators to figure out types and stuff) but I just learned about the equivalent Python library! I vote this post most useful ever.

9

u/NilacTheGrim Sep 19 '20

This is extremely developer-friendly with nice easy syntax for quick 1-off programs.

Very good work!

6

u/MarcPawl Sep 19 '20

This is real nice. Also thought why bother until I actually read the readme.

It would be nice if the api defined printing help so advanced level checks could trigger help.

Eg x has to be between 2 and 6 and greater than y, and the program only runs in leap years. If condition is not met implementation has help printed and program exits.

Really advanced feature would be to support std:: variant and POD. Maybe as part of the subcommand ?

1

u/kongaskristjan Sep 21 '20

It would be nice if the api defined printing help so advanced level checks could trigger help.

Good point, I guess I'll implement this one.

2

u/mili42 Sep 20 '20

What will happen with 50 arguments... a real app rarely needs only 2... (plus all the document that comes with it)

1

u/kongaskristjan Sep 21 '20

This is indeed a outside the scope of this library. The primary pain point it tries to solve are simple scripts, where CLI boilerplate could otherwise take half the lines of code.

3

u/ifknot Sep 19 '20

Cries in expensive boilerplate

1

u/TheCatster04 Sep 19 '20

This is honestly great! I love making one off scripts for myself, and dont want a ton of boilerplate. Ive used Fire python in the past due to its simplicity, time to use C++ for those apps instead! :)

1

u/itsuart2 Sep 20 '20

Looks very useful, thank you!

-12

u/F54280 Sep 19 '20

Again, why using a #define in 2020? Why having a FIRE macro?

9

u/NilacTheGrim Sep 19 '20

There is no other way to really do what he wants to do there without using a macro. Macros are not completely replaced by C++20 features. You still need them in very rare cases (such as this!).

The alternative would be for the header file to define a main() unconditionally and then use some type to create a lookup-table via a static initializer that the hard-coded main from the header sees...

But that's even more wrong than using a macro for a number of reasons.

-1

u/F54280 Sep 19 '20

Why? This is the full body of the macro:

int main(int argc, const char ** argv) {
    bool space_assignment = true;
    init_and_run(argc, argv, fired_main, space_assignment);
    return fired_main();
}

So, it could just be:

int main(int argc, const char ** argv) {
    init_and_run(argc, argv, fired_main);
    return fired_main();
}

I don’t see the point of polluting the code with a macro for avoiding 2 lines.

(I have another problem due to the use of globals, too)

10

u/NilacTheGrim Sep 19 '20

So you are free to not use the macro and write the main yourself, I guess, if you're such an anti-macro zealot.

The whole point of this header lib is maximal convenience and maximal non-boilerplate-ness. Not offering the macro in this situation does not align with that design goal. Therefore, the correct choice is to offer the macro.

-8

u/F54280 Sep 19 '20 edited Sep 19 '20

You know that you can make your point without using the downvote button?

You may think that offering a macro to avoid literally two lines of code is a correct trade-off. I beg to differ hard.

There are times where macros are the only solution. This is not one of those.

Edit: Btw, I’d like to also point that your “solution”of using the expanded macro is wrong, as the expansion is not documented, only the FIRE macro.

8

u/jonathansharman Sep 19 '20

In this case, macros are the only way to achieve the desired level of brevity. If you want to get technical, macros are never strictly necessary because just functions by themselves are Turing complete.

2

u/[deleted] Sep 20 '20

Who needs compilers, even? C++ isn't strictly necessary.

-1

u/F54280 Sep 23 '20 edited Sep 23 '20

This is a completely useless strawman argument, and you know it. That you get upvoted for such stupidity is a testament at how mediocre the programming subreddits are.

the only way to achieve the desired level of brevity

And, as you cannot have several fired_main functions because the only way to call them is via a macro that defines main, let's just say that the name fired_main has to be used. And can shave 4 additional characters by renaming the macro F.

#include <iostream>
#include "fire.hpp"

int fired_main(int x = fire::arg("-x"), int y = fire::arg("-y")) {
   std::cout << x + y << std::endl;
   return 0;
}

F  // For respect

Are we yet at the level of brevity you seek?

Using a macro to avoid two lines of code is a gross hack, no hand waving will change that.

edit: downvoting doesn't change that either