File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,30 +60,33 @@ async function deleteUser(argv) {
6060 console . log ( "Deleted user " + argv [ "del" ] + " ..." ) ;
6161}
6262
63+ var options = {
64+ add : createUser ,
65+ del : deleteUser ,
66+ } ;
67+
6368// Perform commandline-parsing
6469var argv = minimist ( process . argv . slice ( 2 ) ) ;
6570
66- // Check for add/delete missing
67- if ( argv [ "add" ] == undefined && argv [ "del" ] == undefined ) {
68- console . log ( "You did not specify either --add or --del!" ) ;
71+ var keys = Object . keys ( options ) ;
72+ var opts = keys . filter ( ( key ) => argv [ key ] !== undefined ) ;
73+ var action = opts [ 0 ] ;
74+
75+ // Check for options missing
76+ if ( opts . length === 0 ) {
77+ console . log ( `You did not specify either ${ keys . map ( ( key ) => `--${ key } ` ) . join ( ' or ' ) } !` ) ;
6978 console . log ( usage ) ;
7079 process . exit ( 1 ) ;
7180}
7281
7382// Check if both are specified
74- if ( argv [ "add" ] != undefined && argv [ "del" ] != undefined ) {
75- console . log ( " You cannot add and delete at the same time!" ) ;
83+ if ( opts . length > 1 ) {
84+ console . log ( ` You cannot ${ action . join ( ' and ' ) } at the same time!` ) ;
7685 console . log ( usage ) ;
7786 process . exit ( 1 ) ;
7887}
7988
8089// Call respective processing functions
81- if ( argv [ "add" ] != undefined ) {
82- createUser ( argv ) . then ( function ( ) {
83- process . exit ( 0 ) ;
84- } ) ;
85- } else if ( argv [ "del" ] != undefined ) {
86- deleteUser ( argv ) . then ( function ( ) {
87- process . exit ( 0 ) ;
88- } )
89- }
90+ options [ action ] ( argv ) . then ( function ( ) {
91+ process . exit ( 0 ) ;
92+ } ) ;
You can’t perform that action at this time.
0 commit comments