https://github.com/calebstewart/libcommand
Command line parser for C
https://github.com/calebstewart/libcommand
Last synced: about 1 month ago
JSON representation
Command line parser for C
- Host: GitHub
- URL: https://github.com/calebstewart/libcommand
- Owner: calebstewart
- Created: 2016-04-17T20:36:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-11T18:18:11.000Z (over 8 years ago)
- Last Synced: 2025-01-29T13:46:48.445Z (3 months ago)
- Language: C
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `libcommand` - Command Line parser for C
libcommand is a simple command line parser for C. You simply create an array of command handlers with associated names, and then call `command_run` with your
input line (presumably from readline or gets), the command array, and an optional extra data parameter and watch the magic happen.## Functions
### int command_run(char* line, command_t* commands, void* data)
Parses the line into argc/argv and calls the cooresponding command in the commands array.
#### Parameters:
* `line` - the input line you retrieved from the user
* `commands` - the array of commands. the last entry should have a NULL `run` callback.
* `data` - optional. this should hold other data you need passed to the callback.#### Return Value:
* If a command is matched, then the return value of the callback is returned.
* On error, -1 is returned and `errno` is set appropriately (see below)#### Errors:
* `ENOMEM` - No memory could be allocated for argv.
* `EINVAL` - No such command found in the command array.## Types
### typedef int(*command_handler_t)(void* data, int argc, char** argv)
The function type for handler functions. This takes an argc/argv pair and a data parameter from the calling function.
The return value is passed back to the main function.
### typedef struct _command command_t
Defines the command structure. This structure contains at least two items:
* `name` - Character array indicating the command name
* `run` - A `command_handler_t` to call when found in the input line.