Skip to content

adfoke/cli4c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cli4c

A simple, lightweight C library for parsing command line arguments. Designed to be easy to integrate into other C projects.

Features

  • 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

Installation

Using CMake

mkdir build
cd build
cmake ..
make
make install

Using pkg-config

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)

Usage

Basic Example

#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;
}

Option Types

  • CLI4C_OPTION_BOOL: Boolean flag (true/false)
  • CLI4C_OPTION_STRING: String value
  • CLI4C_OPTION_INT: Integer value
  • CLI4C_OPTION_FLOAT: Floating point value

Command Line Syntax

myprogram --verbose --count 5 file1.txt file2.txt
myprogram -v -c 5 file1.txt file2.txt
myprogram --count=5 file1.txt

Help Generation

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)

API Reference

Core Functions

  • cli4c_context_t *cli4c_init(const char *program_name) - Initialize CLI context
  • void cli4c_free(cli4c_context_t *ctx) - Free CLI context
  • cli4c_error_t cli4c_add_option(cli4c_context_t *ctx, const cli4c_option_t *option) - Add option definition
  • cli4c_error_t cli4c_parse(cli4c_context_t *ctx, int argc, char *argv[]) - Parse command line arguments
  • cli4c_error_t cli4c_get_value(const cli4c_context_t *ctx, const char *long_name, cli4c_value_t *value) - Get parsed option value
  • cli4c_error_t cli4c_get_positional_args(const cli4c_context_t *ctx, const char ***args, size_t *count) - Get positional arguments
  • void cli4c_print_help(const cli4c_context_t *ctx) - Print help message
  • const char *cli4c_get_error_message(const cli4c_context_t *ctx) - Get last error message

Error Codes

  • CLI4C_SUCCESS - Operation successful
  • CLI4C_ERROR_INVALID_ARGS - Invalid arguments passed to function
  • CLI4C_ERROR_UNKNOWN_OPTION - Unknown command line option
  • CLI4C_ERROR_MISSING_VALUE - Required value not provided
  • CLI4C_ERROR_INVALID_VALUE - Invalid value format
  • CLI4C_ERROR_OUT_OF_MEMORY - Memory allocation failed

Building Examples

# Build the library
mkdir build
cd build
cmake .. -DBUILD_EXAMPLES=ON
make

# Run example
./examples/basic_usage

Testing

# Build with tests
cmake .. -DBUILD_TESTS=ON
make
make test

License

See LICENSE file for details.

About

cli module for c

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published