MCPcopy Index your code
hub / github.com/cofyc/argparse

github.com/cofyc/argparse @v1.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.1.0 ↗ · + Follow
24 symbols 55 edges 3 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

argparse Build Status

argparse - A command line arguments parsing library in C (compatible with C++).

Description

This module is inspired by parse-options.c (git) and python's argparse module.

Arguments parsing is common task in cli program, but traditional getopt libraries are not easy to use. This library provides high-level arguments parsing solutions.

The program defines what arguments it requires, and argparse will figure out how to parse those out of argc and argv, it also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

Features

  • handles both optional and positional arguments
  • produces highly informative usage messages
  • issues errors when given invalid arguments

There are basically three types of options:

  • boolean options
  • options with mandatory argument
  • options with optional argument

There are basically two forms of options:

  • short option consist of one dash (-) and one alphanumeric character.
  • long option begin with two dashes (--) and some alphanumeric characters.

Short options may be bundled, e.g. -a -b can be specified as -ab.

Options are case-sensitive.

Options and non-option arguments can clearly be separated using the -- option.

Examples

#include <stdio.h>
#include "argparse.h"

static const char *const usage[] = {
    "test_argparse [options] [[--] args]",
    "test_argparse [options]",
    NULL,
};

#define PERM_READ  (1<<0)
#define PERM_WRITE (1<<1)
#define PERM_EXEC  (1<<2)

int
main(int argc, const char **argv)
{
    int force = 0;
    int test = 0;
    int num = 0;
    const char *path = NULL;
    int perms = 0;
    struct argparse_option options[] = {
        OPT_HELP(),
        OPT_GROUP("Basic options"),
        OPT_BOOLEAN('f', "force", &force, "force to do"),
        OPT_BOOLEAN('t', "test", &test, "test only"),
        OPT_STRING('p', "path", &path, "path to read"),
        OPT_INTEGER('n', "num", &num, "selected num"),
        OPT_GROUP("Bits options"),
        OPT_BIT(0, "read", &perms, "read perm", NULL, PERM_READ, OPT_NONEG),
        OPT_BIT(0, "write", &perms, "write perm", NULL, PERM_WRITE),
        OPT_BIT(0, "exec", &perms, "exec perm", NULL, PERM_EXEC),
        OPT_END(),
    };

    struct argparse argparse;
    argparse_init(&argparse, options, usage, 0);
    argparse_describe(&argparse, "\nA brief description of what the program does and how it works.", "\nAdditional description of the program after the description of the arguments.");
    argc = argparse_parse(&argparse, argc, argv);
    if (force != 0)
        printf("force: %d\n", force);
    if (test != 0)
        printf("test: %d\n", test);
    if (path != NULL)
        printf("path: %s\n", path);
    if (num != 0)
        printf("num: %d\n", num);
    if (argc != 0) {
        printf("argc: %d\n", argc);
        int i;
        for (i = 0; i < argc; i++) {
            printf("argv[%d]: %s\n", i, *(argv + i));
        }
    }
    if (perms) {
        printf("perms: %d\n", perms);
    }
    return 0;
}

Core symbols most depended-on inside this repo

argparse_error
called by 7
argparse.c
prefix_skip
called by 2
argparse.c
argparse_getvalue
called by 2
argparse.c
argparse_short_opt
called by 2
argparse.c
argparse_usage
called by 2
argparse.c
prefix_cmp
called by 1
argparse.c
argparse_options_check
called by 1
argparse.c
argparse_long_opt
called by 1
argparse.c

Shape

Function 13
Class 7
Enum 4

Languages

C71%
C++29%

Modules by API surface

argparse.c14 symbols
argparse.h7 symbols
test_argparse.c3 symbols

For agents

$ claude mcp add argparse \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact