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
2925static 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
4339int 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