commandlineobjects v2.2.0
CommandLineObjects (CLO)
Create nested Javascript objects from the commandline (or strings) with unnecessary complexity.
Booleans
Booleans are set using a double-dash --
. If present, they will be set to true. If you place an !
after the dashes, the value will be false.
--static --!verbose --!force
would set true false false
Strings, Integers
All strings and integers can be placed with -var value goes here
({ var: "value goes here" }
). So -port 6110
will be { port: 6110 }
.
Objects
Objects are added the same way as strings and integers, only all their variables and flags are encased in braces.
-obj [ --boolean2 --boolean1 -myinput goes here ]
= { obj: { boolean2: true, boolean1: true, myinput: "goes here" } }
These can be nested as seen in the example below.
You can also create sub-objects with a single token using the colon :
. For example -myobj:subobj [ --something --else ]
would become myobj { subobj { something: true, else: true } }
.
These can also be nested as far down as you want.
Arrays
As of 1.0.4 arrays now have limited support.
Declare an array: -myArray ~[ --static --constant ] ~[--static --constant]
This will produce the resembling object:
CLO {
_s: '-myArray ~[ --static --constant ] ~[--static --constant]',
myArray: [
CLO { static: true, constant: true },
CLO { static: true, constant: true }
]
}
This uses the same recursion code as the rest so you can go as deep as you want in each individual array value.
Example
A string like this:
node . --static --verbose --force -port 6110 -home /opt/websites -cluster ~[ -port 6111 --!verbose ] ~[ -port 6112 --!verbose ] -domains:lee-mcdonald.com [ -ssl [ --ssl-fallback --ssl-force -ssl-key /etc/letsencrypt/live/lee-mcdonald.com/privKey.pem -ssl-cert /etc/letsencrypt/live/lee-mcdonald.com/fullchain.pem ] -home /home/lee/websites/com -options [ --allow-directory --allow-write ] ] -domains:lee-mcdonald.net [ -ssl [ --ssl-fallback --ssl-force -ssl-key /etc/letsencrypt/live/lee-mcdonald.net/privKey.pem -ssl-cert /etc/letsencrypt/live/lee-mcdonald.net/fullchain.pem ] -home /home/lee/websites/net -options [ --!allow-directory --allow-write ] ]
Will produce an object of this caliber:
CLO {
_s: '--static --verbose --force -port 6110 -home /opt/websites -cluster ~[ -port 6111 --!verbose ] ~[ -port 6112 --!verbose ] -domains:lee-mcdonald.com [ -ssl [ --ssl-fallback --ssl-force -ssl-key /etc/letsencrypt/live/lee-mcdonald.com/privKey.pem -ssl-cert /etc/letsencrypt/live/lee-mcdonald.com/fullchain.pem ] -home /home/lee/websites/com -options [ --allow-directory --allow-write ] ] -domains:lee-mcdonald.net [ -ssl [ --ssl-fallback --ssl-force -ssl-key /etc/letsencrypt/live/lee-mcdonald.net/privKey.pem -ssl-cert /etc/letsencrypt/live/lee-mcdonald.net/fullchain.pem ] -home /home/lee/websites/net -options [ --!allow-directory --allow-write ] ]',
static: true,
verbose: true,
force: true,
port: 6110,
home: '/opt/websites',
cluster: [
CLO { verbose: false, port: 6111 },
CLO { verbose: false, port: 6112 }
],
domains: {
'lee-mcdonald.com': CLO {
ssl: CLO {
'ssl-fallback': true,
'ssl-force': true,
'ssl-key': '/etc/letsencrypt/live/lee-mcdonald.com/privKey.pem',
'ssl-cert': '/etc/letsencrypt/live/lee-mcdonald.com/fullchain.pem'
},
home: '/home/lee/websites/com',
options: CLO { 'allow-directory': true, 'allow-write': true }
},
'lee-mcdonald.net': CLO {
ssl: CLO {
'ssl-fallback': true,
'ssl-force': true,
'ssl-key': '/etc/letsencrypt/live/lee-mcdonald.net/privKey.pem',
'ssl-cert': '/etc/letsencrypt/live/lee-mcdonald.net/fullchain.pem'
},
home: '/home/lee/websites/net',
options: CLO { 'allow-directory': false, 'allow-write': true }
}
}
}
Usage
Install via NPM npm i commandlineobjects
const CLO = require('commandlineobjects')
const cmdl = new CLO // process.argv.slice(2).join(' ')
const cuso = new CLO('--secure --!verbose') // Via String.
console.log(cmdl, cuso)