Skip to content

Commit

Permalink
feat(internal): add args handling
Browse files Browse the repository at this point in the history
  • Loading branch information
renejfc committed Apr 10, 2024
1 parent 85da344 commit f3c23c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TODO: not really the most optimal api, will refactor later

type ParsedArgs<T extends [[string, string], string][]> = Map<T[number][0][0], boolean>

export const parseArgs = <const T extends [[string, string], string][]>(args: T): ParsedArgs<T> => {
const LONG_PREFIX = "--"
const SHORT_PREFIX = "-"

const bunArgs = new Set(Bun.argv.slice(2))
const mappedArgs = new Map<string, boolean>()

for (const [[long, short]] of args) {
const longPrefixed = LONG_PREFIX + long
const shortPrefixed = SHORT_PREFIX + short

if (!mappedArgs.has(long) && (bunArgs.has(longPrefixed) || bunArgs.has(shortPrefixed))) mappedArgs.set(long, true)
else mappedArgs.set(long, false)
}

return mappedArgs
}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./args"

0 comments on commit f3c23c3

Please sign in to comment.