Skip to main content

Collections

Handle multiple values and key-value pairs with arrays and maps for complex command-line scenarios.

Array Options

Collect multiple values for a single option through various input methods:

ARGUS_OPTIONS(
options,
HELP_OPTION(),

// String arrays
OPTION_ARRAY_STRING('n', "names", HELP("List of names")),

// Integer arrays with range expansion
OPTION_ARRAY_INT('p', "ports", HELP("Port numbers")),

// Float arrays
OPTION_ARRAY_FLOAT('r', "rates", HELP("Processing rates")),
)

Supported types:

  • OPTION_ARRAY_STRING - Text values
  • OPTION_ARRAY_INT - Integer values with range support
  • OPTION_ARRAY_FLOAT - Decimal values

Map Options

Collect key-value pairs for configuration-style options:

ARGUS_OPTIONS(
options,
HELP_OPTION(),

// String maps
OPTION_MAP_STRING('e', "env", HELP("Environment variables")),

// Integer maps
OPTION_MAP_INT('p', "ports", HELP("Service port mappings")),

// Float maps
OPTION_MAP_FLOAT('s', "scales", HELP("Scaling factors")),

// Boolean maps
OPTION_MAP_BOOL('f', "features", HELP("Feature toggles")),
)

Supported value types:

  • OPTION_MAP_STRING - Text values
  • OPTION_MAP_INT - Integer values
  • OPTION_MAP_FLOAT - Decimal values
  • OPTION_MAP_BOOL - Boolean values

Accessing Collection Values

Access the raw array or map data:

ARGUS_OPTIONS(
options,
OPTION_ARRAY_STRING('n', "names", HELP("Names")),
OPTION_MAP_STRING('e', "env", HELP("Environment variables")),
)

int main(int argc, char **argv)
{
argus_t argus = argus_init(options, "myapp", "1.0.0");
argus_parse(&argus, argc, argv);

// Access arrays
if (argus_is_set(&argus, "names")) {
size_t count = argus_count(&argus, "names");
argus_value_t *names = argus_get(&argus, "names").as_array;

printf("Names (%zu):\n", count);
for (size_t i = 0; i < count; i++)
printf(" %zu: %s\n", i + 1, names[i].as_string);
}

// Access maps
if (argus_is_set(&argus, "env")) {
size_t count = argus_count(&argus, "env");
argus_pair_t *env = argus_get(&argus, "env").as_map;

printf("Environment (%zu):\n", count);
for (size_t i = 0; i < count; i++)
printf(" %s = %s\n", env[i].key, env[i].value.as_string);
}

argus_free(&argus);
return 0;
}

Validation with Collections

Apply validation to collection size and content:

ARGUS_OPTIONS(
options,
HELP_OPTION(),

// Require 1-5 tags
OPTION_ARRAY_STRING('t', "tags", HELP("Resource tags"),
VALIDATOR(V_COUNT(1, 5))),

// Optional labels (0-3)
OPTION_ARRAY_STRING('l', "labels", HELP("Optional labels"),
VALIDATOR(V_COUNT(0, 3))),

// Environment variables (1-10 pairs)
OPTION_MAP_STRING('e', "env", HELP("Environment variables"),
VALIDATOR(V_COUNT(1, 10))),
)

Usage:

./program --tags=web,api,backend        # ✅ Valid (3 tags)
./program --tags=a,b,c,d,e,f # ❌ Error: Count 6 is out of [1, 5]
./program # ❌ Error: Count 0 is out of [1, 5] (for tags)

Real-World Examples

ARGUS_OPTIONS(
options,
HELP_OPTION(),

// Build targets
OPTION_ARRAY_STRING('t', "targets", HELP("Build targets"),
FLAGS(FLAG_UNIQUE),
VALIDATOR(V_COUNT(1, 10))),

// Compiler flags
OPTION_ARRAY_STRING('f', "flags", HELP("Compiler flags")),

// Environment overrides
OPTION_MAP_STRING('D', "define", HELP("Preprocessor definitions")),

// Feature toggles
OPTION_MAP_BOOL('\0', "features", HELP("Feature flags"),
FLAGS(FLAG_SORTED_KEY)),
)

int main(int argc, char **argv)
{
argus_t argus = argus_init(options, "builder", "1.0.0");
argus_parse(&argus, argc, argv);

// Process build targets
argus_array_it_t targets_it = argus_array_it(&argus, "targets");
while (argus_array_next(&targets_it)) {
printf("Building target: %s\n", targets_it.value.as_string);
build_target(targets_it.value.as_string);
}

// Apply feature flags
argus_map_it_t features_it = argus_map_it(&argus, "features");
while (argus_map_next(&features_it)) {
set_feature(features_it.key, features_it.value.as_bool);
}

argus_free(&argus);
return 0;
}

Usage:

./builder --targets=lib,bin,tests \
--define=DEBUG=1,VERSION=2.0 \
--features=optimization=true,debug=false

Performance Considerations

Choose the right access method based on usage patterns:

Access MethodBest ForPerformance
Direct AccessRandom access, full processingO(1) access
Helper FunctionsSpecific element lookupO(1) arrays, O(n) maps
IteratorsSequential processingMost efficient for full traversal

Memory usage:

  • Initial capacity: 8 elements
  • Growth: Doubles when needed
  • Use V_COUNT() validation for large collections

Collection Flags Summary

FlagArraysMapsEffect
FLAG_SORTEDSort array values
FLAG_UNIQUERemove duplicate values
FLAG_SORTED_KEYSort map by keys
FLAG_SORTED_VALUESort map by values
FLAG_UNIQUE_VALUERemove duplicate values

Flag precedence: FLAG_SORTED_KEY overrides FLAG_SORTED_VALUE

What's Next?

Collection Design

Design collections around user workflows. Arrays work well for lists of similar items, while maps excel at configuration-style key-value pairs.