args-tokens 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.d.ts +3 -64
- package/lib/cjs/index.js +18 -226
- package/lib/cjs/parser.d.ts +77 -0
- package/lib/cjs/parser.js +230 -0
- package/lib/cjs/resolver.d.ts +50 -0
- package/lib/cjs/resolver.js +167 -0
- package/lib/cjs/resolver.test-d.d.ts +1 -0
- package/lib/cjs/resolver.test-d.js +6 -0
- package/lib/esm/index.d.ts +3 -64
- package/lib/esm/index.js +2 -225
- package/lib/esm/parser.d.ts +77 -0
- package/lib/esm/parser.js +225 -0
- package/lib/esm/resolver.d.ts +50 -0
- package/lib/esm/resolver.js +164 -0
- package/lib/esm/resolver.test-d.d.ts +1 -0
- package/lib/esm/resolver.test-d.js +4 -0
- package/package.json +14 -2
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Modifier: kazuya kawaguchi (a.k.a. kazupon)
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.resolveArgs = resolveArgs;
|
|
6
|
+
const parser_js_1 = require("./parser.js");
|
|
7
|
+
function resolveArgs(options, tokens) {
|
|
8
|
+
const values = {};
|
|
9
|
+
const positionals = [];
|
|
10
|
+
const longOptionTokens = [];
|
|
11
|
+
const shortOptionTokens = [];
|
|
12
|
+
let currentShortOption;
|
|
13
|
+
const expandableShortOptions = [];
|
|
14
|
+
function toValue() {
|
|
15
|
+
if (expandableShortOptions.length === 0) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const value = expandableShortOptions.map(token => token.name).join('');
|
|
20
|
+
expandableShortOptions.length = 0;
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function applyShortOptionValue() {
|
|
25
|
+
if (currentShortOption) {
|
|
26
|
+
currentShortOption.value = toValue();
|
|
27
|
+
shortOptionTokens.push({ ...currentShortOption });
|
|
28
|
+
currentShortOption = undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* separate tokens into positionals, long options, and short options, after that resolve values
|
|
33
|
+
*/
|
|
34
|
+
// eslint-disable-next-line unicorn/no-for-loop
|
|
35
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
36
|
+
const token = tokens[i];
|
|
37
|
+
if (token.kind === 'positional') {
|
|
38
|
+
positionals.push(token.value);
|
|
39
|
+
applyShortOptionValue(); // check if previous short option is not resolved
|
|
40
|
+
}
|
|
41
|
+
else if (token.kind === 'option') {
|
|
42
|
+
if (token.rawName) {
|
|
43
|
+
if ((0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
|
|
44
|
+
longOptionTokens.push({ ...token });
|
|
45
|
+
applyShortOptionValue(); // check if previous short option is not resolved
|
|
46
|
+
}
|
|
47
|
+
else if ((0, parser_js_1.isShortOption)(token.rawName)) {
|
|
48
|
+
if (currentShortOption) {
|
|
49
|
+
if (currentShortOption.index === token.index) {
|
|
50
|
+
expandableShortOptions.push({ ...token });
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
currentShortOption.value = toValue();
|
|
54
|
+
shortOptionTokens.push({ ...currentShortOption });
|
|
55
|
+
currentShortOption = { ...token };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
currentShortOption = { ...token };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// short option value
|
|
65
|
+
if (currentShortOption && currentShortOption.index == token.index) {
|
|
66
|
+
currentShortOption.value = token.value;
|
|
67
|
+
shortOptionTokens.push({ ...currentShortOption });
|
|
68
|
+
currentShortOption = undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
applyShortOptionValue(); // check if previous short option is not resolved
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* check if the last short option is not resolved
|
|
78
|
+
*/
|
|
79
|
+
applyShortOptionValue();
|
|
80
|
+
/**
|
|
81
|
+
* resolve values
|
|
82
|
+
*/
|
|
83
|
+
for (const [option, schema] of Object.entries(options)) {
|
|
84
|
+
if (longOptionTokens.length === 0 && shortOptionTokens.length === 0 && schema.required) {
|
|
85
|
+
throw createRequireError(option, schema);
|
|
86
|
+
}
|
|
87
|
+
// eslint-disable-next-line unicorn/no-for-loop
|
|
88
|
+
for (let i = 0; i < longOptionTokens.length; i++) {
|
|
89
|
+
const token = longOptionTokens[i];
|
|
90
|
+
// eslint-disable-next-line unicorn/no-null
|
|
91
|
+
if (option === token.name && token.rawName != null && (0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
|
|
92
|
+
validateRequire(token, option, schema);
|
|
93
|
+
if (schema.type !== 'boolean') {
|
|
94
|
+
validateValue(token, option, schema);
|
|
95
|
+
}
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
97
|
+
;
|
|
98
|
+
values[option] = resolveOptionValue(token, schema);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// eslint-disable-next-line unicorn/no-for-loop
|
|
103
|
+
for (let i = 0; i < shortOptionTokens.length; i++) {
|
|
104
|
+
const token = shortOptionTokens[i];
|
|
105
|
+
// eslint-disable-next-line unicorn/no-null
|
|
106
|
+
if (schema.short === token.name && token.rawName != null && (0, parser_js_1.isShortOption)(token.rawName)) {
|
|
107
|
+
validateRequire(token, option, schema);
|
|
108
|
+
if (schema.type !== 'boolean') {
|
|
109
|
+
validateValue(token, option, schema);
|
|
110
|
+
}
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
112
|
+
;
|
|
113
|
+
values[option] = resolveOptionValue(token, schema);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// eslint-disable-next-line unicorn/no-null
|
|
118
|
+
if (values[option] == null && schema.default != null) {
|
|
119
|
+
// check if the default value is in values
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
121
|
+
;
|
|
122
|
+
values[option] = schema.default;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { values, positionals };
|
|
126
|
+
}
|
|
127
|
+
function createRequireError(option, schema) {
|
|
128
|
+
return new Error(`Option '--${option}' ${schema.short ? `or '-${schema.short}'` : ''} is required`);
|
|
129
|
+
}
|
|
130
|
+
function validateRequire(token, option, schema) {
|
|
131
|
+
if (schema.required && schema.type !== 'boolean' && !token.value) {
|
|
132
|
+
throw createRequireError(option, schema);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function validateValue(token, option, schema) {
|
|
136
|
+
switch (schema.type) {
|
|
137
|
+
case 'number': {
|
|
138
|
+
if (!isNumeric(token.value)) {
|
|
139
|
+
throw createTypeError(option, schema);
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'string': {
|
|
144
|
+
if (typeof token.value !== 'string') {
|
|
145
|
+
throw createTypeError(option, schema);
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function isNumeric(str) {
|
|
152
|
+
// @ts-ignore
|
|
153
|
+
// eslint-disable-next-line unicorn/prefer-number-properties
|
|
154
|
+
return str.trim() !== '' && !isNaN(str);
|
|
155
|
+
}
|
|
156
|
+
function createTypeError(option, schema) {
|
|
157
|
+
return new TypeError(`Option '--${option}' ${schema.short ? `or '-${schema.short}'` : ''} should be '${schema.type}'`);
|
|
158
|
+
}
|
|
159
|
+
function resolveOptionValue(token, schema) {
|
|
160
|
+
if (token.value) {
|
|
161
|
+
return schema.type === 'number' ? +token.value : token.value;
|
|
162
|
+
}
|
|
163
|
+
if (schema.type === 'boolean') {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return schema.type === 'number' ? +(schema.default || '') : schema.default;
|
|
167
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,64 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
5
|
-
* - `positional`: positional token
|
|
6
|
-
*/
|
|
7
|
-
export type ArgTokenKind = 'option' | 'option-terminator' | 'positional';
|
|
8
|
-
/**
|
|
9
|
-
* Argument token
|
|
10
|
-
*/
|
|
11
|
-
export interface ArgToken {
|
|
12
|
-
/**
|
|
13
|
-
* Argument token kind
|
|
14
|
-
*/
|
|
15
|
-
kind: ArgTokenKind;
|
|
16
|
-
/**
|
|
17
|
-
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1
|
|
18
|
-
*/
|
|
19
|
-
index: number;
|
|
20
|
-
/**
|
|
21
|
-
* Option name, e.g. `--foo` => `foo`, `-x` => `x`
|
|
22
|
-
*/
|
|
23
|
-
name?: string;
|
|
24
|
-
/**
|
|
25
|
-
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`
|
|
26
|
-
*/
|
|
27
|
-
rawName?: string;
|
|
28
|
-
/**
|
|
29
|
-
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
30
|
-
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
31
|
-
*/
|
|
32
|
-
value?: string;
|
|
33
|
-
/**
|
|
34
|
-
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
35
|
-
*/
|
|
36
|
-
inlineValue?: boolean;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Parse Options
|
|
40
|
-
*/
|
|
41
|
-
export interface ParseOptions {
|
|
42
|
-
/**
|
|
43
|
-
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode
|
|
44
|
-
* @default false
|
|
45
|
-
*/
|
|
46
|
-
allowCompatible?: boolean;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Parse command line arguments
|
|
50
|
-
* @example
|
|
51
|
-
* ```js
|
|
52
|
-
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
53
|
-
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
54
|
-
*
|
|
55
|
-
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
56
|
-
* // do something with using tokens
|
|
57
|
-
* // ...
|
|
58
|
-
* console.log('tokens:', tokens)
|
|
59
|
-
* ```
|
|
60
|
-
* @param args command line arguments
|
|
61
|
-
* @param options parse options
|
|
62
|
-
* @returns argument tokens
|
|
63
|
-
*/
|
|
64
|
-
export declare function parseArgs(args: string[], options?: ParseOptions): ArgToken[];
|
|
1
|
+
export { parseArgs } from './parser.js';
|
|
2
|
+
export * from './resolver.js';
|
|
3
|
+
export type { ArgToken, ParseOptions } from './parser';
|
package/lib/esm/index.js
CHANGED
|
@@ -1,225 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// Forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
4
|
-
// Repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
5
|
-
// Author: Node.js contributors
|
|
6
|
-
// Code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
7
|
-
const HYPHEN_CHAR = '-';
|
|
8
|
-
const HYPHEN_CODE = HYPHEN_CHAR.codePointAt(0);
|
|
9
|
-
const EQUAL_CHAR = '=';
|
|
10
|
-
const EQUAL_CODE = EQUAL_CHAR.codePointAt(0);
|
|
11
|
-
const TERMINATOR = '--';
|
|
12
|
-
const SHORT_OPTION_PREFIX = HYPHEN_CHAR;
|
|
13
|
-
const LONG_OPTION_PREFIX = '--';
|
|
14
|
-
/**
|
|
15
|
-
* Parse command line arguments
|
|
16
|
-
* @example
|
|
17
|
-
* ```js
|
|
18
|
-
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
19
|
-
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
20
|
-
*
|
|
21
|
-
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
22
|
-
* // do something with using tokens
|
|
23
|
-
* // ...
|
|
24
|
-
* console.log('tokens:', tokens)
|
|
25
|
-
* ```
|
|
26
|
-
* @param args command line arguments
|
|
27
|
-
* @param options parse options
|
|
28
|
-
* @returns argument tokens
|
|
29
|
-
*/
|
|
30
|
-
export function parseArgs(args, options = {}) {
|
|
31
|
-
const { allowCompatible = false } = options;
|
|
32
|
-
const tokens = [];
|
|
33
|
-
const remainings = [...args];
|
|
34
|
-
let index = -1;
|
|
35
|
-
let groupCount = 0;
|
|
36
|
-
let hasShortValueSeparator = false;
|
|
37
|
-
while (remainings.length > 0) {
|
|
38
|
-
const arg = remainings.shift();
|
|
39
|
-
if (arg == undefined) {
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
const nextArg = remainings[0];
|
|
43
|
-
if (groupCount > 0) {
|
|
44
|
-
groupCount--;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
index++;
|
|
48
|
-
}
|
|
49
|
-
// check if `arg` is an options terminator.
|
|
50
|
-
// guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
51
|
-
if (arg === TERMINATOR) {
|
|
52
|
-
tokens.push({
|
|
53
|
-
kind: 'option-terminator',
|
|
54
|
-
index
|
|
55
|
-
});
|
|
56
|
-
const mapped = remainings.map(arg => {
|
|
57
|
-
return { kind: 'positional', index: ++index, value: arg };
|
|
58
|
-
});
|
|
59
|
-
tokens.push(...mapped);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
if (isShortOption(arg)) {
|
|
63
|
-
const shortOption = arg.charAt(1);
|
|
64
|
-
let value;
|
|
65
|
-
let inlineValue;
|
|
66
|
-
if (groupCount) {
|
|
67
|
-
// e.g. `-abc`
|
|
68
|
-
tokens.push({
|
|
69
|
-
kind: 'option',
|
|
70
|
-
name: shortOption,
|
|
71
|
-
rawName: arg,
|
|
72
|
-
index,
|
|
73
|
-
value,
|
|
74
|
-
inlineValue
|
|
75
|
-
});
|
|
76
|
-
if (groupCount === 1 && hasOptionValue(nextArg)) {
|
|
77
|
-
value = remainings.shift();
|
|
78
|
-
if (hasShortValueSeparator) {
|
|
79
|
-
inlineValue = true;
|
|
80
|
-
hasShortValueSeparator = false;
|
|
81
|
-
}
|
|
82
|
-
tokens.push({
|
|
83
|
-
kind: 'option',
|
|
84
|
-
index,
|
|
85
|
-
value,
|
|
86
|
-
inlineValue
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
// e.g. `-a`
|
|
92
|
-
tokens.push({
|
|
93
|
-
kind: 'option',
|
|
94
|
-
name: shortOption,
|
|
95
|
-
rawName: arg,
|
|
96
|
-
index,
|
|
97
|
-
value,
|
|
98
|
-
inlineValue
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
// eslint-disable-next-line unicorn/no-null
|
|
102
|
-
if (value != null) {
|
|
103
|
-
++index;
|
|
104
|
-
}
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
if (isShortOptionGroup(arg)) {
|
|
108
|
-
// expend short option group (e.g. `-abc` => `-a -b -c`, `-f=bar` => `-f bar`)
|
|
109
|
-
const expanded = [];
|
|
110
|
-
let shortValue = '';
|
|
111
|
-
for (let i = 1; i < arg.length; i++) {
|
|
112
|
-
const shortableOption = arg.charAt(i);
|
|
113
|
-
if (hasShortValueSeparator) {
|
|
114
|
-
shortValue += shortableOption;
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
if (!allowCompatible && shortableOption.codePointAt(0) === EQUAL_CODE) {
|
|
118
|
-
hasShortValueSeparator = true;
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
expanded.push(`${SHORT_OPTION_PREFIX}${shortableOption}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (shortValue) {
|
|
126
|
-
expanded.push(shortValue);
|
|
127
|
-
}
|
|
128
|
-
remainings.unshift(...expanded);
|
|
129
|
-
groupCount = expanded.length;
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
if (isLongOption(arg)) {
|
|
133
|
-
// e.g. `--foo`
|
|
134
|
-
const longOption = arg.slice(2);
|
|
135
|
-
tokens.push({
|
|
136
|
-
kind: 'option',
|
|
137
|
-
name: longOption,
|
|
138
|
-
rawName: arg,
|
|
139
|
-
index,
|
|
140
|
-
value: undefined,
|
|
141
|
-
inlineValue: undefined
|
|
142
|
-
});
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
if (isLongOptionAndValue(arg)) {
|
|
146
|
-
// e.g. `--foo=bar`
|
|
147
|
-
const equalIndex = arg.indexOf(EQUAL_CHAR);
|
|
148
|
-
const longOption = arg.slice(2, equalIndex);
|
|
149
|
-
const value = arg.slice(equalIndex + 1);
|
|
150
|
-
tokens.push({
|
|
151
|
-
kind: 'option',
|
|
152
|
-
name: longOption,
|
|
153
|
-
rawName: `${LONG_OPTION_PREFIX}${longOption}`,
|
|
154
|
-
index,
|
|
155
|
-
value,
|
|
156
|
-
inlineValue: true
|
|
157
|
-
});
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
tokens.push({
|
|
161
|
-
kind: 'positional',
|
|
162
|
-
index,
|
|
163
|
-
value: arg
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
return tokens;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Check if `arg` is a short option (e.g. `-f`)
|
|
170
|
-
* @param arg the argument to check
|
|
171
|
-
* @returns whether `arg` is a short option
|
|
172
|
-
*/
|
|
173
|
-
function isShortOption(arg) {
|
|
174
|
-
return (arg.length === 2 && arg.codePointAt(0) === HYPHEN_CODE && arg.codePointAt(1) !== HYPHEN_CODE);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Check if `arg` is a short option group (e.g. `-abc`)
|
|
178
|
-
* @param arg the argument to check
|
|
179
|
-
* @returns whether `arg` is a short option group
|
|
180
|
-
*/
|
|
181
|
-
function isShortOptionGroup(arg) {
|
|
182
|
-
if (arg.length <= 2) {
|
|
183
|
-
return false;
|
|
184
|
-
}
|
|
185
|
-
if (arg.codePointAt(0) !== HYPHEN_CODE) {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
if (arg.codePointAt(1) === HYPHEN_CODE) {
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Check if `arg` is a long option (e.g. `--foo`)
|
|
195
|
-
* @param arg the argument to check
|
|
196
|
-
* @returns whether `arg` is a long option
|
|
197
|
-
*/
|
|
198
|
-
function isLongOption(arg) {
|
|
199
|
-
return hasLongOptionPrefix(arg) && !arg.includes(EQUAL_CHAR, 3);
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Check if `arg` is a long option with value (e.g. `--foo=bar`)
|
|
203
|
-
* @param arg the argument to check
|
|
204
|
-
* @returns whether `arg` is a long option
|
|
205
|
-
*/
|
|
206
|
-
function isLongOptionAndValue(arg) {
|
|
207
|
-
return hasLongOptionPrefix(arg) && arg.includes(EQUAL_CHAR, 3);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Check if `arg` is a long option prefix (e.g. `--`)
|
|
211
|
-
* @param arg the argument to check
|
|
212
|
-
* @returns whether `arg` is a long option prefix
|
|
213
|
-
*/
|
|
214
|
-
function hasLongOptionPrefix(arg) {
|
|
215
|
-
return arg.length > 2 && ~arg.indexOf(LONG_OPTION_PREFIX);
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Check if a `value` is an option value
|
|
219
|
-
* @param value a value to check
|
|
220
|
-
* @returns whether a `value` is an option value
|
|
221
|
-
*/
|
|
222
|
-
function hasOptionValue(value) {
|
|
223
|
-
// eslint-disable-next-line unicorn/no-null
|
|
224
|
-
return !(value == null) && value.codePointAt(0) !== HYPHEN_CODE;
|
|
225
|
-
}
|
|
1
|
+
export { parseArgs } from './parser.js';
|
|
2
|
+
export * from './resolver.js';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Argument token Kind
|
|
3
|
+
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
4
|
+
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
5
|
+
* - `positional`: positional token
|
|
6
|
+
*/
|
|
7
|
+
type ArgTokenKind = 'option' | 'option-terminator' | 'positional';
|
|
8
|
+
/**
|
|
9
|
+
* Argument token
|
|
10
|
+
*/
|
|
11
|
+
export interface ArgToken {
|
|
12
|
+
/**
|
|
13
|
+
* Argument token kind
|
|
14
|
+
*/
|
|
15
|
+
kind: ArgTokenKind;
|
|
16
|
+
/**
|
|
17
|
+
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1
|
|
18
|
+
*/
|
|
19
|
+
index: number;
|
|
20
|
+
/**
|
|
21
|
+
* Option name, e.g. `--foo` => `foo`, `-x` => `x`
|
|
22
|
+
*/
|
|
23
|
+
name?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`
|
|
26
|
+
*/
|
|
27
|
+
rawName?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
30
|
+
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
31
|
+
*/
|
|
32
|
+
value?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
35
|
+
*/
|
|
36
|
+
inlineValue?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Parse Options
|
|
40
|
+
*/
|
|
41
|
+
export interface ParseOptions {
|
|
42
|
+
/**
|
|
43
|
+
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode
|
|
44
|
+
* @default false
|
|
45
|
+
*/
|
|
46
|
+
allowCompatible?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Parse command line arguments
|
|
50
|
+
* @example
|
|
51
|
+
* ```js
|
|
52
|
+
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
53
|
+
* // import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
54
|
+
*
|
|
55
|
+
* const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
56
|
+
* // do something with using tokens
|
|
57
|
+
* // ...
|
|
58
|
+
* console.log('tokens:', tokens)
|
|
59
|
+
* ```
|
|
60
|
+
* @param args command line arguments
|
|
61
|
+
* @param options parse options
|
|
62
|
+
* @returns argument tokens
|
|
63
|
+
*/
|
|
64
|
+
export declare function parseArgs(args: string[], options?: ParseOptions): ArgToken[];
|
|
65
|
+
/**
|
|
66
|
+
* Check if `arg` is a short option (e.g. `-f`)
|
|
67
|
+
* @param arg the argument to check
|
|
68
|
+
* @returns whether `arg` is a short option
|
|
69
|
+
*/
|
|
70
|
+
export declare function isShortOption(arg: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Check if `arg` is a long option prefix (e.g. `--`)
|
|
73
|
+
* @param arg the argument to check
|
|
74
|
+
* @returns whether `arg` is a long option prefix
|
|
75
|
+
*/
|
|
76
|
+
export declare function hasLongOptionPrefix(arg: string): number | false;
|
|
77
|
+
export {};
|