stparseargs(3)
π stparseargs(3) π
okay bestie, this one lives in section 3 of the manual, and it's brought to you by the ShapeTools Toolkit Library β yes, that ShapeTools, the icon itself. buckle up, we're parsing command line args and it's giving main character energy π
β¨ NAME β¨
stParseArgs, stShortUsage β parse command line arguments π«Ά
πΈ SYNOPSIS πΈ
#include <config.h>
#include <sttk.h>
int stParseArgs (int argCount, char *argVector,
int newArgCount, char *(*newArgVector[]), OptDesc
optionList[]);
voidstShortUsage (char *programName, OptDesc optionList[], char
*extraText);
π¦ DESCRIPTION π¦
so stParseArgs is the one doing the actual work, and here's the tea: it parses the command line for options that you already declared in an option description list. you hand it your program's argument count and argument vector as argCount and argVector, and the option description list as optionList, and it just... handles it. periodt.
what it does is filter out all the known options (known = the ones from your option list) along with their arguments, and then it gives you back whatever tokens are left over from the command line in newArgVector with newArgCount. so like, the leftovers are yours to play with π
β οΈ important bestie: options on the command line must be preceded by a minus sign. no minus sign, no recognition. it's just not gonna happen, no cap.
the first entry in the argument vector β that's your program name β passes through the function without any damage. untouched. iconic behavior honestly.
and the best part? stParseArgs can be invoked multiple times with different option lists. she's flexible like that π§
π the option list, explained
the option list is an array of option definitions that looks exactly like this:
typedef struct {
char *OptName;
int OptKind;
int (*OptFunc)();
int *OptVar;
char *OptStr;
} StOptDesc;
and the array gets finished off by an option definition that contains only null entries. that's the little "we're done here" signal, okay?
OptNameβ the name of the option exactly how it shows up on the command line, but without the leading minus sign. don't include it, bestie.OptKindβ a flag field that defines both the option type and the kind of action to be performed. two jobs, one field, she's versatile.
π valid option types (aka OptKind tea)
PSWITCHβ the option is a switch and has no argument. this is the default, so if you don't say otherwise, this is what you get β¨PARGβ the option has an argument immediately following the option on the command line.POARGβ the option has an optional argument. cute and chill about it.
π the actions (what actually happens when an option shows up)
when an option shows up on the command line, the action performed refers either to OptFunc β a function that your application defines β or to OptVar β an application variable associated with that option. your choice, your vibe. the different kinds of action are:
PCALLβ call the functionOptFunc. this is the default πPSETβ set the variableOptVarto1(TRUE).PCLEARβ set the variableOptVarto0(FALSE).PTOGGLEβ toggle the value of the variableOptVarbetween1(TRUE) and0(FALSE). flip flopping, but make it intentional π¦PUSAGEβ callstShortUsage(more on her below π).PFAILβ callstShortUsage(again, below π) and terminate program execution with return code1. yeah. she said goodbye fr fr π
π a little example, because examples ate
the following example shows some typical option definitions:
StOptDesc bindOptions[] = {
{ "help", PFAIL, NULL, NULL, NULL },
{ "trace", PSWITCH|PSET, NULL, &atBindTrace, NULL },
{ "since", PCALL|PARG, handleBaseline, NULL, NULL },
{ "version", PCALL, printVersion, NULL, NULL },
{ NULL, NULL, NULL, NULL, NULL },
};
see how that last line is all nulls? that's the array saying "that's a wrap" πΈ
π stShortUsage
stShortUsage generates a short usage message from the given optionList. the message starts with the programName and then lists all option in the option list (yes, every single one, she's thorough π). at the end, extraText gets added β but only if you actually gave her one. and where does this masterpiece go? standard error, obviously. π
π«Ά reminder:stParseArgsreturns anint, so check that return value, bestie. don't be out here ignoring her.
and that's the whole page, no notes. this was written for sttk-1.7, dated Thu Jun 24 17:43:31 1993 β yeah she's vintage, she's a classic, we stan a timeless icon πβ¨
π¦ where this page comes from
- debian bookworm: atfs-dev 1.4pl6-15+b1
- ubuntu jammy: atfs-dev 1.4pl6-15
- ubuntu noble: atfs-dev 1.4pl6-16.1build1
- ubuntu resolute: atfs-dev 1.4pl6-17
- ubuntu stonking: atfs-dev 1.4pl6-17
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms