News for hackers. Stuff that matters.
Re:Show HN: Add a command-line interface to any C++ program by chrisaycock on Saturday October 16, 2021 @04:13AM (#28886331)
I wrote this to add a REPL-like interface to existing programs. It can call normal functions with automatic parsing and type safety.

  class Arithmetic : public CommandInterface {
    int add(int x, int y) {
      return x + y;
    }
  
    void register_commands() override {
      register_command(&Arithmetic::add, "add", "Add two numbers");
    }
  };
Simply evaluate a string to invoke the function.

  Arithmetic arithmetic;
  arithmetic.eval("add 3 4");  // returns "7"
The user can place the eval() call behind any interface that has a string for its input and output. I personally use it behind a TCP socket on my trading application; I can query my strategy's state and change its parameters on the fly.

If funky C++ interests you, the prop firm I work for is looking to hire engineers. Feel free to contact me directly: chris -AT- chrisaycock -DOT- com

Re:Show HN: Add a command-line interface to any C++ program by choeger on Saturday October 16, 2021 @07:57AM (#28887230)
Nice work. Thanks for releasing it as free software.

If I understand the design correctly, one can provide a custom argument parser by specializing lexical_cast?

Re:Show HN: Add a command-line interface to any C++ program by gpderetta on Saturday October 16, 2021 @10:58AM (#28888294)
Lexical cast, aside a couple of specializations, is just a wrapper over iostream, so probably you only need to define operator>> and <<.
Re:Show HN: Add a command-line interface to any C++ program by chrisaycock on Saturday October 16, 2021 @09:21AM (#28887696)
Yes, in theory. I've never tried to specialize lexical_cast since it's a Boost library and likely has its own assumptions. But I imagine it's possible if you want to handle things like arrays or user-defined types.
Re:Show HN: Add a command-line interface to any C++ program by agumonkey on Saturday October 16, 2021 @08:33AM (#28887391)
funny I was thinking of something similar but for vuejs/keymaps (yeah I like keyboard oriented interfaces even on reactive webapps)
Re:Show HN: Add a command-line interface to any C++ program by dmalecaut on Saturday October 16, 2021 @03:05PM (#28890482)
Prop firms sure like their esoteric C++!
Re:Show HN: Add a command-line interface to any C++ program by sfgweilr4f on Sunday October 17, 2021 @08:15AM (#28895904)
"prop"?
Re:Show HN: Add a command-line interface to any C++ program by dmalecaut on Wednesday October 20, 2021 @06:24AM (#28928979)
Proprietary trading firm
Re:Show HN: Add a command-line interface to any C++ program by HMH on Saturday October 16, 2021 @06:09AM (#28886772)
I haven't really touched C++ for a few years and it seems due to its lack of a nice package manager single header libraries are getting more and more poplar. Does this mean I should expect compilation time to significantly increase during development as headers need to be recompiled every time?
Re:Show HN: Add a command-line interface to any C++ program by pjmlp on Saturday October 16, 2021 @06:21AM (#28886818)
In the meantime two big contenders raised up, vcpkg alongside NuGET from Microsoft (also does Mac and Linux), and conan.

Personally I dislike conan, because package managers should be written in the language of the community they serve, but that is me.

Thankfully, both support binary libraries.

Then there are modules, which on Visual C++'s case are going along quite well.

Anyone that bothers using header only libraries only has themselves to blame for waiting for builds, they are a plague from users that pretend C and C++ are scripting languages.

Re:Show HN: Add a command-line interface to any C++ program by jstimpfle on Saturday October 16, 2021 @07:10AM (#28887044)
"Header only" might be a bad name, in my perception what it really means is "you can drop the code files into your project; we won't annoy you with our own over-engineered build system".

Slow-compiling APIs can be made in any form, whether "header only" or not. C++ template APIs cannot be compiled separately and can cause long compile times for all transitively dependent translation units.

For example, stb_image.h is a single file that is 1 copy+paste from github away. It compiles quite quickly, and often you will need it alongside only a single .c file in your project, but anyway it's extremely easy to compile it separately. All you have to do is cut+paste the implementation part to a separate .c file.

Re:Show HN: Add a command-line interface to any C++ program by moonchrome on Saturday October 16, 2021 @03:18PM (#28890602)
Copy pasting shit in your repo means you are responsible for maintaining that code and need to keep track of upstream manually. Especially in c++ where memory bugs are prevalent, this sounds like a terrible idea.
Re:Show HN: Add a command-line interface to any C++ program by pjmlp on Saturday October 16, 2021 @07:14AM (#28887063)
Templates can be instanted for common types and consumed via binary libraries.

Also modules support templates just fine.

Indeed, header only libs are just doing PHP like programming.

Re:Show HN: Add a command-line interface to any C++ program by twqas on Saturday October 16, 2021 @06:52AM (#28886970)
Unfortunately Microsoft is addicted to telemetry, so vcpkg has that. I did not bother checking for NuGET.

Header-only libraries can give substantial speedups (25% in extreme cases).

Re:Show HN: Add a command-line interface to any C++ program by pjmlp on Saturday October 16, 2021 @07:11AM (#28887049)
You can disable it.

I really would like to see benchmarks versus proper use of binary libraries.

Re:Show HN: Add a command-line interface to any C++ program by inetknght on Saturday October 16, 2021 @12:57PM (#28889266)
> You can disable it.

I shouldn't have to think about it; I shouldn't have to explicitly opt-out to have privacy. Telemetry should always be opt-in, and not opted-in by default.

Re:Show HN: Add a command-line interface to any C++ program by ranger_danger on Saturday October 16, 2021 @01:16PM (#28889429)
The problem is, as a developer trying to fix issues, telemetry can be extremely helpful... but if it's solely opt-in, then virtually nobody will actually use it, and then what's the point.
Re:Show HN: Add a command-line interface to any C++ program by kjs3 on Saturday October 16, 2021 @05:44PM (#28891862)
So your developer issues should trump my user issues?
Re:Show HN: Add a command-line interface to any C++ program by TillE on Saturday October 16, 2021 @06:37PM (#28892204)
It tells you to just recompile with -disableMetrics if you want, it's really not a huge deal. It's not like Windows.
Re:Show HN: Add a command-line interface to any C++ program by kjs3 on Tuesday October 19, 2021 @11:16PM (#28926354)
So as a user, I have to recompile in deference to making things easier for the developer. Definitely not like Windows...for all their sins, they don't pull entitled BS like that on users.
Re:Show HN: Add a command-line interface to any C++ program by ngneer on Saturday October 16, 2021 @09:49AM (#28887868)
Conan has been very useful to me
Re:Show HN: Add a command-line interface to any C++ program by mrich on Saturday October 16, 2021 @06:26AM (#28886851)
In the case here most of the code is templates and macros, so a cpp wouldn't save much.
Re:Show HN: Add a command-line interface to any C++ program by ranger_danger on Saturday October 16, 2021 @01:14PM (#28889411)
Just include that header in a separate C/C++ file that has no other purpose, then you won't have to recompile it every time you change something elsewhere.
Re:Show HN: Add a command-line interface to any C++ program by corysama on Saturday October 16, 2021 @01:56PM (#28889860)
Yeah. "header only" had really caught on because it is a catchy term that is simple to understand.

But, what I really, really wish caught on would be "single cpp file" with the expectation that you are capable of adding a single, self-contained source file to whatever build system you happen to be using.

Re:Show HN: Add a command-line interface to any C++ program by electroly on Saturday October 16, 2021 @02:44PM (#28890303)
That's often what "header only" libraries really are -- you create your own .cpp file, define a particular preprocessor symbol, and then include the header which will then produce the implementation in that translation unit. Without the symbol defined you only get the interface, suitable for including in other translation units.
Re:Show HN: Add a command-line interface to any C++ program by svalorzen on Saturday October 16, 2021 @08:18AM (#28887331)
Nice work! I maintain a library, cpp-readline [1], with a very similar API. The main difference is that relegates the REPL part to GNU readline. You do have the additional dependency, but on the other hand it automatically adds history and other nice-to-have features.

[1]: https://github.com/Svalorzen/cpp-readline

Re:Show HN: Add a command-line interface to any C++ program by butwhywhyoh on Saturday October 16, 2021 @03:21PM (#28890622)
This isn't a command-line interface, this is a REPL. Very confusing title.
Re:Show HN: Add a command-line interface to any C++ program by ulnarkressty on Saturday October 16, 2021 @09:56AM (#28887912)
How does this compare to boost::spirit? I tried some years ago to make something similar but gave up due to the huge compile times for even simple grammar parsing.
Re:Show HN: Add a command-line interface to any C++ program by chrisaycock on Saturday October 16, 2021 @10:02AM (#28887955)
Spirit is a parser combinator. That's if you want to design a whole language from scratch.

My library is for single-name commands that can take simple parameters. You can't define your own language here.

Re:Show HN: Add a command-line interface to any C++ program by eps on Saturday October 16, 2021 @07:11AM (#28887052)
Clean and simple API, clever implementation. Very nicely done.

Saying as someone who worked on a cli framework for network devices in the past and quite enjoyed it. It was in C though, so it wasn't as terse.

Re:Show HN: Add a command-line interface to any C++ program by azalemeth on Saturday October 16, 2021 @07:08AM (#28887033)
What about gnu Genopt? I have really enjoyed using it, although it's perhaps pure C rather than C++.
Re:Show HN: Add a command-line interface to any C++ program by jcul on Saturday October 16, 2021 @07:39AM (#28887155)
I haven't used Genopt, it looks cool. At first I thought you meant getopt, which it looks like Genopt is built on.

However I think Genopt is concerned with command line arguments.

This is about an interactive command line interface, like a REPL. So you type command and it calls functions interactivly.

This kind of thing can be a lot of work to do right, so I think this header looks great. I'm looking forward to trying it out.

Re:Show HN: Add a command-line interface to any C++ program by ryandrake on Saturday October 16, 2021 @01:05PM (#28889324)
Yea, I was a little confused by the "command line" claim, too. Like, where is it parsing argv and argc, and does it support long style and short style options? Title should say "Add a REPL interface to any C++ program"
Re:Show HN: Add a command-line interface to any C++ program by cozzyd on Sunday October 17, 2021 @01:30AM (#28894205)
Note that you can use cling/ROOT an actual C++ repl.
Re:Show HN: Add a command-line interface to any C++ program by nuerow on Saturday October 16, 2021 @04:54AM (#28886500)
Obligatory reference to Boost's Program_options , which is as close to a standard way to handle command line options in C++ there is.

https://www.boost.org/doc/libs/1_77_0/doc/html/program_optio...

Re:Show HN: Add a command-line interface to any C++ program by nly on Saturday October 16, 2021 @05:59AM (#28886732)
As mentioned this isn't wholly relevent because TFA is about adding a REPL.

Nonetheless I prefer to use cxxopts[0] over BPO if I can justify the extra submodule

[0] https://github.com/jarro2783/cxxopts

Re:Show HN: Add a command-line interface to any C++ program by anon-user-101 on Saturday October 16, 2021 @05:13AM (#28886571)
To be fair to chrisaycock, this library it to actually add a runtime REPL rather than a command line interface. So the link to boost progam options is irrelevant.
Re:Show HN: Add a command-line interface to any C++ program by jb1991 on Saturday October 16, 2021 @06:55AM (#28886987)
Why is the example flushing the output buffer twice with endl? This is naive, and a simple new line is probably intended for at least one of those flushes.
Re:Show HN: Add a command-line interface to any C++ program by eps on Saturday October 16, 2021 @07:14AM (#28887066)
The only comment that you think is worthy making after looking at OP's project is about adding a blank line in front of a prompt, in an example? Show some rudimentary respect towards the work of others by not nitpicking on trivialities.
Re:Show HN: Add a command-line interface to any C++ program by jkrubin on Saturday October 16, 2021 @07:04AM (#28887025)
I think it’s pretty cool regardless. Don’t be a weenie.