Skip to content

Using a a sorted hash as a DocoptArgs container. #49

@bigopensky

Description

@bigopensky

I am playing around with the results of the docopt-c tool. using this docopt configuration below.
The tool generates a static struct and command or mode with flags are defined as explicit size_t arg_name field.

Question

Might it be possible to use generate a zsorted_hash as a "storage engine" for the parsed results?

I have seen the several injection points for the code generation in the docopt-c tool, but cannot find the storage point to the struct in the code.

Code injection points for the parser

# ---------------------------------------------------------------------
def c_option(obj):
    return '{{{!s}}}'.format(', '.join(to_c(v)
            for v in (obj.short, obj.long, obj.argcount, False, None)))

# ---------------------------------------------------------------------
def c_name(s):
    return ''.join(c if c.isalnum() else '_' for c in s).strip('_')

# ---------------------------------------------------------------------
def c_if_command(cmd):
    return 'if (strcmp(command->name, {val!s}) == 0) {{\n' \
           '    args->{prop!s} = command->value;\n' \
           '}}\n'.format(val=to_c(cmd.name),
                         prop=c_name(cmd.name))

# ---------------------------------------------------------------------
def c_if_argument(arg):
    return 'if (strcmp(argument->name, {val!s}) == 0) {{\n' \
           '    args->{prop!s} = (char *) argument->value;\n' \
           '}}\n'.format(val=to_c(arg.name),
                         prop=c_name(arg.name))

# ---------------------------------------------------------------------
def c_if_flag(obj):
    return ' else if (strcmp(option->o{typ!s}, {val!s}) == 0) {{\n' \
           '    args->{prop!s} = option->value;\n' \
           '}}\n'.format(typ=('long' if obj.long else 'short'),
                         val=to_c(obj.long or obj.short),
                         prop=c_name(obj.long or obj.short))

# ---------------------------------------------------------------------
def c_if_option(obj):
    return ' else if (strcmp(option->o{typ!s}, {val!s}) == 0) {{\n' \
           '    if (option->argument) {{\n' \
           '        args->{prop!s} = (char *) option->argument;\n' \
           '    }}\n}}\n'.format(typ=('long' if obj.long else 'short'),
                                 val=to_c(obj.long or obj.short),
                                 prop=c_name(obj.long or obj.short))

Docopt config

program:
  rst-calc-common  – calculate commons

usage:
  rst-calc-common (-h | --help)
  rst-calc-common (-m | --man)
  rst-calc-common (-v | --version)
  rst-calc-common gray INPUT --output OUTPUT
  rst-calc-common min INPUT  --output OUTPUT [--ksize KSIZE [--normalize] [(-N NAN | 
options:
  -h, --help    Show this screen.
  --input INPUT Input raster file  [default: in.tif]
  --ksize KSIZE  Kernel size of the operator:INT  [default: 3]
  -m, --man  Show the documentation
  -n, --normalize  Normalize the result [default: False]
  -N NAN  A float value to address NoData [default: 9999.0]
  --output OUTPUT  Output raster [default: out.tif]
  -v, --version  Show version.

Static structure

struct DocoptArgs {

    /* commands */
    size_t gray;
    size_t min;
    /* arguments */
    char *INPUT;
    /* options without arguments */
    size_t help;
    size_t man;
    size_t version;
    /* options with arguments */
    char *ksize;
    char *nan;
    char *output;
    /* special */
    const char *usage_pattern;
    const char *help_message[..];
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions