• Stars
    star
    316
  • Rank 127,833 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C-code generator for docopt language.

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.

More Repositories

1

docopt

This project is no longer maintained. Please see https://github.com/jazzband/docopt-ng
Python
7,893
star
2

docopt.go

A command-line arguments parser that will make you smile.
Go
1,422
star
3

docopt.cpp

C++11 port of docopt
C++
1,028
star
4

docopt.rs

Docopt for Rust (command line argument parser).
Rust
754
star
5

docopts

Shell interpreter for docopt, the command-line interface description language.
Shell
489
star
6

docopt.rb

Parse command line arguments from nothing more than a usage message
Ruby
461
star
7

docopt.net

Port of docopt to .net
C#
348
star
8

docopt.php

Command line argument parser
PHP
298
star
9

docopt.R

Command-line interface description language for R (http://docopt.org)
R
205
star
10

docopt.nim

Command line arguments parser that will make you smile (port of docopt to Nim)
Nim
202
star
11

docopt.java

Java port of docopt
Java
156
star
12

docopt.coffee

docopt - A command line option parser that will make you smile.
CoffeeScript
148
star
13

docopt.hs

A command-line interface description language and parser that will make you smile
Haskell
118
star
14

DocOpt.jl

command line arguments parser
Julia
88
star
15

docopt.clj

Clojure implementation of the docopt language.
Clojure
65
star
16

try.docopt.org

Try out docopt in browser (Flask app running on Heroku)
CSS
61
star
17

docopt.swift

A command-line interface description language and parser that will make you smile http://docopt.org/
Swift
52
star
18

docopt.lua

Lua
37
star
19

docopt.scala

Scala implementation of docopt language
Scala
37
star
20

docopt.fs

Docopt for F#
F#
34
star
21

docopt.d

D
30
star
22

docopt.org

The website
Tcl
25
star
23

docopt.tcl

docopt.org for TCL
Tcl
4
star
24

challenge.docopt.org

Challenge docopt site
Python
3
star
25

docopt.rs-old

New Rust port is here:
Rust
2
star