Skip to content

Commit

Permalink
parse_args - handle multiple spaces as one
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaur-bndes authored and jmoalves committed Mar 22, 2023
1 parent 2b04aad commit 224887d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
14 changes: 13 additions & 1 deletion src/lib/parse_args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Deno.test('should use stringOnce option only once', () => {
})
},
Error,
'Use option stringOption only once'
'Use option --stringOption only once'
)
})

Expand Down Expand Up @@ -108,3 +108,15 @@ Deno.test('should parse arg and option', () => {
assertEquals(args._, ['firstArg'])
assertEquals(args.myOption, 'yes')
})

Deno.test('should ignore multiple spaces', () => {
const args = parseArgs(['--myOption', '', '', '', 'yes', '', '', '', 'firstArg', '', '', ''], {
stringOnce: [
'myOption'
]
})

assertEquals(args._.length, 1)
assertEquals(args._, ['firstArg'])
assertEquals(args.myOption, 'yes')
})
45 changes: 34 additions & 11 deletions src/lib/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Opts {
boolean?: string[]
}

export function parseArgs(args: string[], optsDef?: Opts): any {
export function parseArgs(pArgs: string[], optsDef?: Opts): any {
let opts: any = {
stopEarly: true,
unknown: (v: string) => {
Expand All @@ -26,6 +26,7 @@ export function parseArgs(args: string[], optsDef?: Opts): any {
opts.string.push(v);
})
}

// Handling BREAKING changes
// https://github.com/denoland/deno_std/commit/6a95e2954dd58b68dbbb246cc362a8906a01ec04
if (optsDef?.stringMany) {
Expand All @@ -41,17 +42,11 @@ export function parseArgs(args: string[], optsDef?: Opts): any {
opts.boolean = optsDef?.boolean;
}

let myArgs = parse(args, opts);
let args = normalizeArgs(pArgs)

if (optsDef?.stringOnce) {
optsDef.stringOnce.forEach((key) => {
if (myArgs[key]) {
if (typeof (myArgs[key]) != "string") {
throw new Error(`Use option ${key} only once`);
}
}
})
}
checkStringOnce(args, optsDef?.stringOnce)

let myArgs = parse(args, opts);

if (optsDef?.stringMany) {
optsDef.stringMany.forEach((key) => {
Expand Down Expand Up @@ -135,3 +130,31 @@ function countQuotes(text: string): number {
function addArg(args: string[], text: string): void {
args.push(text.replace(/"/g, ''));
}

function normalizeArgs(args: string[]): string[] {
return args
.filter(item => item)
.map(item => item.trim())
.filter(item => item.length > 0)
}

function checkStringOnce(args: string[], stringOnce: string[] | undefined) {
if (!stringOnce || !args || args.length == 0) {
return
}

let knownKeys = new Set()
let stringOnceSet = new Set(stringOnce)

args
.filter(element => element?.startsWith('-'))
.filter(element => stringOnceSet.has(element.replace(/^-{1,2}/, '')))
.forEach(option => {
if (knownKeys.has(option)) {
throw new Error(`Use option ${option} only once`)
}

knownKeys.add(option)
})
}

0 comments on commit 224887d

Please sign in to comment.