https://github.com/docopt/docopt.c
C-code generator for docopt language.
https://github.com/docopt/docopt.c
Last synced: 7 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 (over 13 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T15:06:47.000Z (over 1 year ago)
- Last Synced: 2024-11-29T16:38:38.893Z (about 1 year ago)
- Language: Python
- Homepage:
- Size: 181 KB
- Stars: 320
- Watchers: 32
- Forks: 47
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
- awesome-c - docopt.c - Implementation of a command-line option parser. [MIT](https://spdx.org/licenses/MIT.html) (Utilities / YAML)
- awesome-c-zh - docopt.c - 命令行选项解析器的实现。[](https://spdx.org/licenses/MIT.html) (公用事业 / YAML)
- awesome-c - docopt.c - Implementation of a command-line option parser. [MIT](https://spdx.org/licenses/MIT.html) (Utilities / YAML)
README
C-code generator for docopt language
====================================
[](https://opensource.org/licenses/MIT)
[](https://github.com/offscale/docopt.c/actions/workflows/main.yml)
[](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 --version
Options:
-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.