Skip to content

Commit 660f3e0

Browse files
authored
Merge pull request #409 from toddbluhm/feat-combine-f-r-flags
Remove `-r` flag and use only `-f` flag
2 parents 2a76925 + abf4cca commit 660f3e0

6 files changed

Lines changed: 29 additions & 24 deletions

File tree

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ To use a custom env filename or path, pass the `-f` flag. This is a major breaki
5757
Usage: env-cmd [options] -- <command> [...args]
5858
5959
Options:
60-
-v, --version output the version number
61-
-e, --environments [env1,env2,...] The rc file environment(s) to use
62-
-f, --file [path] Custom env file path (default path: ./.env)
63-
--fallback Fallback to default env file path, if custom env file path not found
64-
--no-override Do not override existing environment variables
65-
-r, --rc-file [path] Custom rc file path (default path: ./.env-cmdrc(|.js|.json)
66-
--silent Ignore any env-cmd errors and only fail on executed program failure.
67-
--use-shell Execute the command in a new shell with the given environment
68-
--verbose Print helpful debugging information
69-
-x, --expand-envs Replace $var in args and command with environment variables
70-
-h, --help output usage information
60+
-v, --version output the version number
61+
-e, --environments [envs...] The rc file environment(s) to use
62+
-f, --file [path] Custom env file path or .rc file path if '-e' used (default path: ./.env or
63+
./.env-cmdrc.(js|cjs|mjs|json))
64+
-x, --expand-envs Replace $var in args and command with environment variables
65+
--fallback Fallback to default env file path, if custom env file path not found
66+
--no-override Do not override existing environment variables
67+
--silent Ignore any env-cmd errors and only fail on executed program failure.
68+
--use-shell Execute the command in a new shell with the given environment
69+
--verbose Print helpful debugging information
70+
-h, --help display help for command
7171
```
7272

7373
## 🔬 Advanced Usage

dist/parse-args.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Command } from '@commander-js/extra-typings';
1+
import { Command, Option, CommanderError } from '@commander-js/extra-typings';
22
import { parseArgList } from './utils.js';
33
import packageJson from '../package.json' with { type: 'json' };
44
/**
@@ -43,9 +43,9 @@ export function parseArgs(args) {
4343
rc = {
4444
environments: parsedCmdOptions.environments,
4545
// if we get a boolean value assume not defined
46-
filePath: parsedCmdOptions.rcFile === true ?
46+
filePath: parsedCmdOptions.file === true ?
4747
undefined :
48-
parsedCmdOptions.rcFile,
48+
parsedCmdOptions.file,
4949
};
5050
}
5151
let envFile;
@@ -82,14 +82,17 @@ export function parseArgsUsingCommander(args) {
8282
.version(packageJson.version, '-v, --version')
8383
.usage('[options] -- <command> [...args]')
8484
.option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList)
85-
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
86-
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc.(js|cjs|mjs|json)')
85+
.option('-f, --file [path]', 'Custom env file path or .rc file path if \'-e\' used (default path: ./.env or ./.env-cmdrc.(js|cjs|mjs|json))')
8786
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
8887
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
8988
.option('--no-override', 'Do not override existing environment variables')
9089
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
9190
.option('--use-shell', 'Execute the command in a new shell with the given environment')
9291
.option('--verbose', 'Print helpful debugging information')
92+
// TODO: Remove -r deprecation error on version >= v12
93+
.addOption(new Option('-r, --rc-file [path]', 'Deprecated Option')
94+
.hideHelp()
95+
.argParser(() => { throw new CommanderError(1, 'deprecated-option', 'The -r flag has been deprecated, use the -f flag instead.'); }))
9396
.allowUnknownOption(true)
9497
.allowExcessArguments(true)
9598
.parse(['_', '_', ...args], { from: 'node' });

dist/types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export type CommanderOptions = Command<[], {
77
fallback?: boolean;
88
file?: true | string;
99
override?: boolean;
10-
rcFile?: true | string;
1110
silent?: boolean;
1211
useShell?: boolean;
1312
verbose?: boolean;

src/parse-args.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Command } from '@commander-js/extra-typings'
1+
import { Command, Option, CommanderError } from '@commander-js/extra-typings'
22
import type { EnvCmdOptions, CommanderOptions, EnvFileOptions, RCFileOptions } from './types.ts'
33
import { parseArgList } from './utils.js'
44
import packageJson from '../package.json' with { type: 'json' }
@@ -51,9 +51,9 @@ export function parseArgs(args: string[]): EnvCmdOptions {
5151
rc = {
5252
environments: parsedCmdOptions.environments,
5353
// if we get a boolean value assume not defined
54-
filePath: parsedCmdOptions.rcFile === true ?
54+
filePath: parsedCmdOptions.file === true ?
5555
undefined :
56-
parsedCmdOptions.rcFile,
56+
parsedCmdOptions.file,
5757
}
5858
}
5959

@@ -88,19 +88,23 @@ export function parseArgs(args: string[]): EnvCmdOptions {
8888
}
8989

9090
export function parseArgsUsingCommander(args: string[]): CommanderOptions {
91+
9192
return new Command('env-cmd')
9293
.description('CLI for executing commands using an environment from an env file.')
9394
.version(packageJson.version, '-v, --version')
9495
.usage('[options] -- <command> [...args]')
9596
.option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList)
96-
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
97-
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc.(js|cjs|mjs|json)')
97+
.option('-f, --file [path]', 'Custom env file path or .rc file path if \'-e\' used (default path: ./.env or ./.env-cmdrc.(js|cjs|mjs|json))')
9898
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
9999
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
100100
.option('--no-override', 'Do not override existing environment variables')
101101
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
102102
.option('--use-shell', 'Execute the command in a new shell with the given environment')
103103
.option('--verbose', 'Print helpful debugging information')
104+
// TODO: Remove -r deprecation error on version >= v12
105+
.addOption(new Option('-r, --rc-file [path]', 'Deprecated Option')
106+
.hideHelp()
107+
.argParser(() => { throw new CommanderError(1, 'deprecated-option', 'The -r flag has been deprecated, use the -f flag instead.') }))
104108
.allowUnknownOption(true)
105109
.allowExcessArguments(true)
106110
.parse(['_', '_', ...args], { from: 'node' })

src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type CommanderOptions = Command<[], {
1111
fallback?: boolean // Default false
1212
file?: true | string
1313
override?: boolean // Default: false
14-
rcFile?: true | string
1514
silent?: boolean // Default: false
1615
useShell?: boolean // Default: false
1716
verbose?: boolean // Default: false

test/parse-args.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('parseArgs', (): void => {
6969
})
7070

7171
it('should parse rc file path', (): void => {
72-
const res = parseArgs(['-e', environments[0], '-r', rcFilePath, '--', command, ...commandArgs])
72+
const res = parseArgs(['-e', environments[0], '-f', rcFilePath, '--', command, ...commandArgs])
7373
assert.exists(res.rc)
7474
assert.equal(res.rc.filePath, rcFilePath)
7575
})

0 commit comments

Comments
 (0)