Skip to content

docopt/docopt.c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C-code generator for docopt language

License PyPi publish PyPi: release

Note, at this point the code generator handles only options (positional arguments, commands and pattern matching will follow).

Step 1. Describe your CLI in docopt language

Naval Fate.

Usage:
  naval_fate ship create <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

Step 2. Generate the C code

$ python -m docopt_c -o docopt.c example.docopt

or by using pipe

$ cat example.docopt | python -m docopt_c > docopt.c

Step 3. Include the generated docopt.c into your program

#include "docopt.h"

int main(int argc, char *argv[])
{
    struct DocoptArgs args = docopt(argc, argv, /* help */ 1, /* version */ "2.0rc2");

    puts("Commands");
    printf("\tmine == %s\n", args.mine ? "true" : "false");
    printf("\tmove == %s\n", args.move ? "true" : "false");
    printf("\tcreate == %s\n", args.create ? "true" : "false");
    printf("\tremove == %s\n", args.remove ? "true" : "false");
    printf("\tset == %s\n", args.set ? "true" : "false");
    printf("\tship == %s\n", args.ship ? "true" : "false");
    printf("\tshoot == %s\n", args.shoot ? "true" : "false");
    puts("Arguments");
    printf("\tx == %s\n", args.x);
    printf("\ty == %s\n", args.y);
    puts("Flags");
    printf("\t--drifting == %s\n", args.drifting ? "true" : "false");
    printf("\t--help == %s\n", args.help ? "true" : "false");
    printf("\t--moored == %s\n", args.moored ? "true" : "false");
    printf("\t--version == %s\n", args.version ? "true" : "false");
    puts("Options");
    printf("\t--speed == %s\n", args.speed);

    return EXIT_SUCCESS;
}

Step 4. Profit!

$ c99 example.c -o example.out
$ ./example.out mine --drifting --speed=20
Commands
    mine == true
    move == false
    create == false
    remove == false
    set == false
    ship == false
    shoot == false
Arguments
    x == (null)
    y == (null)
Flags
    --drifting == true
    --help == false
    --moored == false
    --version == false
Options
    --speed == 20

Development

See the Python version's page for more info on developing.

About

C-code generator for docopt language.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published