8 ms·
Show HN: Structopt for C++ – Parse command line arguments by defining a struct
- gwenzek 6y agoSimilar lib I made for Python: https://pypi.org/project/func-argparse/ https://pypi.org/project/func-argparse/ The nice thing with Python is that I implemented a parser from a function signature, and it also works for NamedTuple. The advantage wrt to other python parser is that it uses the type annotations, instead of guessing
- bgia 6y agoPretty cool. I like it. Seems one more step toward simplicity than Click for instance.
- 1337shadow 6y agoWell there's https://cli2.rtfd.io https://cli2.rtfd.io
- gwenzek 6y agoThanks for sharing, didn't knew about this one. It seems quite similar indeed.
- 1337shadow 6y agoI'd say the difference with click is that it exposes a nice Python API which makes it easier to meta program CLIs, nice for lazy people.
- schattschneider 6y agoI can also recommend Typer https://typer.tiangolo.com/ https://typer.tiangolo.com/ It's similar and awesome
- rmnclmnt 6y agoAnd based on Click for maximum interoperability with existing modules!
- cb321 6y agofunc-argparse seems much like https://pypi.org/project/argh/ https://pypi.org/project/argh/ the inspiration for the Nim https://github.com/c-blake/cligen https://github.com/c-blake/cligen which can source its "pseudo specification" from either a proc signature or a type/object definition.
- deleted 6y ago[deleted]
- cozzyd 6y agoThis looks cool but c++17 is required (as far as I can tell, since it requires std::optional), so it's difficult to use for many things until all my colleagues (and their clusters) upgrade their compilers.
- mehrdada 6y agoMight be possible to substitute with `absl::optional` for the time being.
- jeffbee 6y agoIt's sort of weird to drag in absl to get a flags library.
- malkia 6y agoI switched from gflags to absl::flags and I like it, though there are recommendations against using them too much - https://abseil.io/tips/45 https://abseil.io/tips/45 and https://abseil.io/tips/103 https://abseil.io/tips/103 (one of my first surprises starting at google, was that binaries (e.g. "exe's") would have thousands of options, just adding a new library to your binary, and more flags are there). I actually like that one, as rather than configuring top-down everything, one can directly "inject" values - for example if you were told to switch from one library to the other, and change your borg template for launching jobs, that new borg template that you've used would have flags set (by another team). Then that team can take it further and modify them if needed, thus allowing them to control their part of the software in your own deployed binary. (This way experiments can be rolled out too, or who knows what else).
- jeffbee 6y agoI don't think it's weird to use absl flags, I think it would be weird to import absl to support some other flags library, and not use absl flags.
- 6y ago
- gomoboo 6y agoI will try this one out. Here’s a similar library that I’ve had success with before: https://github.com/mmahnic/argumentum https://github.com/mmahnic/argumentum I’ve also recently used https://docs.opencv.org/trunk/d0/d2e/classcv_1_1CommandLineParser.html#details https://docs.opencv.org/trunk/d0/d2e/classcv_1_1CommandLineP... from OpenCV. It’s useful when you’re already using OpenCV and don’t want to bring in another dependency.
- pansa2 6y agoDoes anyone have experience with CLI11 [0]? It seems to have plenty of features and good documentation, plus it's available as a single header file. [0] https://github.com/CLIUtils/CLI11 https://github.com/CLIUtils/CLI11
- ddavis 6y agoI've used it for a couple of small projects. I like it a lot.
- gomoboo 6y agoNever used it but I’ll give it a look as well. Looks like everyone and their grandmum makes a command line parser. I can see the draw. It’s a problem with a small scope that you can make a dent in over a weekend.
- zarkov99 6y agoYes, worked great for what I needed it for.
- tekknolagi 6y agoI came across this C++11 library a couple days ago: https://github.com/kongaskristjan/fire-hpp https://github.com/kongaskristjan/fire-hpp
- ur-whale 6y agoThis seems to support git-like subcommands. Very, very nice: something mostly missing from other option parsers. Now does it generate the synopsis part of a man page automatically?
- worldsayshi 6y ago> Include <structopt/app.hpp> and you're good to go. I haven't touched C++ for almost a decade but I assume there's (still?) no obvious way that always works to include a dependency package in your build? For you C++ devs, does this instruction make it obvious how to install this thing into your project?
- nestorD 6y agoThe instruction makes me suspect that it is a header only library. In this case I would just drop the library source in a dedicated subfolder and expect it to work. (While it feels better than fighting the system to get a dependency to link properly I would still rather have a proper package manager...)
- flohofwoe 6y agoIt requires some digging into the project, because at first glance it doesn't seem to be a simple header-only library. There's a conanfile.py, and a CMakeLists.txt and all sorts of "junk" in the project directory. But in the end it all seems to come down to this single header file which only depends on C++ stdlib headers and which can be dropped into your own project (which seems to be generated (aka "amalgamated") from multiple smaller header files): https://github.com/p-ranav/structopt/tree/master/single_include/structopt https://github.com/p-ranav/structopt/tree/master/single_incl... So for typical C++ libraries, this seems quite straightforward to integrate. Usually when looking at a project directory full of random files like this, it's a lot more complicated.
- f00zz 6y agoIf your project uses CMake, you could * add the structopt repository as a git submodule to your project * add something like `add_subdirectory(structopt)` to your top-level CMake script * add structopt to your target_link_libraries and you're good to go.
- skrebbel 6y agoYou could do that if you wanted to use git submodules as a sort of poor man's package manager. But given that it's a header-only library, you can also just download the files, put them in your repo somewhere, and #include them.
- boltzmann_brain 6y agoI am somewhat enraged that it doesn't mention the fact it is inspired by amazing Rust's https://docs.rs/structopt/0.3.16/structopt https://docs.rs/structopt/0.3.16/structopt.
- ho_schi 6y agoA nice way to declare command line options and arguments. I expected already templates, but some of the generated code looks strange: static VISIT_STRUCT_CONSTEXPR const int max_visitable_members = 69; https://github.com/p-ranav/structopt/blob/master/single_include/structopt/structopt.hpp#L270 https://github.com/p-ranav/structopt/blob/master/single_incl...
- aldanor 6y agoThis is a copy/paste from a boost header, with some renaming.
- hoistbypetard 6y agoIt's this: https://github.com/p-ranav/structopt/blob/master/include/structopt/third_party/visit_struct/visit_struct.hpp https://github.com/p-ranav/structopt/blob/master/include/str... mashed into a single header with some other stuff by a build tool, I think. That looks like something that was proposed for inclusion in boost a long time ago. I think it eventually got accepted after some renaming :) The amount of preprocessor magic in that file makes my head hurt.
- agumonkey 6y agohow many problems in computing are parsing problems ?
- throwawaygh 6y agoAll of them. The hard part is getting the grammar right. ;-)
- z29LiTp5qUC30n 6y agobut parsing command line arguments is a solved problem https://github.com/oriansj/M2-Planet/blob/master/cc.c#L40 https://github.com/oriansj/M2-Planet/blob/master/cc.c#L40
- bschwindHN 6y agoIs 'structopt' a common name for this type of argument parser library? I thought it started with the Rust library of the same name but no one has mentioned it yet... In my opinion, it is _the_ way to quickly create a quality CLI. https://github.com/TeXitoi/structopt https://github.com/TeXitoi/structopt
- uranusjr 6y agoHalf of the argument-parsing libraries contain either “arg” or “opt” due to the initial widespread of getopt or argp. And since both tools parse the arguments into a struct, the choice isn’t that surprising.
- Omin 6y agoIf you look at the repo's tags, they also include `clap`, which is the most popular Rust argument parsing library and the one that the original structopt is built upon. I don't think this coincidence. This looks like it's directly inspired by the Rust library, even though the README gives no indication of that.
- the-dude 6y agoIsn't CLAPs origin C++ ?
- Macha 6y agoAt least clap for "command line arg parser" has been used in this C++ library from 2006: http://tclap.sourceforge.net/manual.html http://tclap.sourceforge.net/manual.html Though I'm not sure if the Rust clap has any origins in any specific library for another language.
- Koshkin 6y agoBy the way, using variadic templates it's not hard to write a strictly-typed command-line parser.
- chli 6y agoI've used http://docopt.org/ http://docopt.org/ in the past for a toy project. It's an interesting concept where you define the command line "help" syntax and it generates an arguments structure and an associated parser. I've used the C version, I believe the C++ version is better maintained.
- thechao 6y agoDocopt is great until you start digging in and discover the author doesn't have the first clue about parsing. I don't mean in the sense of "weird opinions about recursive descent", but in the literal sense of "has no idea what parsing is or means". The internal implementation is a gigantic clusterfuck of the worst sort of poorly constructed, unmaintainable code you could imagine. I maintain two ports of docopt and I'm telling you not to use it.