|
| 1 | +# args-tokenizer |
| 2 | + |
| 3 | +`args-tokenizer` is a lightweight JavaScript library for parsing shell commands with arguments into an `argv` array. This makes it easy to work with command-line tools and libraries that expect an array format for arguments, such as [`tinyexec`](https://github.com/tinyexec). |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Simple and intuitive API. |
| 8 | +- Handles quoted strings and escapes correctly. |
| 9 | +- Supports multiline input. |
| 10 | +- Ideal for parsing human-readable shell commands, especially `curl`-style commands. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Install `args-tokenizer`: |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install args-tokenizer |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Here's how you can use `args-tokenizer` to parse shell commands: |
| 23 | + |
| 24 | +```js |
| 25 | +import { tokenizeArgs } from "args-tokenizer"; |
| 26 | + |
| 27 | +const args = tokenizeArgs(`ls -la "./src"`); |
| 28 | +console.log(args); // ["ls", "-la", "./src"] |
| 29 | +``` |
| 30 | + |
| 31 | +### Multiline Input Support |
| 32 | + |
| 33 | +`args-tokenizer` also supports multiline commands, such as: |
| 34 | + |
| 35 | +```js |
| 36 | +const args = tokenizeArgs(` |
| 37 | + curl \\ |
| 38 | + -X POST \\ |
| 39 | + "https://my-url.com" |
| 40 | +`); |
| 41 | +console.log(args); // ["curl", "-X", "POST", "https://my-url.com"] |
| 42 | +``` |
| 43 | + |
| 44 | +### Example with `tinyexec` |
| 45 | + |
| 46 | +One common use case is passing more human-readable commands into the [`tinyexec`](https://github.com/tinyexec) library: |
| 47 | + |
| 48 | +```js |
| 49 | +import { tokenizeArgs } from "args-tokenizer"; |
| 50 | +import { x } from "tinyexec"; |
| 51 | + |
| 52 | +const [command, ...args] = tokenizeArgs("ls -la"); |
| 53 | +const result = await x(command, args); |
| 54 | +console.log(result.stdout); |
| 55 | +``` |
| 56 | + |
| 57 | +## API |
| 58 | + |
| 59 | +### `tokenizeArgs(command: string): string[]` |
| 60 | + |
| 61 | +Parses a shell command string into an array of arguments. Properly handles: |
| 62 | + |
| 63 | +- Quoted strings (e.g., `'"./path/to/file"'`). |
| 64 | +- Escaped characters (e.g., `\"`). |
| 65 | +- Multiline commands (e.g., lines ending with `\\`). |
| 66 | + |
| 67 | +## License |
| 68 | + |
| 69 | +This project is licensed under the [MIT License](./LICENSE). |
| 70 | + |
| 71 | +## Contributing |
| 72 | + |
| 73 | +Contributions are welcome! Feel free to open issues or submit pull requests to improve the library. |
0 commit comments