Commit eceb0de1 authored by Kelvin Fichter's avatar Kelvin Fichter

feat(cmn): allow BSv2 to ignore env/argv

Introduces new standard options that allow users to ignore variables
from the environment or the command line. Useful for programmatic usage
where there may be unrelated environment variables or command line
arguments that get parsed by BSv2 by accident.
parent c68fb1e2
---
'@eth-optimism/common-ts': patch
---
Adds new standard options to disable parsing variables from environment and command line.
......@@ -158,6 +158,11 @@ export abstract class BaseServiceV2<
// commander for anything besides the ability to run `ts-node ./service.ts --help`.
const program = new Command()
for (const [optionName, optionSpec] of Object.entries(params.optionsSpec)) {
// Skip options that are not meant to be used by the user.
if (['useEnv', 'useArgv'].includes(optionName)) {
continue
}
program.addOption(
new Option(`--${optionName.toLowerCase()}`, `${optionSpec.desc}`).env(
`${opSnakeCase(
......@@ -197,8 +202,8 @@ export abstract class BaseServiceV2<
dotenv.config()
const config = new Config(params.name)
config.load({
env: true,
argv: true,
env: params.options?.useEnv ?? true,
argv: params.options?.useEnv ?? true,
})
// Clean configuration values using the options spec.
......
......@@ -30,6 +30,8 @@ export type StandardOptions = {
port?: number
hostname?: string
logLevel?: LogLevel
useEnv?: boolean
useArgv?: boolean
}
/**
......@@ -60,6 +62,18 @@ export const stdOptionsSpec: OptionsSpec<StandardOptions> = {
default: 'debug',
public: true,
},
useEnv: {
validator: validators.bool,
desc: 'For programmatic use, whether to use environment variables',
default: true,
public: true,
},
useArgv: {
validator: validators.bool,
desc: 'For programmatic use, whether to use command line arguments',
default: true,
public: true,
},
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment