cmdparse
v1.0.5
Published
A lightweight CLI-style command parser that converts raw input strings into structured command, arguments, and flags.
Maintainers
Readme
cmdparse
A lightweight CLI-style command parser that converts raw input strings into structured { command, args, flags }.
Supports:
- Positional arguments
- Short flags (
-p 80) - Long flags (
--port=80,--port 80) - Boolean flag clusters (
-abc) - Quoted strings (
"hello world") - Automatic type casting (
"80" → 80,"true" → true)
📦 Installation
npm install cmdparse🚀 Usage
import { parseInput } from 'cmdparse';
const result = parseInput("ssh 10.0.0.1 -u admin -p secret");
console.log(result);Output
{
command: "ssh",
args: ["10.0.0.1"],
flags: {
u: "admin",
p: "secret"
}
}🧠 Examples
Basic command
parseInput("ls /etc");{
command: "ls",
args: ["/etc"],
flags: {}
}With short flags
parseInput("nmap 192.168.1.1 -p 80");{
command: "nmap",
args: ["192.168.1.1"],
flags: { p: 80 }
}With long flags
parseInput("run --env=prod --debug true");{
command: "run",
args: [],
flags: {
env: "prod",
debug: true
}
}Boolean flag cluster
parseInput("cmd -abc");{
command: "cmd",
args: [],
flags: {
a: true,
b: true,
c: true
}
}Quoted strings
parseInput("echo \"hello world\"");{
command: "echo",
args: ["hello world"],
flags: {}
}🔧 API
parseInput(raw: string): ParsedCommand | null
Parses a raw CLI-like string into structured data.
Returns null if input is empt
