Code to go‎ > ‎

    OptionParser

    This small package (a .c file and associated .h header) written in standard c aims at handling command line arguments and configuration files in a unified manner. Please note that many of the features presented here may be enabled or disabled at will, making the package quite robust. To download the package feel free to scroll to the bottom of this page.
    At this time the package only works with ASCII strings.

    Table of contents

    Application interface

    The application is required to provide storage for a OPTHD header structure that holds pointer towards an array of OPTENTRY options. Each option is expected to feature:
    • a name: a string that does not include white spaces (- and _ are valid members); the case of the components of this string is unimportant for the package; An_Option is a valid option name, for example;
    • a type: this may be either integer number, real number or string. Conversion is automatically handled for numerical types at load and save;
    • a description: one line description of the option used when saving configuration to file and when presenting usage help
    • a set of flags:
      • how to handle multiple occurrences of same option name (error / append / overwrite)
      • arguments (does it accepts any?, is it required?);
      • dirty flag (was this ever specified?);

    Command line parsing

    The function OptPrs_ParseString is intended to process the command line that an application receives. First element in the string is expected to be the name of the application.

    Strings in general

    The package finds strings in one of two ways, depending on the first character. If a quote or double quote starts the string, then next quote character of same type is searched; all in between is the string. On the other hand, if no such characters are fist the string extends until first white space is encountered.
    As a side note, the executable is considered a string in this respect.

    White spaces

    These are all characters with an ASCII code lower than or equal to space character. 0, new line and line feed retain historical meaning. Entities on the command line are delimited by white spaces.

    Option markers

    At the command line a word is interpreted as an option name if it is preceded by one of following strings: -, --, / and \

    Option names

    Options specified at the command prompt (or elsewhere) are case insensitive. They must consist of a single word (no characters <= space).

    Option values

    The option name may optionally be followed by an equal sign and an optional argument. The argument may became a required feature by turning appropriate flag on.

    Orphan strings

    A string placed right after executable's name or one that follows an option that does not take arguments or one that follows the argument of an option is named an "orphan" string. Usually, these will represent paths to input files. The package stores these strings separately in an array in the order that they appear.

    File parsing

    The function OptPrs_ParseFile is able to process a file, extracting the information stored in proper format.

    Comments

    Comments may only appear on a dedicated line. To be identified as an comment line, first character that is not a white space must be one of #, //, ;
    Multi-line comment is also accepted, taking the form /* */. As an exception, after the */ an option may be placed.

    Option names

    A meaningful line must start with an option name followed by an equal sign. The name is case insensitive.

    Option values

    After the equal sign all white spaces are skipped. The value is either the inner part of a quoted text or all the text on that line without eventual white-spaces at the end of the line.

    Usage reporting

    Beside succinct information about how options may be presented at command line, OptPrs_PrintUsage function also prints each option and it's description.

    Saving the options

    Using OptPrs_Save function one may save the options as they are present inside the array at any given time. The format of the file that is generated is compatible with the format expected by  OptPrs_ParseFile function, so the file may be reloaded.
    The file starts with a two-line comment stating the name of the application and the time when the save operation was issued. Following this short intro a group of two lines is written for each option: first one is a comment telling the description of that option and second is the NAME = VALUE pair.

    Final notes

    This is not intended to be a complete description of the package. It is considered just enough to make you a top user. Compile time options were, for example, entirely omitted. Please feel free to send comments, questions, whatever at nicu [here goes a dot] tofan [ here goes @ ] gmail [ dot again ] com.
    Č
    ċ
    Nicu Tofan,
    Apr 11, 2011, 10:11 AM