Wrote a tiny version of argp for CLI parsing in embedded environments

zkwinkle1 pts1 comments

GitHub - zkwinkle/tiny_argp: A GNU argp inspired CLI parser for embedded / bare-metal C. · GitHub

/" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

zkwinkle

tiny_argp

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

main

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>28 Commits<br>28 Commits

examples

examples

tests

tests

.gitignore

.gitignore

LICENSE

LICENSE

Makefile

Makefile

README.md

README.md

tiny_argp.c

tiny_argp.c

tiny_argp.h

tiny_argp.h

View all files

Repository files navigation

tiny_argp

A GNU argp inspired CLI parser for embedded / bare-metal C.

Usage

Copy tiny_argp.c and tiny_argp.h into your<br>project and build them alongside your other sources.

To get started, look at examples/ex1_minimal.c for a<br>minimal working CLI program, then browse the rest of the examples<br>below for options, positional arguments, and subcommands.

Embedded suitability

No allocations

All state is stack-based or caller-provided.

Minimal stdlib dependency

Only and<br>(compiler-provided freestanding headers, always available) plus a handful<br>of small routines from and (memcpy, strcmp,<br>strlen, isprint, …) that an embedded libc like newlib-nano or<br>picolibc supplies.

Configurable output

All output goes through printer / err_printer callbacks, so you provide<br>your own printf and route it to whichever channel your target uses (UART,<br>USB serial, stdout, an in-memory log buffer, etc.). See Printer<br>requirements below.

Never terminates the runtime

No exit() or similar, errors are returned to the caller.

Printer requirements

All output goes through the printer and err_printer callbacks on the<br>tiny_argp struct. They have a printf-style signature:

typedef int (*tiny_argp_printer_t)(const char *fmt, ...);

The library only ever calls them with the following conversion specifiers:

%s: null-terminated string

%c: single character

Any minimal printf implementation that handles %s and %c is sufficient<br>(e.g. mpaland/printf, nanoprintf).

Examples

The examples/ directory contains four self-contained programs.

examples/ex1_minimal.c: A minimal program with no<br>options and no positional handling. Only has --help and --usage which are<br>added by the library.

examples/ex2_options.c: A program with a handful<br>of options and no positional arguments.

examples/ex3_positionals.c: Adds a required<br>positional argument ARG1 and a variable-length list of trailing STRINGs on<br>top of ex2's options. Also has more advanced --help output formatting.

examples/ex4_subcommands.c: An example using<br>git-style subcommands. An initial parser looks for an add or list<br>subcommands and then calls their parsers with the remaining arguments. Each<br>subcommand has its own options and help output.

Build them all with:

make -C examples

Then invoke any of the binaries directly:

examples/build/ex2_options --help<br>examples/build/ex3_positionals -v -o /tmp/out A B C<br>examples/build/ex4_subcommands add --name=widget --count=3

make -C examples verify runs a shell harness that exercises each example with<br>a spread of arg combinations and asserts on exit code and output.

To Test

make -C tests run

Run the test suite with -O0, -Os, -O2, and -O3:

make -C tests matrix

Check binary size

Prints the text / data / bss sizes of the compiled object at the<br>current CFLAGS:

make size

Override optimization level to compare, e.g.:

make clean<br>make size CFLAGS="-std=c11 -O2"

As a rough reference, on x86_64 with GCC 16.1 the compiled object is around<br>~12.5 kB with -O0, and ~6.5 kB with -Os.

AI usage

All library code was written by hand.

AI assistance was limited to writing unit tests (reviewed and edited by hand)<br>and copyediting the docs.

About

A GNU argp inspired CLI parser for embedded / bare-metal C.

Resources

Readme

License

MIT license

Uh oh!

There was an error while loading. Please reload this page.

Activity

Stars

stars

Watchers

watching

Forks

forks

Report repository

Contributors

Uh oh!

There was an error while loading. Please reload this page.

Languages

78.6%

C++<br>21.0%

Makefile<br>0.4%

You can’t...

examples tiny_argp make embedded output search

Related Articles