Skip to content

Commit c4694cb

Browse files
committed
Extend cmd line processor to deal with envar ops
PMIx supports forward/set, unset, append, and prepend of environmental variables. However, the cmd line processor wasn't setup to handle cmd line options for that purpose. Extend it to provide such support. Forward (-x) of envars can be just the envar name (to pickup the local value and forward it), or can be envar=value to set the envar to a specific value. Unset (--unset) takes just the name of the envar. Append (--append-env) takes two arguments: * the name of the envar, appended with a "[c]" where the 'c' is the character to be used as the separator between envar values * the value to be appended So it looks like "--append-env FOO[:] 20" Prepend (--prepend-env) behaves exactly like append except it prepends the value to whatever current envar value it finds Multiple instances of any of these options may be present on the cmd line. Each instance will have its arguments appended to the parameter's pmix_cli_item_t's values argv-array. Signed-off-by: Ralph Castain <[email protected]>
1 parent 57555c8 commit c4694cb

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/util/pmix_cmd_line.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* and Technology (RIST). All rights reserved.
1818
* Copyright (c) 2016-2020 Intel, Inc. All rights reserved.
1919
* Copyright (c) 2017 IBM Corporation. All rights reserved.
20-
* Copyright (c) 2021-2024 Nanook Consulting All rights reserved.
20+
* Copyright (c) 2021-2025 Nanook Consulting All rights reserved.
2121
* $COPYRIGHT$
2222
*
2323
* Additional copyrights may follow
@@ -130,7 +130,6 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
130130
break;
131131
}
132132
opt = getopt_long(argc, argv, shorts, myoptions, &option_index);
133-
134133
switch (opt) {
135134
case 0:
136135
/* if this is an MCA param of some type, store it */
@@ -144,7 +143,21 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
144143
++optind;
145144
break;
146145
}
147-
/* store actual option */
146+
// if this is a "-env" option, then there can be three entries
147+
// that describe what to do
148+
if (0 == strcmp(myoptions[option_index].name, PMIX_CLI_PREPEND_ENVAR) ||
149+
0 == strcmp(myoptions[option_index].name, PMIX_CLI_APPEND_ENVAR)) {
150+
mystore(myoptions[option_index].name, argv[optind-1], results);
151+
mystore(myoptions[option_index].name, argv[optind], results);
152+
++optind;
153+
break;
154+
}
155+
if (0 == strcmp(myoptions[option_index].name, PMIX_CLI_UNSET_ENVAR)) {
156+
// unset-env option only takes one argument
157+
mystore(myoptions[option_index].name, optarg, results);
158+
break;
159+
}
160+
/* otherwise, store actual option */
148161
mystore(myoptions[option_index].name, optarg, results);
149162
break;
150163
case 'h':
@@ -162,7 +175,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
162175
if (0 == strcmp(ptr, "version") || 0 == strcmp(ptr, "V")) {
163176
str = pmix_show_help_string("help-cli.txt", "version", false);
164177
if (NULL != str) {
165-
printf("%s", str);
178+
fprintf(stderr, "%s", str);
179+
fflush(stderr);
166180
free(str);
167181
}
168182
PMIx_Argv_free(argv);
@@ -171,7 +185,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
171185
if (0 == strcmp(ptr, "verbose") || 0 == strcmp(ptr, "v")) {
172186
str = pmix_show_help_string("help-cli.txt", "verbose", false);
173187
if (NULL != str) {
174-
printf("%s", str);
188+
fprintf(stderr, "%s", str);
189+
fflush(stderr);
175190
free(str);
176191
}
177192
PMIx_Argv_free(argv);
@@ -185,7 +200,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
185200
pmix_tool_basename, pmix_tool_basename,
186201
pmix_tool_basename, pmix_tool_basename);
187202
if (NULL != str) {
188-
printf("%s", str);
203+
fprintf(stderr, "%s", str);
204+
fflush(stderr);
189205
free(str);
190206
}
191207
PMIx_Argv_free(argv);
@@ -198,11 +214,13 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
198214
str = pmix_show_help_string("help-cli.txt", "unknown-option", true,
199215
ptr, pmix_tool_basename);
200216
if (NULL != str) {
201-
printf("%s", str);
217+
fprintf(stderr, "%s", str);
218+
fflush(stderr);
202219
free(str);
203220
}
204221
} else {
205-
printf("%s", str);
222+
fprintf(stderr, "%s", str);
223+
fflush(stderr);
206224
free(str);
207225
}
208226
PMIx_Argv_free(argv);
@@ -215,7 +233,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
215233
pmix_tool_basename,
216234
pmix_tool_msg);
217235
if (NULL != str) {
218-
printf("%s", str);
236+
fprintf(stderr, "%s", str);
237+
fflush(stderr);
219238
free(str);
220239
}
221240
PMIx_Argv_free(argv);
@@ -224,7 +243,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
224243
str = pmix_show_help_string("help-cli.txt", "unrecognized-option", true,
225244
pmix_tool_basename, optarg);
226245
if (NULL != str) {
227-
printf("%s", str);
246+
fprintf(stderr, "%s", str);
247+
fflush(stderr);
228248
free(str);
229249
}
230250
}
@@ -236,7 +256,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
236256
pmix_tool_version,
237257
pmix_tool_msg);
238258
if (NULL != str) {
239-
printf("%s", str);
259+
fprintf(stderr, "%s", str);
260+
fflush(stderr);
240261
free(str);
241262
}
242263
// if they ask for the version, that is all we do
@@ -302,7 +323,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
302323
str = pmix_show_help_string("help-cli.txt", "short-arg-error", true,
303324
pmix_tool_basename, shorts[n], ptr);
304325
if (NULL != str) {
305-
printf("%s", str);
326+
fprintf(stderr, "%s", str);
327+
fflush(stderr);
306328
free(str);
307329
}
308330
PMIx_Argv_free(argv);
@@ -339,7 +361,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
339361
str = pmix_show_help_string("help-cli.txt", "short-no-long", true,
340362
pmix_tool_basename, shorts[n]);
341363
if (NULL != str) {
342-
printf("%s", str);
364+
fprintf(stderr, "%s", str);
365+
fflush(stderr);
343366
free(str);
344367
}
345368
PMIx_Argv_free(argv);
@@ -362,7 +385,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
362385
pmix_tool_basename, argv[optind-1],
363386
pmix_tool_basename, &argv[optind-1][2]);
364387
if (NULL != str) {
365-
printf("%s", str);
388+
fprintf(stderr, "%s", str);
389+
fflush(stderr);
366390
free(str);
367391
}
368392
PMIx_Argv_free(argv);
@@ -381,7 +405,8 @@ int pmix_cmd_line_parse(char **pargv, char *shorts,
381405
str = pmix_show_help_string("help-cli.txt", "unregistered-option", true,
382406
pmix_tool_basename, argv[optind-1], pmix_tool_basename);
383407
if (NULL != str) {
384-
printf("%s", str);
408+
fprintf(stderr, "%s", str);
409+
fflush(stderr);
385410
free(str);
386411
}
387412
PMIx_Argv_free(argv);

src/util/pmix_cmd_line.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Copyright (c) 2016-2017 Los Alamos National Security, LLC. All rights
1616
* reserved.
1717
* Copyright (c) 2017 IBM Corporation. All rights reserved.
18-
* Copyright (c) 2021-2023 Nanook Consulting. All rights reserved.
18+
* Copyright (c) 2021-2025 Nanook Consulting All rights reserved.
1919
* $COPYRIGHT$
2020
*
2121
* Additional copyrights may follow
@@ -157,6 +157,10 @@ PMIX_CLASS_DECLARATION(pmix_cli_result_t);
157157
#define PMIX_CLI_TARGETS "targets" // required
158158
#define PMIX_CLI_TERMINATE "terminate" // none
159159
#define PMIX_CLI_PSET_NAME "pset" // required
160+
#define PMIX_CLI_FWD_ENVAR "x" // required
161+
#define PMIX_CLI_PREPEND_ENVAR "prepend-env" // required
162+
#define PMIX_CLI_APPEND_ENVAR "append-env" // required
163+
#define PMIX_CLI_UNSET_ENVAR "unset-env" // required
160164

161165
typedef void (*pmix_cmd_line_store_fn_t)(const char *name, const char *option,
162166
pmix_cli_result_t *results);

0 commit comments

Comments
 (0)