[OxLUG] Unix command line options/arguments

Top Page
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Sad Clouds
To: oxlug
Subject: [OxLUG] Unix command line options/arguments
Hi, I've looked at various ways of parsing command line options, things
like getopt and getsubopt, and then realised that if you have a complex
program that might require a config file, then you end up doing the
same work twice (openssl is a good example):

1. Write code to parse config file.
2. Write code to parse command line options with getop, etc.

The above two approaches do the same things, i.e. specify various
program parameters and options, but in two different ways.

So after a bit of thinking I came up with config syntax that can be
reused in config files and on command line:

Options are grouped into sections, you can have multiple sections, but
a section must not contain sub-sections inside it, i.e. section cannot


Each field can consist of attributes, and attributes can consist of
parameter=value pairs. Below are some examples:

One attribute
    field: attr;

Multiple attributes:
    field: attr1, attr2, attr3;

One attribute with one parameter
    field: attr:param;

One attribute with one parameter, parameter is given a value
    field: attr:param=value;

One attribute with multiple parameter=value pairs
    field: attr:param1=value1&param2=value2;

Multiple attributes with parameter=value pairs
    field: attr1:param=value, attr2:param=value&param2=value2;

Here is an example of how it can be used.

A program to load test a web server, options passed via command line:

wsload '
        threads: 2;
        tcp_params: so_rcvbuf:8K, so_sndbuf:8K, tcp_nodelay;
        serv: ipv4:, port:80;

        connection: persistent, pipeline:16;
        #connection: keep-alive;
        method: GET;


Alternatively, you could put everything in config file and invoke the
program with:

wsload 'global_options{ conf_file: "/tmp/wsload.conf"; }'

As you can see, the options passed via command line and config file are
consistent, have the same syntax and can be parsed with the same code.

Does anyone know if someone has already developed a library that does
more or less what I have described above (I couldn't find any)? Or
maybe someone can see a flaw in my methodology??