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?
-
Very close to Argument-parsing helpers for C.Jonathan Leffler– Jonathan Leffler2012-09-14 13:06:34 +00:00Commented Sep 14, 2012 at 13:06
-
1All 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– Jonathan Leffler2012-09-14 13:25:10 +00:00Commented Sep 14, 2012 at 13:25
-
I want to specify which options are allowed, so those I don't process would cause an error.Daniel– Daniel2012-09-14 13:42:56 +00:00Commented Sep 14, 2012 at 13:42
2 Answers
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.
1 Comment
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.