Skip to content

Commit b3d3b64

Browse files
committed
Throw error by default
1 parent b473675 commit b3d3b64

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,30 @@ console.log(result.stdout);
6262

6363
## API
6464

65-
### `tokenizeArgs(command: string): string[]`
65+
### `tokenizeArgs(command: string, options: Options): string[]`
6666

6767
Parses a shell command string into an array of arguments. Properly handles:
6868

6969
- Quoted strings (e.g., `'"./path/to/file"'`).
7070
- Escaped characters (e.g., `\"`).
7171
- Multiline commands (e.g., lines ending with `\\`).
7272

73+
### Options
74+
75+
- `loose`: If `true`, the tokenizer will not throw an error when closing quotes are missing. Default is `false`.
76+
77+
#### Examples
78+
79+
```js
80+
// Without loose option (default behavior)
81+
// This will throw an error due to the missing closing quote
82+
tokenizeArgs('command "arg1 arg2');
83+
84+
// With loose option enabled
85+
const args = tokenizeArgs('command "arg1 arg2', { loose: true });
86+
// ['command', 'arg1 arg2']
87+
```
88+
7389
## License
7490

7591
This project is licensed under the [MIT License](./LICENSE).

src/args-tokenizer.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ test("inconsistently quoted arguments", () => {
2121
expect(tokenizeArgs(`command "arg"um"en"t`)).toEqual(["command", "argument"]);
2222
});
2323

24-
test("forgive incomplete quotes", () => {
25-
expect(tokenizeArgs(`command "arg`)).toEqual(["command", "arg"]);
26-
});
27-
28-
test("detects incomplete quotes in strict mode", () => {
24+
test("detects incomplete quotes ", () => {
2925
expect(() => {
30-
tokenizeArgs(`command "arg`, { strict: true });
26+
tokenizeArgs(`command "arg1 "arg2" "arg3"`);
3127
}).toThrow("Unexpected end of string. Closing quote is missing.");
3228
});
3329

30+
test("forgive incomplete quotes in loose mode", () => {
31+
expect(tokenizeArgs(`command "arg1 "arg2" "arg3"`, { loose: true })).toEqual([
32+
"command",
33+
"arg1 arg2 arg3",
34+
]);
35+
});
36+
3437
test("escape quotes and spaces with other quotes", () => {
3538
expect(tokenizeArgs(`command 'quote "' "quote '"`)).toEqual([
3639
"command",
@@ -47,7 +50,7 @@ test("escape quotes with backslashes", () => {
4750
});
4851

4952
test("escape spaces with backslashes", () => {
50-
expect(tokenizeArgs(`command space\\ "`)).toEqual(["command", "space "]);
53+
expect(tokenizeArgs(`command space\\ `)).toEqual(["command", "space "]);
5154
});
5255

5356
test("ignore escaped newlines outside of quotes", () => {

src/args-tokenizer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const spaceRegex = /\s/;
22

33
type Options = {
4-
strict?: boolean;
4+
loose?: boolean;
55
};
66

77
/**
@@ -57,7 +57,10 @@ export const tokenizeArgs = (
5757
if (currentToken.length > 0) {
5858
tokens.push(currentToken);
5959
}
60-
if (options?.strict && openningQuote) {
60+
if (options?.loose) {
61+
return tokens;
62+
}
63+
if (openningQuote) {
6164
throw Error("Unexpected end of string. Closing quote is missing.");
6265
}
6366
return tokens;

0 commit comments

Comments
 (0)