annotate src/dmargs.c @ 2634:f3c7115cbf85 default tip

Fix verbose build echos.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 29 Feb 2024 21:47:31 +0200
parents 70b589a22495
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
1 /*
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
2 * Simple commandline argument processing
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
4 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
5 *
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
7 */
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
8 /// @file
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
9 /// @brief Simple commandline argument processing functions
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmargs.h"
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
11
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
12
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
13 /**
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
14 * Parse and optionally handle the given long or short option argument.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
15 * @param currArg current argument string
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
16 * @param[in,out] argIndex pointer to index of current argument in argv[]
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
17 * @param argc number of arguments
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
18 * @param argv argument string array
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
19 * @param[in] opts options list array
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
20 * @param[in] nopts number of elements in options list array
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
21 * @param[in] handle_option function pointer to callback that handles option arguments
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
22 * @param[in] process if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked.
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
23 * @param[in] isLong true if the option is a --long-format one
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
24 * @returns returns @c true if option processing was successful
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
25 */
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
26 static bool dmArgsProcessOpt(
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
27 char *currArg, int *argIndex,
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
28 int argc, char *argv[],
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
29 const DMOptArg opts[], int nopts,
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
30 bool (*handle_option)(int id, char *, char *),
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
31 bool process, bool isLong)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
32 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
33 const DMOptArg *opt = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
34 char *optArg = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
35
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
36 for (int optIndex = 0; optIndex < nopts; optIndex++)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
37 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
38 const DMOptArg *node = &opts[optIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
39 if (isLong && node->o_long != NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
40 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
41 if (strcmp(currArg, node->o_long) == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
42 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
43 opt = node;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
44 optArg = NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
45 break;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
46 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
47
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
48 size_t len = strlen(node->o_long);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
49 if (strncmp(currArg, node->o_long, len) == 0 &&
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
50 currArg[len] == '=')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
51 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
52 opt = node;
2426
bb7264ddaefd Fix the check for --long option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 2425
diff changeset
53 optArg = (currArg[len+1] != 0) ? &currArg[len+1] : NULL;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
54 break;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
55 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
56 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
57 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
58 if (!isLong && node->o_short != 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
59 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
60 if (*currArg == node->o_short)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
61 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
62 opt = node;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
63 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
64 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
65 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
66 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
67
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
68 if (opt != NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
69 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
70 // Check for the possible option argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
71 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
72 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
73 if (*argIndex < argc)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
74 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
75 (*argIndex)++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
76 optArg = argv[*argIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
77 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
78
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
79 if (optArg == NULL)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
80 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
81 dmErrorMsg("Option '%s%s' requires an argument.\n",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
82 isLong ? "--" : "-",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
83 currArg);
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
84 return false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
85 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
86 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
87
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
88 // Option was given succesfully, try to process it
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
89 if (process && !handle_option(opt->id, optArg, currArg))
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
90 return false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
91 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
92 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
93 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
94 dmErrorMsg("Unknown %s option '%s%s'\n",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
95 isLong ? "long" : "short",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
96 isLong ? "--" : "-",
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
97 currArg);
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
98
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
99 return false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
100 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
101
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
102 return true;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
103 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
104
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
105
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
106 /**
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
107 * Process given array of commandline arguments, handling short
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
108 * and long options by calling the respective callback functions.
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
109 *
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
110 * @param argc number of arguments
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
111 * @param argv argument list
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
112 * @param[in] opts supported option list array
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
113 * @param[in] nopts number of elements in the option list array
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
114 * @param[in] handle_option callback function
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
115 * @param[in] handle_other callback function
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
116 * @param[in] flags processing flags
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
117 * @return returns @c true if arguments were processed successfully
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
118 */
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
119 bool dmArgsProcess(int argc, char *argv[],
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
120 const DMOptArg *opts, const int nopts,
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
121 bool(*handle_option)(int id, char *, char *),
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
122 bool(*handle_other)(char *), const int flags)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
123 {
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
124 int handleFlags = flags & OPTH_ONLY_MASK;
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
125 bool optionsOK = true, endOfOptions = false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
126
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
127 for (int argIndex = 1; argIndex < argc; argIndex++)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
128 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
129 char *str = argv[argIndex];
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
130 if (*str == '-' && !endOfOptions)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
131 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
132 // Should we process options?
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
133 bool process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
134 bool isLong;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
136 str++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
137 if (*str == '-')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
138 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
139 // Check for "--", which ends the options-list
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
140 str++;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
141 if (*str == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
142 {
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
143 endOfOptions = true;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
144 continue;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
145 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
146
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
147 // We have a long option
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
148 isLong = true;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
149 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
150 else
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
151 isLong = false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
152
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
153 if (!dmArgsProcessOpt(str, &argIndex, argc, argv,
2425
85b6d7ce8ca5 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 2423
diff changeset
154 opts, nopts, handle_option, process, isLong))
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
155 optionsOK = false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
156 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
157 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
158 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
159 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
160 // Was not option argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
161 if (handle_other == NULL ||
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
162 (handle_other != NULL && !handle_other(str)))
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
163 {
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
164 optionsOK = false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
165 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
166 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
167
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
168 // Check if we bail out on invalid argument
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
169 if (!optionsOK && (flags & OPTH_BAILOUT))
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
170 return false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
171 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
172
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
173 return optionsOK;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
174 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
175
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
176
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
177 void dmPrintPad(FILE *fh, int count, const char och)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
178 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
179 while (count--)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
180 fputc(och, fh);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
181 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
182
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
183
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
184 /**
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
185 * Print given string indented in such a way that it is automatically
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
186 * line-wrapped as necessary, taking hard linefeeds into account as well.
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
187 * @param fh stdio file handle to output to
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
188 * @param[in] spad starting pad/indent of the first line
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
189 * @param[in] rpad how much to pad the other lines
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
190 * @param[in] width total line width to wrap at
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
191 * @param[in] str string to output
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
192 */
2401
263093248f26 Change dmPrintWrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 2400
diff changeset
193 void dmPrintWrap(FILE *fh, const int spad, int const rpad,
263093248f26 Change dmPrintWrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 2400
diff changeset
194 const int width, const char *str)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
195 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
196 size_t pos = 0;
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
197 bool first = true;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
198
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
199 while (str[pos])
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
200 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
201 // Pre-pad line
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
202 int linelen = first ? spad : rpad;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
203 dmPrintPad(fh, first ? 0 : rpad, ' ');
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
204 first = false;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
205
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
206 // Skip whitespace at line start
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
207 while (isspace(str[pos]) || str[pos] == '\n')
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
208 pos++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
210 // Handle each word
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
211 while (str[pos] && str[pos] != '\n')
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
212 {
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
213 size_t next;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
214 int wlen;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
215
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
216 // Find word length and next break
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
217 for (wlen = 0, next = pos;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
218 str[next] && !isspace(str[next]) &&
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
219 str[next] != '\n';
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
220 next++, wlen++);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
221
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
222 // Check if we have too much of text?
2423
8d057a2459e3 Adjust options help width check, it was off-by-one.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
223 if (linelen + wlen > width)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
224 break;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
225
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
226 // Print what we have
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
227 for (;pos < next; pos++, linelen++)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
228 fputc(str[pos], fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
229
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
230 // Check if we are at end of input or hard linefeed
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
231 if (str[next] == '\n' || str[next] == 0)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
232 break;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
233 else
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
234 {
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
235 fputc(str[pos], fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
236 pos++;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
237 linelen++;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
238 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
239 }
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
240 fprintf(fh, "\n");
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
241 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
242 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
243
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
244
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
245 static inline const char *dmArgsGetOptArg(const DMOptArg *opt)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
246 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
247 #ifdef DM_USE_OPT_ARG
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
248 return opt->o_arg != NULL ? opt->o_arg : "ARG";
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
249 #else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
250 (void) opt;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
251 return "ARG";
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
252 #endif
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
253 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
254
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
255
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
256 static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt,
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
257 int *optWidth, const int maxOptWidth, const int termWidth,
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
258 const bool doPrint)
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
259 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
260 const char *arg = dmArgsGetOptArg(opt);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
261 char fmtBuf[32];
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
262 int padWidth;
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
263 bool hasLongOpt = opt->o_long != NULL;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
264
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
265 if (opt->o_short != 0)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
266 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
267 if (!hasLongOpt && (opt->flags & OPT_ARGREQ))
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
268 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
269 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
270 opt->o_short, arg);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
271 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
272 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
273 {
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
274 snprintf(fmtBuf, sizeof(fmtBuf), " -%c%s",
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
275 opt->o_short,
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
276 hasLongOpt ? "," : "");
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
277 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
278
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
279 *optWidth = strlen(fmtBuf);
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
280 if (doPrint)
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
281 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
282 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
283 padWidth = 2;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
284 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
285 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
286 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
287 fmtBuf[0] = 0;
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
288 *optWidth = 0;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
289 padWidth = 4 + 2;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
290 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
291
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
292 if (doPrint)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
293 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
294 fputs(fmtBuf, fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
295 dmPrintPad(fh, padWidth, ' ');
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
296 }
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
297 *optWidth += padWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
298
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
299 if (hasLongOpt)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
300 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
301 if (opt->flags & OPT_ARGREQ)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
302 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
303 snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
304 opt->o_long, arg);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
305 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
306 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
307 {
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
308 snprintf(fmtBuf, sizeof(fmtBuf), "--%s",
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
309 opt->o_long);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
310 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
311
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
312 *optWidth += strlen(fmtBuf);
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
313 }
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
314 else
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
315 fmtBuf[0] = 0;
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
316
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
317 if (doPrint)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
318 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
319 padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0;
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
320 *optWidth += padWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
321
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
322 fputs(fmtBuf, fh);
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
323 dmPrintPad(fh, padWidth, ' ');
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
324 dmPrintWrap(fh, *optWidth, *optWidth, termWidth, opt->desc);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
325 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
326 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
327
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
328
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
329 /**
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
330 * Print neatly formatted help for specified array of commandline options.
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
331 * The function will attempt to find the best indentation and formatting of
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
332 * the output using a simple fitting algorithm. Option descriptions will be
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
333 * automatically split over several lines if necessary.
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
334 * @param fh stdio file handle to output to
2592
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
335 * @param[in] opts options list array
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
336 * @param[in] nopts number of elements in the options list array @p opts
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
337 * @param[in] flags (currently unused)
70b589a22495 Sync some changes from th-libs/th_args.c
Matti Hamalainen <ccr@tnsp.org>
parents: 2586
diff changeset
338 * @param[in] width width of the output terminal or the desired maximum width of the printout
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
339 */
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
340 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts,
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
341 const int nopts, const int flags, const int width)
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
342 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
343 int index, maxOptWidth;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
344 (void) flags;
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
345
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
346 // Determine width of the options and arguments
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
347 maxOptWidth = 0;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
348 for (index = 0; index < nopts; index++)
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
349 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
350 int optWidth = 0;
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
351 dmArgsPrintHelpPrintItem(NULL, &opts[index], &optWidth, 0, width, false);
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
352 if (optWidth > maxOptWidth)
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
353 maxOptWidth = optWidth;
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
354 }
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
355
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
356 maxOptWidth += 2;
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
357
2400
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
358 // Print out the formatted option list
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
359 for (index = 0; index < nopts; index++)
01c6a5261962 Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2361
diff changeset
360 {
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2401
diff changeset
361 int optWidth;
2586
9807ae37ad69 Require stdbool.h, we require C11 now.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
362 dmArgsPrintHelpPrintItem(fh, &opts[index], &optWidth, maxOptWidth, width, true);
1742
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
363 }
ddec147d1f90 Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
364 }