ArgParse
Description
Parser state. Stack-allocate, init once, register verbs, run.
name – shown as the program name in --help and errors. about – one-line description at the top of --help. May be NULL. alloc – where the specs Vec and any owned strings come from.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
ArgParse.c:2:
#include <Misra/Std/Allocator/Default.h>
#include <Misra/Std/ArgParse.h>
#include <Misra/Std/Log.h>- In
ArgParse.c:24:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
const char *listen = NULL;- In
ArgParse.c:41:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
const char *listen = NULL;- In
ArgParse.c:58:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
const char *listen = NULL;- In
ArgParse.c:75:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 timeout = 30;- In
ArgParse.c:92:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 timeout = 30;- In
ArgParse.c:109:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
bool verbose = false;- In
ArgParse.c:126:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
bool verbose = false;- In
ArgParse.c:143:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 verbose = 0;- In
ArgParse.c:160:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 verbose = 0;- In
ArgParse.c:177:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("cp", NULL, A);
const char *src = NULL;- In
ArgParse.c:196:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("cp", NULL, A);
const char *src = NULL;- In
ArgParse.c:221:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 n = 0;- In
ArgParse.c:238:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
i64 v = 0;- In
ArgParse.c:255:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
f64 ratio = 1.0;- In
ArgParse.c:272:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
Str name = StrInit(A);- In
ArgParse.c:294:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
const char *listen = NULL;- In
ArgParse.c:311:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("cp", NULL, A);
const char *src = NULL;- In
ArgParse.c:330:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
bool v = false;- In
ArgParse.c:347:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u32 n = 0;- In
ArgParse.c:364:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
u8 v = 0;- In
ArgParse.c:381:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", NULL, A);
const char *x = NULL;- In
ArgParse.c:402:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("cat", NULL, A);
const char *file = NULL;- In
ArgParse.c:427:
DefaultAllocator a = DefaultAllocatorInit();
Allocator *A = ALLOCATOR_OF(&a);
ArgParse p = ArgParseInit("prog", "test prog", A);
const char *required = NULL;- In
ArgParse.c:443:
int main(void) {
WriteFmt("[INFO] Starting ArgParse tests\n\n");
TestFunction tests[] = {- In
ArgParse.c:471:
};
return run_test_suite(tests, sizeof(tests) / sizeof(tests[0]), NULL, 0, "ArgParse");
}- In
ArgParse.c:8:
/// for the public API surface and the design notes.
#include <Misra/Std/ArgParse.h>
#include <Misra/Std/File.h>
#include <Misra/Std/Io.h>- In
ArgParse.c:232:
/* ------------------------------------------------------------------ */
static ArgSpec *find_long(ArgParse *self, const char *long_name) {
VecForeachPtr(&self->specs, sp) {
if (sp->role == ARG_ROLE_POSITIONAL)- In
ArgParse.c:242:
}
static ArgSpec *find_short(ArgParse *self, const char *short_name) {
VecForeachPtr(&self->specs, sp) {
if (sp->role == ARG_ROLE_POSITIONAL)- In
ArgParse.c:308:
}
static void print_help(ArgParse *self) {
File err = FileStderr();- In
ArgParse.c:417:
void arg_register(
ArgParse *self,
ArgRole role,
const char *short_name,- In
ArgParse.c:461:
/* ------------------------------------------------------------------ */
ArgParse arg_parse_init(const char *name, const char *about, Allocator *alloc) {
if (!name)
LOG_FATAL("ArgParseInit: name is required");- In
ArgParse.c:466:
if (!alloc)
LOG_FATAL("ArgParseInit: allocator is required");
ArgParse p = {0};
p.alloc = alloc;
p.name = name;- In
ArgParse.c:474:
}
void ArgParseDeinit(ArgParse *self) {
if (!self)
return;- In
ArgParse.c:484:
// `ARG_RUN_ERROR` on failure (after printing the error).
static ArgRun handle_option_token(
ArgParse *self,
const char *tok, // the current argv[i] token
int *i_io, // walked forward by 1 when we consume a value
- In
ArgParse.c:583:
// when -v is a Flag or a Count. -lFOO (stuck value) is intentionally
// not supported in v1.
static ArgRun handle_short_bundle(ArgParse *self, const char *tok, File *err) {
// tok looks like "-XYZ..."; verify every char maps to a Flag/Count.
for (const char *p = tok + 1; *p; ++p) {- In
ArgParse.c:606:
}
ArgRun ArgParseRun(ArgParse *self, int argc, char **argv) {
if (!self || argc < 0 || (argc > 0 && !argv)) {
LOG_ERROR("ArgParseRun: bad arguments");- In
Std.h:18:
#include <Misra/Std/Allocator/Heap.h>
#include <Misra/Std/Allocator/Page.h>
#include <Misra/Std/ArgParse.h>
#include <Misra/Std/Container.h>
#include <Misra/Std/Container/Str.h>- In
ArgParse.h:136:
const char *about;
ArgSpecs specs;
} ArgParse;
///
- In
ArgParse.h:151:
/// FAILURE: Aborts via `LOG_FATAL` on allocator OOM.
///
ArgParse arg_parse_init(const char *name, const char *about, Allocator *alloc);
#define ArgParseInit(...) MISRA_OVERLOAD(ArgParseInit, __VA_ARGS__)- In
ArgParse.h:161:
/// safe on a zero-initialised one.
///
void ArgParseDeinit(ArgParse *self);
///
- In
ArgParse.h:174:
/// `ARG_RUN_ERROR` (parse error logged + usage hint).
///
ArgRun ArgParseRun(ArgParse *self, int argc, char **argv);
///
- In
ArgParse.h:184:
///
void arg_register(
ArgParse *self,
ArgRole role,
const char *short_name,- In
Beam.c:22:
#include <Misra/Parsers/Http.h>
#include <Misra/Std/Allocator/Default.h>
#include <Misra/Std/ArgParse.h>
#include <Misra/Sys/Dns.h>
#include <Misra/Sys/Socket.h>- In
Beam.c:200:
const char *upstream_spec = NULL;
ArgParse ap = ArgParseInit("beam", "small reverse-proxy");
ArgRequired(&ap, "-l", "--listen", &listen_spec, "host:port to listen on");
ArgRequired(&ap, "-u", "--upstream", &upstream_spec, "upstream host:port");- In
Resolve.c:15:
#include <Misra.h>
#include <Misra/Std/Allocator/Default.h>
#include <Misra/Std/ArgParse.h>
#include <Misra/Sys/Dns.h>
#include <Misra/Sys/Socket.h>- In
Resolve.c:23:
const char *hostname = NULL;
ArgParse ap = ArgParseInit("resolve", "look up a hostname via /etc/hosts and DNS");
ArgPositional(&ap, "hostname", &hostname, "name to resolve");
Last updated on