Commit 90193df2 authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub

Merge pull request #4401 from ethereum-optimism/sc/cmn-use-env-argv

feat(cmn): allow BSv2 to ignore env/argv
parents ae951300 eceb0de1
---
'@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< ...@@ -158,6 +158,11 @@ export abstract class BaseServiceV2<
// commander for anything besides the ability to run `ts-node ./service.ts --help`. // commander for anything besides the ability to run `ts-node ./service.ts --help`.
const program = new Command() const program = new Command()
for (const [optionName, optionSpec] of Object.entries(params.optionsSpec)) { 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( program.addOption(
new Option(`--${optionName.toLowerCase()}`, `${optionSpec.desc}`).env( new Option(`--${optionName.toLowerCase()}`, `${optionSpec.desc}`).env(
`${opSnakeCase( `${opSnakeCase(
...@@ -197,8 +202,8 @@ export abstract class BaseServiceV2< ...@@ -197,8 +202,8 @@ export abstract class BaseServiceV2<
dotenv.config() dotenv.config()
const config = new Config(params.name) const config = new Config(params.name)
config.load({ config.load({
env: true, env: params.options?.useEnv ?? true,
argv: true, argv: params.options?.useEnv ?? true,
}) })
// Clean configuration values using the options spec. // Clean configuration values using the options spec.
......
...@@ -30,6 +30,8 @@ export type StandardOptions = { ...@@ -30,6 +30,8 @@ export type StandardOptions = {
port?: number port?: number
hostname?: string hostname?: string
logLevel?: LogLevel logLevel?: LogLevel
useEnv?: boolean
useArgv?: boolean
} }
/** /**
...@@ -60,6 +62,18 @@ export const stdOptionsSpec: OptionsSpec<StandardOptions> = { ...@@ -60,6 +62,18 @@ export const stdOptionsSpec: OptionsSpec<StandardOptions> = {
default: 'debug', default: 'debug',
public: true, 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