From 3c7b67aceb1ccf75e71358e84d9cc58d01b9abd5 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 1 Feb 2026 12:58:37 +0000 Subject: feat: add help option printing, add description --- ls_args.h | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'ls_args.h') diff --git a/ls_args.h b/ls_args.h index b66c462..de87a9b 100644 --- a/ls_args.h +++ b/ls_args.h @@ -1,7 +1,7 @@ /* Lion's Standard (LS) ANSI C commandline argument parser with included help * renderer. * - * Version: 2.1 + * Version: 2.2 * Website: https://libls.org * Repo: https://github.com/libls/args * SPDX-License-Identifier: MIT @@ -136,6 +136,9 @@ typedef struct ls_args { * if you want to, for whatever reason. */ const char* program_name; + /* description rendered under the "usage" line in ls_args_help */ + const char* help_description; + /* some bookkeeping -- these are used to free dynamically allocated memory * for help or errors cleanly on `ls_args_free`. */ void* _allocated_error; @@ -700,6 +703,32 @@ char* ls_args_help(ls_args* a) { goto alloc_fail; } } + if (a->help_description) { + if (!_lsa_buffer_append_cstr(&help, "\n\n")) + goto alloc_fail; + if (!_lsa_buffer_append_cstr(&help, a->help_description)) + goto alloc_fail; + } + if (!_lsa_buffer_append_cstr(&help, "\n\nOptions:")) + goto alloc_fail; + for (i = 0; i < a->args_len; ++i) { + if (!a->args[i].is_pos) { + if (!_lsa_buffer_append_cstr(&help, "\n -")) + goto alloc_fail; + if (!_lsa_buffer_append_cstr( + &help, a->args[i].match.name.short_opt)) + goto alloc_fail; + if (!_lsa_buffer_append_cstr(&help, " \t--")) + goto alloc_fail; + if (!_lsa_buffer_append_cstr( + &help, a->args[i].match.name.long_opt)) + goto alloc_fail; + if (!_lsa_buffer_append_cstr(&help, " \t\t")) + goto alloc_fail; + if (!_lsa_buffer_append_cstr(&help, a->args[i].help)) + goto alloc_fail; + } + } } a->_allocated_help = help.data; -- cgit