Skip to content

Commit 06e1af0

Browse files
committed
feat: add spread arguments
1 parent d8c17fb commit 06e1af0

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type CommandArg = {
1515
isOptional?: boolean;
1616
defaultValue?: string;
1717
alternativeValue?: string;
18+
isSpread?: boolean;
1819
};
1920

2021
export const USER_DIRECTORY = os.homedir();
@@ -111,13 +112,14 @@ export function formatCommand(
111112

112113
let allowRequired = true;
113114

114-
const commandArguments: CommandArg[] = commandArgs.map((arg) => {
115+
const commandArguments: CommandArg[] = commandArgs.map((arg, index) => {
115116
const object: CommandArg = { name: '' };
116117

117118
const hasDefaultValue = arg.includes('??');
118119
const hasElse = !hasDefaultValue && arg.includes(':');
119120
const isTernary = !hasDefaultValue && hasElse && arg.includes('?');
120121
const isOptional = !hasDefaultValue && !hasElse && arg.includes('?');
122+
const isSpread = arg.includes('...');
121123

122124
// TODO - make linting function
123125

@@ -141,6 +143,16 @@ export function formatCommand(
141143
object.name = name;
142144
object.isOptional = true;
143145

146+
allowRequired = false;
147+
} else if (isSpread) {
148+
if (index !== commandArgs.length - 1)
149+
error('You can only use a spread argument at the last position');
150+
151+
const name = arg.replace('...', '').trim();
152+
object.name = name;
153+
object.isOptional = true;
154+
object.isSpread = true;
155+
144156
allowRequired = false;
145157
} else {
146158
if (!allowRequired)
@@ -164,7 +176,10 @@ export function formatCommand(
164176

165177
for (let index = 0; index < commandArguments.length; index++) {
166178
const arg = commandArguments[index];
167-
let argValue = arg.alternativeValue
179+
180+
let argValue = arg.isSpread
181+
? args.slice(index).join(' ')
182+
: arg.alternativeValue
168183
? args[index]
169184
? arg.defaultValue
170185
: arg.alternativeValue

0 commit comments

Comments
 (0)