Skip to content

Commit 8262358

Browse files
committed
init
0 parents  commit 8262358

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tabWidth": 4,
3+
"useTabs": true,
4+
"semi": true,
5+
"trailingComma": "none"
6+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"deno.enable": true
3+
}

src/main.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export function parseArgs(
2+
command: string,
3+
options: {
4+
positionalArgumentName?: string;
5+
prefix: string;
6+
}
7+
) {
8+
const initialRegex = new RegExp(
9+
`^${options.prefix}([\-_a-zA-Z0-9]{1,32})(?: (.+))?$`
10+
);
11+
const argRegex =
12+
/(?:--([a-zA-Z0-9\-_]+)(?:(?:=| )(?:((?!-)[^ \"'\n]+)|["'](.*?)["']))?|-([a-zA-Z0-9\-_])(?: (?:((?!-)[^ \"'\n]+)|["'](.*?)["']))?)/g;
13+
14+
const [_fullCmd, commandName, argsString] = command.match(initialRegex)!;
15+
16+
const args: {
17+
[name: string]: string | boolean;
18+
} = {};
19+
if (argsString) {
20+
const argsMatch = argsString.matchAll(argRegex);
21+
let workingArgsString = argsString;
22+
for (const arg of argsMatch) {
23+
workingArgsString = workingArgsString.replace(arg[0], "");
24+
const foundValues = [];
25+
for (const lookAt of [1, 2, 3, 4, 5, 6]) {
26+
if (arg[lookAt]) foundValues.push(arg[lookAt]);
27+
if (foundValues.length === 2) break;
28+
}
29+
30+
Object.assign(
31+
args,
32+
Object.fromEntries([
33+
[
34+
foundValues[0],
35+
foundValues.length === 1 ? true : foundValues[1]
36+
]
37+
])
38+
);
39+
}
40+
options.positionalArgumentName &&
41+
workingArgsString.trim() &&
42+
Object.assign(
43+
args,
44+
Object.fromEntries([
45+
[options.positionalArgumentName, workingArgsString.trim()]
46+
])
47+
);
48+
}
49+
50+
return { commandName, args };
51+
}

src/test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { parseArgs } from "./main.ts";
2+
3+
const testString = `!ban --fakeUser nin0`;
4+
5+
console.log(
6+
parseArgs(testString, {
7+
positionalArgumentName: "user",
8+
prefix: "!"
9+
})
10+
);

0 commit comments

Comments
 (0)