docopt / docopt.c

C-code generator for docopt language.
MIT License
318 stars 46 forks source link

How are we going to handle the "struct DocoptArgs" element's names? #10

Closed ffunenga closed 8 years ago

ffunenga commented 11 years ago

Currently we've got the Naval Fate example being converted from this:

--- LeafNodes ---
  Argument('<y>', None)
  Argument('<x>', None)
  Argument('<name>', None)
  Command('remove', False)
  Command('set', False)
  Command('mine', False)
  Command('shoot', False)
  Command('ship', False)
  Command('move', False)
  Command('new', False)
  Option('--version', False)
  Option('--help', False)
  Option('--drifting', False)
  Option('--moored', False)
  Option('--speed', '10')

to this:

typedef struct {
    /* flag options */
    int help;
    int version;
    int moored;
    int drifting;
    /* options with arguments */
    char *speed;
    /* special */
    const char *usage_pattern;
    const char *help_message;
} DocoptArgs;

How do we translate CLI syntax into C valid struct elements?

Valid variable names in C respect the following three rules:

Variable names in C are made up of letters (upper and lower case) and digits.
The underscore character ("_") is also permitted.
Names must not begin with a digit.

[EDIT1] Is this (a bad idea | ugly | not universal C)? leafname = ''.join(('_' if c in '<>-' else c) for c in leaf.name)

originating in:

typedef struct {
    int remove;
    int set;
    int mine;
    int shoot;
    int ship;
    int move;
    int new;
    int __version;
    int __help;
    int __drifting;
    int __moored;
    char *_y_;
    char *_x_;
    char *_name_;
    char *__speed;
    const char *usage_pattern;
    const char *help_message;
} DocoptArgs;

In my PC, it compiles and executes.

[EDIT2] Possible conflict: an argument called __remove and an option named --remove wich will become __remove. Its crazy/strange to have an argument named __remove.

Maybe Arguments not starting in letters should be prohibited upstream. @docopt/docopt @halst Or maybe only docopt_c.py should prohibit it.

kblomqvist commented 11 years ago

Good question. We had a quite long discussion with @halst about this and I think we came into conclusion that let's just map all arguments to plain words. For example, <y> will become y. Special arguments could be double underscored, like __help, __usage.

keleshev commented 11 years ago

Yeah, just keep it simple in the beginning. Maybe in the end we'll have some templates that will produce arguments.options.help and arguments.commands.help kind of structs.