From b7367f599c9955eb5140d56967a24291d5522393 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 22 Oct 2020 20:35:10 -0600 Subject: [PATCH] Parse command line arguments --- src/constants.ts | 17 +---------------- src/main.ts | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 601884f..9ee6fb8 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,16 +1 @@ -/* -The level for the logger to display, can be one of the following: - "silly" - "debug" - "info" - "error" - "warn" - "fatal" - "trace" -defaults to "info" -*/ -export const LOG_LEVEL: log_level = `silly`; -export const LOG_NAME: string = `Secret Hitler`; - - -export const WSS_PORT: number = 3000; \ No newline at end of file +export const LOG_NAME: string = `Secret Hitler`; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 6f15c38..d5794c2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,10 @@ +import arg from "arg"; import { Logger } from "tslog"; import * as sio from "socket.io"; import { writeFileSync } from "fs"; import { Game } from "./utils/Game"; import { Vote } from "./events/Vote"; +import { LOG_NAME } from "./constants"; import { KillGame } from "./events/KillGame"; import { JoinGame } from "./events/JoinGame"; import { HostGame } from "./events/HostGame"; @@ -18,14 +20,32 @@ import { ChancellorPolicy } from "./events/ChancellorPolicy"; import { PresidentPolicies } from "./events/PresidentPolicies"; import { ChancellorNomination } from "./events/ChancellorNomination"; import { ExecutiveConfirmation } from "./events/ExecutiveConfirmation"; -import { LOG_LEVEL, LOG_NAME, WSS_PORT } from "./constants"; + + +// Argument parsing +const args = arg({ + "--port": Number, + "--log": String, + + // Aliases + "-l": "--log", + "-p": "--port", + "--level": "--log" +}); +const LOG_LEVEL: log_level = args["--log"] as log_level || `info`; +const WSS_PORT = args["--port"] || 3000; + +// Ensure the log level is valid +if (![`silly`,`debug`,`info`,`error`,`warn`,`fatal`,`trace`].includes(LOG_LEVEL)) { + throw new Error(`Invalid log level: ${LOG_LEVEL}`) +} export var log: Logger = new Logger({ displayFunctionName: false, displayLoggerName: false, displayFilePath: `hidden`, displayLogLevel: true, - minLevel: LOG_LEVEL || `info`, + minLevel: LOG_LEVEL, name: LOG_NAME, });