Skip to content

Commit

Permalink
chore: cleaner cli
Browse files Browse the repository at this point in the history
  • Loading branch information
CosminPerRam committed Jan 11, 2024
1 parent 0b8ebc8 commit 688dd02
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions bin/gamedig.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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]
Expand All @@ -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(':')
Expand All @@ -38,19 +28,24 @@ 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 = argv.debug
const pretty = !!argv.pretty || debug
const givenPortOnly = argv.givenPortOnly
const requestRulesRequired = argv.requestRulesRequired
const requestPlayersRequired = argv.requestPlayersRequired

applyBoolean(debug, 'debug')
applyBoolean(givenPortOnly, 'givenPortOnly')
applyBoolean(requestRulesRequired, 'requestRulesRequired')
applyBoolean(requestPlayersRequired, 'requestPlayersRequired')

const printOnPretty = (object) => {
if (pretty) {
console.log(JSON.stringify(object, null, ' '))
Expand Down

0 comments on commit 688dd02

Please sign in to comment.