A simple, lightweight C library for parsing command line arguments. Designed to be easy to integrate into other C projects.
- Support for long options (
--verbose
) and short options (-v
) - Multiple option types: boolean, string, integer, float
- Default values and required options
- Positional arguments
- Automatic help generation
- Error handling with descriptive messages
- Cross-platform (Linux, macOS, Windows)
- No external dependencies
- Easy integration with pkg-config
mkdir build
cd build
cmake ..
make
make install
After installation, other projects can use cli4c with:
# Compile
gcc your_program.c $(pkg-config --cflags --libs cli4c)
# Or with CMake
find_package(cli4c REQUIRED)
target_link_libraries(your_target cli4c)
#include <cli4c/cli.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
// Initialize CLI context
cli4c_context_t *ctx = cli4c_init("myprogram");
if (!ctx) {
fprintf(stderr, "Failed to initialize CLI\n");
return 1;
}
// Add options
cli4c_option_t verbose_opt = {
.long_name = "verbose",
.short_name = 'v',
.type = CLI4C_OPTION_BOOL,
.description = "Enable verbose output",
.required = false,
.default_value = "false"
};
cli4c_add_option(ctx, &verbose_opt);
cli4c_option_t count_opt = {
.long_name = "count",
.short_name = 'c',
.type = CLI4C_OPTION_INT,
.description = "Number of items to process",
.required = false,
.default_value = "10"
};
cli4c_add_option(ctx, &count_opt);
// Parse arguments
cli4c_error_t err = cli4c_parse(ctx, argc, argv);
if (err != CLI4C_SUCCESS) {
fprintf(stderr, "Error: %s\n", cli4c_get_error_message(ctx));
cli4c_print_help(ctx);
cli4c_free(ctx);
return 1;
}
// Get option values
cli4c_value_t verbose_val, count_val;
cli4c_get_value(ctx, "verbose", &verbose_val);
cli4c_get_value(ctx, "count", &count_val);
// Get positional arguments
const char **args;
size_t arg_count;
cli4c_get_positional_args(ctx, &args, &arg_count);
// Use the parsed values
if (verbose_val.value.bool_val) {
printf("Verbose mode enabled\n");
}
printf("Processing %d items\n", count_val.value.int_val);
for (size_t i = 0; i < arg_count; ++i) {
printf("Argument %zu: %s\n", i, args[i]);
}
// Cleanup
cli4c_free(ctx);
return 0;
}
CLI4C_OPTION_BOOL
: Boolean flag (true/false)CLI4C_OPTION_STRING
: String valueCLI4C_OPTION_INT
: Integer valueCLI4C_OPTION_FLOAT
: Floating point value
myprogram --verbose --count 5 file1.txt file2.txt
myprogram -v -c 5 file1.txt file2.txt
myprogram --count=5 file1.txt
The library automatically generates help text:
cli4c_print_help(ctx);
Output:
Usage: myprogram [OPTIONS] [ARGS...]
Options:
-v, --verbose
Enable verbose output (default: false)
-c, --count <value>
Number of items to process (default: 10)
cli4c_context_t *cli4c_init(const char *program_name)
- Initialize CLI contextvoid cli4c_free(cli4c_context_t *ctx)
- Free CLI contextcli4c_error_t cli4c_add_option(cli4c_context_t *ctx, const cli4c_option_t *option)
- Add option definitioncli4c_error_t cli4c_parse(cli4c_context_t *ctx, int argc, char *argv[])
- Parse command line argumentscli4c_error_t cli4c_get_value(const cli4c_context_t *ctx, const char *long_name, cli4c_value_t *value)
- Get parsed option valuecli4c_error_t cli4c_get_positional_args(const cli4c_context_t *ctx, const char ***args, size_t *count)
- Get positional argumentsvoid cli4c_print_help(const cli4c_context_t *ctx)
- Print help messageconst char *cli4c_get_error_message(const cli4c_context_t *ctx)
- Get last error message
CLI4C_SUCCESS
- Operation successfulCLI4C_ERROR_INVALID_ARGS
- Invalid arguments passed to functionCLI4C_ERROR_UNKNOWN_OPTION
- Unknown command line optionCLI4C_ERROR_MISSING_VALUE
- Required value not providedCLI4C_ERROR_INVALID_VALUE
- Invalid value formatCLI4C_ERROR_OUT_OF_MEMORY
- Memory allocation failed
# Build the library
mkdir build
cd build
cmake .. -DBUILD_EXAMPLES=ON
make
# Run example
./examples/basic_usage
# Build with tests
cmake .. -DBUILD_TESTS=ON
make
make test
See LICENSE file for details.