this repo has no description
at main 85 lines 2.7 kB view raw
1import assert from 'node:assert/strict' 2import { describe, test } from 'node:test' 3import { parseArgs } from 'node:util' 4 5describe('CLI Argument Parsing', () => { 6 test('parseArgs handles basic arguments', () => { 7 const { positionals } = parseArgs({ 8 args: ['owner/repo', 'v1.0.0'], 9 options: { 10 'bin-name': { type: 'string', short: 'b' }, 11 output: { type: 'string', short: 'o' }, 12 verbose: { type: 'boolean', short: 'v' }, 13 }, 14 allowPositionals: true, 15 }) 16 17 assert.equal(positionals[0], 'owner/repo') 18 assert.equal(positionals[1], 'v1.0.0') 19 }) 20 21 test('parseArgs handles short flags', () => { 22 const { values } = parseArgs({ 23 args: ['-b', 'custom-bin', '-o', './custom-output', '-v'], 24 options: { 25 'bin-name': { type: 'string', short: 'b' }, 26 output: { type: 'string', short: 'o' }, 27 verbose: { type: 'boolean', short: 'v' }, 28 }, 29 allowPositionals: true, 30 }) 31 32 assert.equal(values['bin-name'], 'custom-bin') 33 assert.equal(values.output, './custom-output') 34 assert.equal(values.verbose, true) 35 }) 36 37 test('parseArgs handles long flags', () => { 38 const { values } = parseArgs({ 39 args: ['--bin-name', 'custom-bin', '--output', './custom-output', '--verbose'], 40 options: { 41 'bin-name': { type: 'string', short: 'b' }, 42 output: { type: 'string', short: 'o' }, 43 verbose: { type: 'boolean', short: 'v' }, 44 }, 45 allowPositionals: true, 46 }) 47 48 assert.equal(values['bin-name'], 'custom-bin') 49 assert.equal(values.output, './custom-output') 50 assert.equal(values.verbose, true) 51 }) 52 53 test('parseArgs handles platform-map JSON', () => { 54 const platformMap = '{"darwin-x64":"app-macos.tar.gz"}' 55 const { values } = parseArgs({ 56 args: ['--platform-map', platformMap], 57 options: { 58 'platform-map': { type: 'string', short: 'p' }, 59 }, 60 allowPositionals: true, 61 }) 62 63 assert.equal(values['platform-map'], platformMap) 64 65 // Test JSON parsing 66 const parsed = JSON.parse(values['platform-map']) 67 assert.equal(parsed['darwin-x64'], 'app-macos.tar.gz') 68 }) 69 70 test('parseArgs handles mixed positional and option args', () => { 71 const { values, positionals } = parseArgs({ 72 args: ['owner/repo', 'v1.0.0', '--verbose', '--output', './bin'], 73 options: { 74 output: { type: 'string', short: 'o' }, 75 verbose: { type: 'boolean', short: 'v' }, 76 }, 77 allowPositionals: true, 78 }) 79 80 assert.equal(positionals[0], 'owner/repo') 81 assert.equal(positionals[1], 'v1.0.0') 82 assert.equal(values.output, './bin') 83 assert.equal(values.verbose, true) 84 }) 85})