mirror of
https://github.com/gamedig/node-gamedig.git
synced 2024-11-16 00:38:31 +01:00
chore: cleaner cli (#459)
* chore: cleaner cli * chore: use destruction for argv
This commit is contained in:
parent
944716e453
commit
0a3338320e
1 changed files with 15 additions and 24 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import * as process from "node:process";
|
||||
import * as process from 'node:process'
|
||||
|
||||
import Minimist from 'minimist'
|
||||
import { GameDig } from './../lib/index.js'
|
||||
|
@ -10,17 +10,6 @@ const argv = Minimist(process.argv.slice(2), {
|
|||
string: ['guildId', 'listenUdpPort', 'ipFamily']
|
||||
})
|
||||
|
||||
const debug = argv.debug
|
||||
delete argv.debug
|
||||
const pretty = !!argv.pretty || debug
|
||||
delete argv.pretty
|
||||
const givenPortOnly = argv.givenPortOnly
|
||||
delete argv.givenPortOnly
|
||||
const requestRulesRequired = argv.requestRulesRequired
|
||||
delete argv.requestRulesRequired
|
||||
const requestPlayersRequired = argv.requestPlayersRequired
|
||||
delete argv.requestPlayersRequired
|
||||
|
||||
const options = {}
|
||||
for (const key of Object.keys(argv)) {
|
||||
const value = argv[key]
|
||||
|
@ -30,6 +19,7 @@ for (const key of Object.keys(argv)) {
|
|||
options[key] = value
|
||||
}
|
||||
|
||||
// Separate host and port
|
||||
if (argv._.length >= 1) {
|
||||
const target = argv._[0]
|
||||
const split = target.split(':')
|
||||
|
@ -38,21 +28,22 @@ if (argv._.length >= 1) {
|
|||
options.port = split[1]
|
||||
}
|
||||
}
|
||||
if (debug) {
|
||||
options.debug = true
|
||||
}
|
||||
if (givenPortOnly) {
|
||||
options.givenPortOnly = true
|
||||
}
|
||||
if (requestRulesRequired) {
|
||||
options.requestRulesRequired = true
|
||||
}
|
||||
if (requestPlayersRequired) {
|
||||
options.requestPlayersRequired = true
|
||||
|
||||
const applyBoolean = (value, fieldName, defaultValue = true) => {
|
||||
if (value) {
|
||||
options[fieldName] = defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
const { debug, pretty, givenPortOnly, requestRulesRequired, requestPlayersRequired } = argv
|
||||
|
||||
applyBoolean(debug, 'debug')
|
||||
applyBoolean(givenPortOnly, 'givenPortOnly')
|
||||
applyBoolean(requestRulesRequired, 'requestRulesRequired')
|
||||
applyBoolean(requestPlayersRequired, 'requestPlayersRequired')
|
||||
|
||||
const printOnPretty = (object) => {
|
||||
if (pretty) {
|
||||
if (!!pretty || debug) {
|
||||
console.log(JSON.stringify(object, null, ' '))
|
||||
} else {
|
||||
console.log(JSON.stringify(object))
|
||||
|
|
Loading…
Reference in a new issue