A straightforward CLI argument parser with no added complexity or opinions. Built for simplicity, performance, and TypeScript support.
- 🚀 Zero dependencies - Lightweight and fast
- 📝 TypeScript first - Built with TypeScript, includes full type definitions
- 🎯 Simple API - Minimal learning curve, just works
- 🔧 Flexible - Support for all argument types and patterns
- ⚡ Performance focused - Optimized for speed and memory usage
- 🛡️ Error handling - Clear, actionable error messages
npm install argcomyarn add argcompnpm add argcomimport { arg } from 'argcom';
const args = arg({
// Boolean flags
'--help': Boolean,
'--version': Boolean,
// String arguments
'--name': String,
'--host': String,
// Number arguments
'--port': Number,
// Array arguments (multiple values)
'--tag': [String],
// Count flags (count occurrences)
'--verbose': arg.COUNT,
// Aliases
'-h': '--help',
'-v': '--version',
'-n': '--name'
});
console.log(args);Parses command line arguments according to the provided specification.
spec(object): Argument specification objectoptions(object, optional): Parser options
The specification object maps argument names to their types:
{
'--flag': Boolean, // Boolean flag
'--string': String, // String argument
'--number': Number, // Number argument
'--array': [String], // Array of strings
'--count': arg.COUNT, // Count occurrences
'--alias': '--flag' // Alias to another argument
}{
argv?: string[]; // Custom argv (default: process.argv.slice(2))
permissive?: boolean; // Allow unknown arguments (default: false)
stopAtPositional?: boolean; // Stop parsing at first positional arg (default: false)
}Returns an object with parsed arguments and a special _ array containing positional arguments:
{
_: string[]; // Positional arguments
[key: string]: any; // Parsed argument values
}import { arg } from 'argcom';
// Command: node script.js --port 3000 --host localhost file1.txt file2.txt
const args = arg({
'--port': Number,
'--host': String
});
console.log(args);
// Output: { _: ['file1.txt', 'file2.txt'], '--port': 3000, '--host': 'localhost' }// Command: node script.js --verbose --debug
const args = arg({
'--verbose': Boolean,
'--debug': Boolean
});
console.log(args);
// Output: { _: [], '--verbose': true, '--debug': true }// Command: node script.js -v -p 8080
const args = arg({
'--verbose': Boolean,
'--port': Number,
'-v': '--verbose',
'-p': '--port'
});
console.log(args);
// Output: { _: [], '--verbose': true, '--port': 8080 }// Command: node script.js --tag dev --tag test --tag prod
const args = arg({
'--tag': [String]
});
console.log(args);
// Output: { _: [], '--tag': ['dev', 'test', 'prod'] }// Command: node script.js -vvv
const args = arg({
'--verbose': arg.COUNT,
'-v': '--verbose'
});
console.log(args);
// Output: { _: [], '--verbose': 3 }// Permissive mode - unknown arguments go to positional array
const args = arg({
'--known': Boolean
}, {
permissive: true,
argv: ['--known', '--unknown', 'value']
});
console.log(args);
// Output: { _: ['--unknown', 'value'], '--known': true }// Stop parsing at first positional argument
const args = arg({
'--verbose': Boolean,
'--port': Number
}, {
stopAtPositional: true,
argv: ['--verbose', 'file.txt', '--port', '3000']
});
console.log(args);
// Output: { _: ['file.txt', '--port', '3000'], '--verbose': true }argcom provides clear error messages for common mistakes:
import { arg, ArgcomError } from 'argcom';
try {
const args = arg({
'--port': Number
}, {
argv: ['--port'] // Missing value
});
} catch (error) {
if (error instanceof ArgcomError) {
console.log(error.message); // "option requires argument: --port"
console.log(error.code); // "ARG_MISSING_REQUIRED_LONGARG"
}
}ARG_CONFIG_NO_SPEC- No specification object providedARG_CONFIG_EMPTY_KEY- Empty string used as argument keyARG_CONFIG_NONOPT_KEY- Argument key doesn't start with-ARG_CONFIG_NONAME_KEY- Singular-used as keyARG_CONFIG_BAD_TYPE- Invalid type specificationARG_CONFIG_SHORTOPT_TOOLONG- Short option longer than one characterARG_UNKNOWN_OPTION- Unknown argument encounteredARG_MISSING_REQUIRED_LONGARG- Required argument value missingARG_MISSING_REQUIRED_SHORTARG- Short argument missing required value
Create custom argument processors using arg.flag():
const collect = arg.flag((value, name, previous = []) => {
return [...previous, value];
});
const args = arg({
'--collect': collect
});// Command: node script.js --item apple --item banana --item cherry
const args = arg({
'--item': [String]
});
console.log(args['--item']); // ['apple', 'banana', 'cherry']Use -- to separate options from positional arguments:
// Command: node script.js --verbose -- --not-an-option file.txt
const args = arg({
'--verbose': Boolean
});
console.log(args);
// Output: { _: ['--not-an-option', 'file.txt'], '--verbose': true }argcom is written in TypeScript and includes full type definitions:
import { arg, ArgHandler, ArgOptions, ArgConfig, ArgcomError } from 'argcom';
// Type-safe argument specification
const spec: ArgOptions = {
'--port': Number,
'--host': String
};
// Type-safe options
const options: ArgConfig = {
permissive: false,
stopAtPositional: false
};
const result = arg(spec, options);| Feature | argcom | minimist | yargs | commander |
|---|---|---|---|---|
| Bundle size | ~3KB | ~4KB | ~500KB | ~200KB |
| TypeScript | ✅ Native | ❌ | ✅ | ✅ |
| Zero deps | ✅ | ✅ | ❌ | ❌ |
| Error handling | ✅ Clear | ❌ Basic | ✅ | ✅ |
| Performance | ✅ Fast | ✅ Fast | ❌ Slow | ❌ Slow |
We welcome contributions! Please see our Contributing Guide for details.