Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/docopt/docopt.c
C-code generator for docopt language.
https://github.com/docopt/docopt.c
Last synced: about 2 months ago
JSON representation
C-code generator for docopt language.
- Host: GitHub
- URL: https://github.com/docopt/docopt.c
- Owner: docopt
- License: mit
- Created: 2012-06-23T22:48:38.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2023-07-11T22:17:39.000Z (12 months ago)
- Last Synced: 2024-03-25T20:28:54.552Z (3 months ago)
- Language: Python
- Homepage:
- Size: 181 KB
- Stars: 316
- Watchers: 32
- Forks: 47
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Lists
- awesome-c - docopt.c - C implementation of a command-line option parser. [`Expat`](https://directory.fsf.org/wiki/License:Expat) (Utilities ## / Vim ###)
- awesome-stars - docopt/docopt.c - C-code generator for docopt language. (Python)
- awesome-c-zh - docopt.c - 命令行选项解析器的实现。[](https://spdx.org/licenses/MIT.html) (公用事业 / YAML)
README
C-code generator for docopt language
====================================
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![PyPi publish](https://github.com/offscale/docopt.c/actions/workflows/main.yml/badge.svg)](https://github.com/offscale/docopt.c/actions/workflows/main.yml)
[![PyPi: release](https://img.shields.io/pypi/v/docopt_c.svg?maxAge=3600)](https://pypi.org/project/docopt_c)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 ...
naval_fate ship move [--speed=]
naval_fate ship shoot
naval_fate mine (set|remove) [--moored|--drifting]
naval_fate --help
naval_fate --versionOptions:
-h --help Show this screen.
--version Show version.
--speed= Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
```### Step 2. Generate the C code
```bash
$ python -m docopt_c -o docopt.c example.docopt
```or by using pipe
```bash
$ cat example.docopt | python -m docopt_c > docopt.c
```### Step 3. Include the generated `docopt.c` into your program
```c
#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!
```bash
$ 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](http://github.com/docopt/docopt) for more
info on developing.