Created
February 25, 2026 21:20
-
-
Save alanning/e3fb9343e020008cb322da22e28a3175 to your computer and use it in GitHub Desktop.
Simple function to parse long options from argv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { expect } from 'chai' | |
| import { parseArgvLongOptions } from './parseArgvLongOptions.ts' | |
| describe('parseArgv', function () { | |
| it('parses --key value pairs', function () { | |
| const argv = ['/usr/local/bin/node', 'script.ts', '--stage', 'qa'] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({ stage: 'qa' }) | |
| }) | |
| it('parses key=value pairs', function () { | |
| const argv = ['/usr/local/bin/node', 'script.ts', 'stage=qa'] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({ stage: 'qa' }) | |
| }) | |
| it('parses multiple --key value pairs', function () { | |
| const argv = [ | |
| '/usr/local/bin/node', 'script.ts', | |
| '--stage', 'qa', | |
| '--region', 'us-east-1', | |
| ] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({ | |
| stage: 'qa', | |
| region: 'us-east-1', | |
| }) | |
| }) | |
| it('parses multiple key=value pairs', function () { | |
| const argv = [ | |
| '/usr/local/bin/node', 'script.ts', | |
| 'stage=qa', | |
| 'region=us-east-1', | |
| ] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({ | |
| stage: 'qa', | |
| region: 'us-east-1', | |
| }) | |
| }) | |
| it('parses mixed --key value and key=value pairs', function () { | |
| const argv = [ | |
| '/usr/local/bin/node', 'script.ts', | |
| '--stage', 'qa', | |
| 'region=us-east-1', | |
| ] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({ | |
| stage: 'qa', | |
| region: 'us-east-1', | |
| }) | |
| }) | |
| it('returns empty object when no arguments are provided', function () { | |
| const argv = ['/usr/local/bin/node', 'script.ts'] | |
| expect(parseArgvLongOptions(argv)).to.deep.equal({}) | |
| }) | |
| it('returns empty object for empty argv', function () { | |
| expect(parseArgvLongOptions([])).to.deep.equal({}) | |
| }) | |
| it('sets value to undefined for --key at end of argv', function () { | |
| const argv = ['/usr/local/bin/node', 'script.ts', '--stage'] | |
| const result = parseArgvLongOptions(argv) | |
| expect(result).to.have.property('stage') | |
| expect(result.stage).to.equal(undefined) | |
| }) | |
| it('only splits key=value on first equals sign', function () { | |
| const argv = ['/usr/local/bin/node', 'script.ts', 'url=http://example.com?a=1'] | |
| const result = parseArgvLongOptions(argv) | |
| expect(result.url).to.equal('http://example.com?a=1') | |
| }) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Matches cli long options: --stage=qa or --stage qa | |
| // Does not match short options: -v | |
| export function parseArgvLongOptions (argv: string[]): Record<string, string> { | |
| const result: Record<string, string> = {} | |
| argv.forEach((arg, i) => { | |
| if (arg.includes('=')) { | |
| // Use indexOf vs split to support cases like this: | |
| // node script.ts url=http://example.com?a=1 | |
| const indexOfEquals = arg.indexOf('=') | |
| let key = arg.substring(0, indexOfEquals) | |
| const value = arg.substring(indexOfEquals + 1) | |
| if (key.startsWith('--')) { | |
| key = key.substring(2) | |
| } | |
| result[key] = value | |
| } else if (arg.startsWith('--')) { | |
| // next argument is the value | |
| const value = argv[i + 1] | |
| const key = arg.substring(2) | |
| result[key] = value | |
| } | |
| }) | |
| return result | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment