title: src/crelude/argparse.h
src/crelude/argparse.h
Classes
Name | |
---|---|
struct | _Arg |
struct | _ArgID |
struct | Args |
struct | _ArgTemplate |
struct | _ArgParser |
Types
Name | |
---|---|
enum | @0 { ARGPARSE_OK = EXIT_SUCCESS, ARGPARSE_INVALID, ARGPARSE_UNEXPECTED, ARGPARSE_EXPECTED_ARGUMENT, ARGPARSE_UNKNOWN} |
typedef struct _Arg | Arg |
typedef struct _ArgID | ArgID |
typedef struct _ArgTemplate | ArgTemplate |
typedef struct _ArgParser | ArgParser |
Functions
Name | |
---|---|
ArgID | argreg(ArgParser * ctx, char * short_form, char * long_form, bool takes_value, char * help) |
u0 | arginit(ArgParser * ctx) Initialise argument parser. |
ierr | argparse_c(ArgParser * ctx, u0 * map_id_to_arg, char * arg) Parse arguments one value of argv at the time. |
ierr | argparse(ArgParser * ctx, u0 * map_id_to_arg, string arg) Parse arguments one string value from argv at the time. |
ierr | argparseall_c(ArgParser * ctx, u0 * map_id_to_arg, usize argc, char ** argv) Parse arguments from argc and argv. |
Attributes
Defines
Detailed Description
Parsing command line arguments.
Types Documentation
enum @0
Enumerator | Value | Description |
---|---|---|
ARGPARSE_OK | EXIT_SUCCESS | |
ARGPARSE_INVALID | ||
ARGPARSE_UNEXPECTED | ||
ARGPARSE_EXPECTED_ARGUMENT | ||
ARGPARSE_UNKNOWN |
typedef Arg
typedef struct _Arg Arg;
typedef ArgID
typedef struct _ArgID ArgID;
typedef ArgTemplate
typedef struct _ArgTemplate ArgTemplate;
typedef ArgParser
typedef struct _ArgParser ArgParser;
Functions Documentation
function argreg
ArgID argreg(
ArgParser * ctx,
char * short_form,
char * long_form,
bool takes_value,
char * help
)
function arginit
u0 arginit(
ArgParser * ctx
)
Initialise argument parser.
function argparse_c
ierr argparse_c(
ArgParser * ctx,
u0 * map_id_to_arg,
char * arg
)
Parse arguments one value of argv
at the time.
function argparse
ierr argparse(
ArgParser * ctx,
u0 * map_id_to_arg,
string arg
)
Parse arguments one string
value from argv
at the time.
function argparseall_c
ierr argparseall_c(
ArgParser * ctx,
u0 * map_id_to_arg,
usize argc,
char ** argv
)
Parse arguments from argc and argv.
Attributes Documentation
variable hash
u64 hash;
variable value
ArgID value;
variable key
struct @4 key;
variable next
u0 * next;
Macros Documentation
define ARGP_MAGIC_INIT_CONST
#define ARGP_MAGIC_INIT_CONST 0x61726770
Source code
#pragma once
#include "common.h"
#define ARGP_MAGIC_INIT_CONST 0x61726770
/* a r g p */
enum {
ARGPARSE_OK = OK,
ARGPARSE_INVALID, //< invalid argument structure.
ARGPARSE_UNEXPECTED, //< unexpected argument while parsing.
ARGPARSE_EXPECTED_ARGUMENT, //< expected value to be given to parameter.
ARGPARSE_UNKNOWN, // < unknown argument/option given.
};
record(Arg) {
string value; //< slice into ARGV string, nil string if no argument.
bool is_on; //< true if argument toggled on, false if not (-a/+a).
};
newtype(ArgID, u16);
newmap(Args, ArgID, Arg);
record(ArgTemplate) {
ArgID id;
string short_form; //< may be nil.
string long_form; //< may be nil.
bool takes_value;
string help; //< may be nil, but morally shouldn't be.
};
record(ArgParser) {
u32 magic;
arrayof(ArgTemplate) templates;
mapof(byte, usize) index_of_short_form;
mapof(string, usize) index_of_long_form;
bool awaiting_value;
ArgID awaiting_value_id;
usize string_count; //< current index into ARGV.
string error_message; //< error if encountered.
};
ArgID argreg(ArgParser *ctx, char *short_form, char *long_form, bool takes_value, char *help);
u0 arginit(ArgParser *ctx);
ierr argparse_c(ArgParser *ctx, u0 *map_id_to_arg, char *arg);
ierr argparse(ArgParser *ctx, u0 *map_id_to_arg, string arg);
ierr argparseall_c(ArgParser *ctx, u0 *map_id_to_arg, usize argc, char **argv);
#ifdef ENTRY_FUNCTION
ierr argparseall(ArgParser *ctx, u0 *map_id_to_arg, Arguments args);
#endif
Updated on 23 August 2022 at 00:54:19 UTC