Skip to content

Commit acd7eb2

Browse files
committed
flux-keygen: use optparse
Problem: flux-keygen does not have any option parsing infrastructure. Convert positional argument parser to optparse.
1 parent f1b97f0 commit acd7eb2

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/cmd/flux-keygen.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
#endif
1414
#include <unistd.h>
1515
#include <flux/core.h>
16+
#include <flux/optparse.h>
1617
#include <czmq.h>
1718

1819
#include "src/common/libutil/log.h"
1920

20-
21-
void usage (void)
22-
{
23-
fprintf (stderr,
24-
"Usage: flux-keygen PATH\n"
25-
);
26-
exit (1);
27-
}
21+
static struct optparse_option opts[] = {
22+
OPTPARSE_TABLE_END,
23+
};
2824

2925
static char * ctime_iso8601_now (char *buf, size_t sz)
3026
{
@@ -42,18 +38,28 @@ static char * ctime_iso8601_now (char *buf, size_t sz)
4238

4339
int main (int argc, char *argv[])
4440
{
41+
const char *usage_msg = "[OPTIONS] [PATH]";
42+
optparse_t *p;
43+
int optindex;
4544
zcert_t *cert;
4645
char buf[64];
4746
char *path = NULL;
4847

4948
log_init ("flux-keygen");
50-
51-
if (argc == 1)
49+
if (!(p = optparse_create ("flux-keygen"))
50+
|| optparse_add_option_table (p, opts) != OPTPARSE_SUCCESS
51+
|| optparse_set (p, OPTPARSE_USAGE, usage_msg) != OPTPARSE_SUCCESS)
52+
log_err_exit ("error setting up otpion parsing");
53+
if ((optindex = optparse_parse_args (p, argc, argv)) < 0)
54+
exit (1);
55+
if (optindex < argc)
56+
path = argv[optindex++];
57+
if (optindex < argc) {
58+
optparse_print_usage (p);
59+
exit (1);
60+
}
61+
if (!path)
5262
log_msg ("WARNING: add PATH argument to save generated certificate");
53-
else if (argc == 2 && *argv[1] != '-')
54-
path = argv[1];
55-
else
56-
usage ();
5763

5864
if (!(cert = zcert_new ()))
5965
log_msg_exit ("zcert_new: %s", zmq_strerror (errno));
@@ -70,6 +76,7 @@ int main (int argc, char *argv[])
7076

7177
zcert_destroy (&cert);
7278

79+
optparse_destroy (p);
7380
log_fini ();
7481

7582
return 0;

0 commit comments

Comments
 (0)