0.2.0 • Published 5 months ago
@calmcli/opt v0.2.0
calmcli/opt (node)
A POSIX-compatible getopt implementation for node.
Key notes
- Expects all options(flags and parameters) before arguments(operands)
- Stops processing options on first non-option argument.
- Supports
-operands
- Supports option bundling;
-abc
is equivalent to-a -b -c
- Supports operand bundling;
-show-onlycsv
is equivalent to-showonly csv
- Rudimentary command support.
- No long
--option
support.
Heavily inspired by Hare::getopt
Status
Currently a Work in Progress (WIP). Please do not use it for production/critical purposes.
Example Usage
#!/usr/bin/env node
import { parse, printUsage, println, eprintln, type Spec } from '@calmcli/opt';
const spec: Spec[] = [
'Concatenate FILE(s) to standard output.',
'With no FILE, or when FILE is -, read standard input.',
{ ch: 'a', help: 'equivalent to -vET' },
{ label: 'Display', summary: 'display options' },
{ ch: 'E', help: 'display $ at end of each line' },
{ ch: 'T', help: 'display TAB characters as ^I' },
{ ch: 'v', help: 'use ^ and M- notation, except for LFD and TAB' },
{
ch: 'd',
help: 'read and concat all files from directory',
kind: 'directory',
rules: [],
},
'GNU coreutils online help: <https://www.gnu.org/software/coreutils/>',
'FILES',
];
const main = async function() {
const app = parse(process.argv.slice(1), spec);
if (app.args.length === 0) {
eprintln(`${app.name}: FILES: file arguments expected\n`);
printUsage(app.name, spec);
process.exit(1);
};
for (const opt of app.opts) {
switch(opt.ch) {
case 'a':
println('using option v E and T');
break;
case 'E':
println('using option E');
break;
case 'T':
println('using option T');
break;
case 'v':
println('using option v');
break;
default:
eprintln(`${app.name}: -${opt.ch}: unknown option`);
printUsage(app.name, spec);
process.exit(1);
}
}
println('Files:');
for (const arg of app.args) {
println(` ${arg}`);
}
}
void main();