One-stop code and documentation generator for CLI (command-line interface) tools written in C
Find a file
Daiki Ueno 59911854af Merge branch 'wip/no-error' into 'main'
codegen: do not emit error function call

Closes #1

See merge request gnutls/cligen!6
2024-07-24 05:37:25 +00:00
cligen codegen: do not emit error function call 2024-07-24 11:15:43 +09:00
fixtures codegen: do not emit error function call 2024-07-24 11:15:43 +09:00
.dir-locals.el Import the initial version 2022-03-08 15:22:20 +01:00
.gitignore Import the initial version 2022-03-08 15:22:20 +01:00
.gitlab-ci.yml .gitlab-ci.yml: use correct tag for GitLab 1.70 deployment 2024-07-24 08:50:54 +09:00
cli-codegen.py Import the initial version 2022-03-08 15:22:20 +01:00
cli-docgen.py cli-docgen.py: open include files 2022-03-09 15:46:06 +01:00
cligen.mk Add two SPDX-License-Identifier tags. 2022-05-22 07:29:17 +02:00
cligen.schema.json Support "argument-default" attribute on option 2022-08-17 16:54:48 +09:00
LICENSE Add LICENSE file 2023-07-03 06:06:40 +02:00
Makefile Add two SPDX-License-Identifier tags. 2022-05-22 07:29:17 +02:00
pyproject.toml .gitlab-ci.yml: Run tests through tox 2022-06-20 21:15:46 +09:00
README.md Fix typo found by codespell 2022-06-10 10:12:15 +02:00
setup.cfg .gitlab-ci.yml: Run tests through tox 2022-06-20 21:15:46 +09:00
tox.ini .gitlab-ci.yml: Run tests through tox 2022-06-20 21:15:46 +09:00

cligen

One-stop code and documentation generator for CLI (command-line interface) tools written in C.

Why?

This project was born during the process of migrating the GnuTLS CLI infrastructure from GNU AutoGen. Several other tools had been considered but none of them satisfied the following requirements:

  • The generator produces option parsing code and documentation from the same specification
  • The generator is written and works with minimal (build-)dependencies
  • The generated code works without separate runtime library

Usage

Writing the JSON specification

See certtool example. The schema is defined in cligen.schema.json so you can validate your specification with:

$ jsonschema --error-format "ERROR: {error.path} {error.message}" \
             --instance your-options.json cligen.schema.json

after installing python3-jsonschema package.

Generating the code and documentation

To generate command-line parser code (based on getopt) from JSON specification:

$ PYTHONPATH=. ./cli-codegen.py --package YOUR_PACKAGE --version YOUR_VERSION \
                                your-options.json foo.c foo.h

To generate man page from JSON specification:

$ PYTHONPATH=. ./cli-docgen.py --format man \
                               --package YOUR_PACKAGE --version YOUR_VERSION \
                               your-options.json foo.1

To generate texinfo documentation from JSON specification:

$ PYTHONPATH=. ./cli-docgen.py --format texi \
                               --package YOUR_PACKAGE --version YOUR_VERSION \
                               your-options.json foo.texi

Integrating the infrastructure into your project

The easiest way would be to embed the cligen project as a git submodule. To make it easy to integrate with Automake, a helper Makefile is provided, which can be included from the top-level Makefile.am:

$ git submodule add https://gitlab.com/gnutls/cligen.git
$ cat Makefile.am
include $(top_srcdir)/cligen/cligen.mk
noinst_PYTHON += $(cligen_sources)

Using the generated code from your program

Include the generated header file (*.h) and link to the generated source file (*.c). To parse command line arguments, call parse_options function defined as:

int process_options (int argc, char **argv);

To check the option status, use:

  • HAVE_OPT(name): expands to the option presence (true or false)
  • OPT_ARG(name): expands to the option argument (string or integer, depending on the specification
  • ENABLED_OPT(name): expands to the option enablement status (true or false)
  • OPTS_COUNT(name): expands to the count of occurrences of the option, if it is defined as multiple
  • OPTS_ARRAY(name): expands to the array of option arguments, if the option is defined as multiple and takes an argument.

TODO

  • Add different flavors of codegen output other than getopt (argp, GLib OptionContext)

License

LGPL-2.1-or-later