0

I'm looking for some C library able to parse both a file and a command line with some rules system so I could specify which parameters should be accepted and what's their type. Any ideas?

Charles
  • 50,943
  • 13
  • 104
  • 142
Daniel
  • 4,272
  • 8
  • 35
  • 48
  • Very close to [Argument-parsing helpers for C](http://stackoverflow.com/questions/189972/argument-parsing-helpers-for-c-unix/191821). – Jonathan Leffler Sep 14 '12 at 13:06
  • 1
    All option parsing functions or libraries allow the program to specify which options are accepted, and which of those options take arguments. Most assume that the option argument is a string; what the calling code does with the string is up to the calling code. Clearly, you can write validation routines for particular types of argument. Do you want to specify which command line options are allowed in the configuration file (so by changing the configuration file, you change which options are acceptable to the program), or are you looking to allow complicated command lines to be read from file? – Jonathan Leffler Sep 14 '12 at 13:25
  • I want to specify which options are allowed, so those I don't process would cause an error. – Daniel Sep 14 '12 at 13:42

2 Answers2

2

For simple command-line parsing you have getopt together with getopt_long. This function can of course be used for configuration files as well, with a little "pre-parsing", if they follow the same format as command line options.

Note: getopt is standard on POSIX systems, e.g. Linux and Mac OSX, but not on Windows. There are implementations to be found for Windows though.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • i thought about that and even did PoC, but it was a complete mess: parsing file and creating a list of lines passed to getopt_long was not that good as expected also i still would like to get some validation mechanism w/o writing tonns of code for each option – Daniel Sep 14 '12 at 12:50
1

You could use GLib which provides a command line options parser, you can specify a type to each option. This library also provides a simple key/value file parser. It may be worth trying it out, even if you still have to code something (generating GOptionEntry from the ini file at least) ...
GLib is available on Linux, MacOSX and Windows.

Kwariz
  • 1,306
  • 8
  • 11
  • I don't want to link with glibc plus it still requires to manually merge results of parsing command line and file – Daniel Sep 14 '12 at 13:43
  • Well it's GLib, not glibc. In my mind it was more creating the GOptionEntry array from you ini file, and using this generated array to parse the command line. But if you don't want to link against GLib ... LLVM CommandLine library could be useful but it's c++ not c. – Kwariz Sep 14 '12 at 13:51
  • sorry, I've meant glib and I have plain C project – Daniel Sep 14 '12 at 14:20