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+ { .name = "name" , .key = 'n' , .has_arg = 1 , .arginfo = "NAME" ,
23+ .usage = "Set certificate name (default: hostname)" , },
24+ OPTPARSE_TABLE_END ,
25+ };
2826
2927static char * ctime_iso8601_now (char * buf , size_t sz )
3028{
@@ -42,26 +40,37 @@ static char * ctime_iso8601_now (char *buf, size_t sz)
4240
4341int main (int argc , char * argv [])
4442{
43+ const char * usage_msg = "[OPTIONS] [PATH]" ;
44+ optparse_t * p ;
45+ int optindex ;
4546 zcert_t * cert ;
4647 char buf [64 ];
4748 char * path = NULL ;
4849
4950 log_init ("flux-keygen" );
50-
51- if (argc == 1 )
51+ if (!(p = optparse_create ("flux-keygen" ))
52+ || optparse_add_option_table (p , opts ) != OPTPARSE_SUCCESS
53+ || optparse_set (p , OPTPARSE_USAGE , usage_msg ) != OPTPARSE_SUCCESS )
54+ log_err_exit ("error setting up otpion parsing" );
55+ if ((optindex = optparse_parse_args (p , argc , argv )) < 0 )
56+ exit (1 );
57+ if (optindex < argc )
58+ path = argv [optindex ++ ];
59+ if (optindex < argc ) {
60+ optparse_print_usage (p );
61+ exit (1 );
62+ }
63+ if (!path )
5264 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 ();
5765
5866 if (!(cert = zcert_new ()))
5967 log_msg_exit ("zcert_new: %s" , zmq_strerror (errno ));
6068
6169 if (gethostname (buf , sizeof (buf )) < 0 )
6270 log_err_exit ("gethostname" );
6371 zcert_set_meta (cert , "hostname" , "%s" , buf );
64- zcert_set_meta (cert , "name" , "%s" , buf ); // used in overlay logging
72+ // name is used in overlay logging
73+ zcert_set_meta (cert , "name" , "%s" , optparse_get_str (p , "name" , buf ));
6574 zcert_set_meta (cert , "time" , "%s" , ctime_iso8601_now (buf , sizeof (buf )));
6675 zcert_set_meta (cert , "userid" , "%d" , getuid ());
6776
@@ -70,6 +79,7 @@ int main (int argc, char *argv[])
7079
7180 zcert_destroy (& cert );
7281
82+ optparse_destroy (p );
7383 log_fini ();
7484
7585 return 0 ;
0 commit comments