diff options
| author | Lion Kortlepel <[email protected]> | 2026-02-01 12:58:37 +0000 |
|---|---|---|
| committer | Lion Kortlepel <[email protected]> | 2026-02-01 12:58:37 +0000 |
| commit | 3c7b67aceb1ccf75e71358e84d9cc58d01b9abd5 (patch) | |
| tree | ebaaac40bf7a291002abfa3a91f4f7535382c92a | |
| parent | a29480c901a92a1b1891b4edc3239f8d997a3f9d (diff) | |
| download | args-3c7b67aceb1ccf75e71358e84d9cc58d01b9abd5.tar.zst args-3c7b67aceb1ccf75e71358e84d9cc58d01b9abd5.zip | |
feat: add help option printing, add descriptionv2.2
| -rw-r--r-- | examples/basic_example.c | 20 | ||||
| -rw-r--r-- | ls_args.h | 31 |
2 files changed, 40 insertions, 11 deletions
diff --git a/examples/basic_example.c b/examples/basic_example.c index 2f77b6c..43413f4 100644 --- a/examples/basic_example.c +++ b/examples/basic_example.c @@ -9,24 +9,24 @@ int main(int argc, char** argv) { const char* testfile; ls_args_init(&args); + args.help_description = "An example program to show how arguments work. " + "Provide an input file and optionally an output " + "file and test file and see what happens!"; ls_args_bool(&args, &help, "h", "help", "Prints help", 0); ls_args_string(&args, &outfile, "o", "out", "Specify the outfile, default 'out.txt'", 0); - ls_args_pos_string(&args, &infile, "Input file", LS_ARGS_REQUIRED); - ls_args_pos_string(&args, &testfile, "Test file", 0); + ls_args_pos_string(&args, &infile, "input file", LS_ARGS_REQUIRED); + ls_args_pos_string(&args, &testfile, "test file", 0); if (!ls_args_parse(&args, argc, argv)) { - printf("Error: %s\n", args.last_error); - puts(ls_args_help(&args)); + if (help) { + puts(ls_args_help(&args)); + } else { + printf("Error: %s\n", args.last_error); + } ls_args_free(&args); return 1; } - if (help) { - puts(ls_args_help(&args)); - ls_args_free(&args); - return 0; - } - printf("Got input file: %s\n", infile); printf("Got output file: %s\n", outfile); printf("Got test file: %s\n", testfile); @@ -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; |
