1- // Inspired by mri, https://github.com/lukeed/mri 1.2.0 MIT
2- // Rewritten in TypeScript by Ivo Dolenc, Hypernym Studio
1+ // Inspired by mri, 1.2.0, MIT License, https://github.com/lukeed/mri
2+ // Rewritten and adapted to @hypernym /args, 0.3.0, MIT License, https://github.com/hypernym-studio/args
33
4- import { isString , isArray , isFlag , isAlias } from './utils.js '
5- import type { Defaults , Args , Options } from './types/index.js '
4+ import { isString , isArray , isFlag , isAlias } from './utils'
5+ import type { Defaults , Args , Options } from './types'
66
77/**
8- * Creates a command-line argument parser for the Node process .
8+ * Creates a command-line argument parser.
99 *
1010 * @example
1111 *
@@ -24,53 +24,70 @@ import type { Defaults, Args, Options } from './types/index.js'
2424export function createArgs < T = Defaults > ( {
2525 argv = process . argv . slice ( 2 ) ,
2626 alias,
27+ defaults,
28+ exclude,
2729} : Options = { } ) : Args < T > {
2830 const args : Args = {
2931 _ : [ ] ,
3032 }
3133
32- function _setArg ( arg : string , index : number ) {
34+ const excludes = [ '--' , '-' , ...( exclude || [ ] ) ]
35+ argv = argv . filter ( ( arg ) => ! excludes . includes ( arg ) )
36+
37+ function setArg ( arg : string , index : number ) {
3338 let value : boolean | string = true
34- const argKey = isFlag ( arg ) ? arg . slice ( 2 ) : arg . slice ( 1 )
35- const argValue = argv [ index + 1 ]
39+ let argKey : string = isFlag ( arg ) ? arg . slice ( 2 ) : arg . slice ( 1 )
40+ let argValue : string
41+
42+ if ( arg . includes ( '=' ) ) {
43+ const split = argKey . split ( '=' )
44+ argKey = split [ 0 ]
45+ argValue = split [ 1 ]
46+ } else {
47+ argValue = argv [ index + 1 ]
48+ }
3649
3750 if ( argValue && ! argValue . startsWith ( '-' ) ) value = argValue
3851
39- if ( argKey && ! argKey . includes ( '=' ) ) {
40- if ( alias ) {
41- for ( const [ aliasKey , aliasValue ] of Object . entries ( alias ) ) {
42- if ( aliasKey . includes ( argKey ) || aliasValue . includes ( argKey ) ) {
43- args [ aliasKey ] = value
44- if ( isString ( aliasValue ) ) args [ aliasValue ] = value
45- if ( isArray ( aliasValue ) ) for ( const v of aliasValue ) args [ v ] = value
46- }
52+ if ( alias ) {
53+ for ( const [ aliasKey , aliasValue ] of Object . entries ( alias ) ) {
54+ if ( aliasKey . includes ( argKey ) || aliasValue . includes ( argKey ) ) {
55+ args [ aliasKey ] = value
56+ if ( isString ( aliasValue ) ) args [ aliasValue ] = value
57+ if ( isArray ( aliasValue ) ) for ( const v of aliasValue ) args [ v ] = value
58+ return
4759 }
4860 }
49-
50- args [ argKey ] = value
5161 }
62+
63+ args [ argKey ] = value
5264 }
5365
54- for ( const [ index , arg ] of argv . entries ( ) ) {
55- // flags '--'
56- if ( isFlag ( arg ) ) _setArg ( arg , index )
57- // aliases '- '
58- else if ( isAlias ( arg ) ) _setArg ( arg , index )
59- // unprefixed values
66+ for ( let i = 0 , l = argv . length ; i < l ; i ++ ) {
67+ const arg = argv [ i ]
68+
69+ // '--flags' and '-alias '
70+ if ( isFlag ( arg ) || isAlias ( arg ) ) setArg ( arg , i )
71+ // 'arguments' (unprefixed)
6072 else {
61- const _arg = argv [ index - 1 ]
73+ const prevArg : string | undefined = argv [ i - 1 ]
6274
63- if ( ! _arg ) {
64- if ( ! arg . startsWith ( '-' ) && ! arg . includes ( '=' ) ) {
65- args . _ . push ( arg )
75+ if ( ! prevArg ) {
76+ if ( ! arg . includes ( '=' ) ) args . _ . push ( arg )
77+ } else if ( prevArg ) {
78+ if ( ! prevArg . startsWith ( '-' ) || prevArg . includes ( '=' ) ) {
79+ if ( ! arg . includes ( '=' ) ) args . _ . push ( arg )
6680 }
67- } else if ( ! _arg . startsWith ( '-' ) && ! arg . includes ( '=' ) ) {
68- args . _ . push ( arg )
69- } else if ( ( _arg === '-' || _arg === '--' ) && ! arg . includes ( '=' ) ) {
70- args . _ . push ( arg )
7181 }
7282 }
7383 }
7484
85+ if ( defaults ) {
86+ if ( defaults . _ ) args . _ = [ ...defaults . _ , ...args . _ ]
87+ for ( const [ dKey , dValue ] of Object . entries ( defaults ) ) {
88+ if ( dKey !== '_' ) args [ dKey ] = dValue
89+ }
90+ }
91+
7592 return args as Args < T >
7693}
0 commit comments