File tree Expand file tree Collapse file tree 1 file changed +17
-14
lines changed Expand file tree Collapse file tree 1 file changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -60,30 +60,33 @@ async function deleteUser(argv) {
60
60
console . log ( "Deleted user " + argv [ "del" ] + " ..." ) ;
61
61
}
62
62
63
+ var options = {
64
+ add : createUser ,
65
+ del : deleteUser ,
66
+ } ;
67
+
63
68
// Perform commandline-parsing
64
69
var argv = minimist ( process . argv . slice ( 2 ) ) ;
65
70
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 ' ) } !` ) ;
69
78
console . log ( usage ) ;
70
79
process . exit ( 1 ) ;
71
80
}
72
81
73
82
// 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!` ) ;
76
85
console . log ( usage ) ;
77
86
process . exit ( 1 ) ;
78
87
}
79
88
80
89
// 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