Comparing version
import { camelToKebab, kebabToCamel, padString } from './utils.js'; | ||
export function parseArgs(config) { | ||
const { command, options, version } = config; | ||
const args = process.argv.slice(2); | ||
// Normalize args to support --option=value and -o=value | ||
const args = process.argv.slice(2).flatMap(arg => { | ||
if (/^--?\w[\w-]*=/.test(arg)) { | ||
const [flag, ...rest] = arg.split('='); | ||
return [flag, rest.join('=')]; | ||
} | ||
return arg; | ||
}); | ||
const result = {}; | ||
@@ -27,3 +34,3 @@ // Check for duplicate aliases | ||
// Validate: required positionals must come before optional ones | ||
const positionals = command.positional ?? []; | ||
const positionals = command.positionals ?? []; | ||
let foundOptional = false; | ||
@@ -164,3 +171,3 @@ for (const pos of positionals) { | ||
if (!option || !configKey) { | ||
throw new Error(`Unknown option: ${arg}`); | ||
throw new Error(`Unknown CLI option: ${arg}`); | ||
} | ||
@@ -221,3 +228,3 @@ switch (option.type) { | ||
// Build usage string for positionals | ||
const usagePositionals = (command.positional ?? []) | ||
const usagePositionals = (command.positionals ?? []) | ||
.map(p => { | ||
@@ -234,3 +241,3 @@ const variadic = p.variadic ? '..' : ''; | ||
console.log('\nPositionals:'); | ||
command.positional?.forEach(arg => { | ||
command.positionals?.forEach(arg => { | ||
console.log(` ${arg.name.padEnd(20)}${arg.description.slice(0, 65).padEnd(65)}[${arg.type || 'string'}]`); | ||
@@ -237,0 +244,0 @@ }); |
export interface ArgumentOptions { | ||
/** command option type */ | ||
/** option type */ | ||
type?: 'string' | 'boolean' | 'number' | 'array'; | ||
/** description of the command option */ | ||
/** description of the flag option */ | ||
description: string; | ||
@@ -28,12 +28,18 @@ /** defaults to undefined, provide shorter aliases as command options */ | ||
export interface CommandOptions { | ||
/** CLI command name, used in the help docs */ | ||
/** command name, used in the help docs */ | ||
name: string; | ||
/** command description */ | ||
description: string; | ||
positional?: PositionalArgument[]; | ||
/** list of positional arguments */ | ||
positionals?: PositionalArgument[]; | ||
} | ||
/** CLI options */ | ||
export interface Config { | ||
/** CLI definition */ | ||
command: CommandOptions; | ||
/** CLI list of flag options */ | ||
options: Record<string, ArgumentOptions>; | ||
/** CLI or package version */ | ||
version?: string; | ||
} | ||
//# sourceMappingURL=interfaces.d.ts.map |
{ | ||
"name": "cli-nano", | ||
"version": "0.3.10", | ||
"version": "1.0.0", | ||
"description": "Mini command-line tool similar to `yargs` or `parseArgs` from Node.js that accepts positional arguments, flags and options.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -17,7 +17,7 @@ [](https://opensource.org/licenses/MIT) | ||
- Supports Variadic args (1 or more positional args) | ||
- Automatically converts flags to camelCase | ||
- Automatically converts flags to camelCase to match config options | ||
- accepts both `--camelCase` and `--kebab-case` | ||
- Negates flags when using the `--no-` prefix | ||
- Outputs version, when defined, when using `--version` | ||
- Outputs description and supplied help text when using `--help` | ||
- Outputs version, when defined, by using `--version` | ||
- Outputs description and supplied help text by using `--help` | ||
- Supports defining `required` options | ||
@@ -43,3 +43,3 @@ - Supports `default` values | ||
description: 'Start a server with the given options', | ||
positional: [ | ||
positionals: [ | ||
{ | ||
@@ -145,1 +145,7 @@ name: 'input', | ||
See [examples/](examples/) for more usage patterns. | ||
## Used by | ||
`cli-nano` is currently used in these other projects of mine (feel free to edit this list): | ||
- [native-copyfiles](https://github.com/ghiscoding/native-copyfiles) |
@@ -8,3 +8,11 @@ import type { ArgumentOptions, Config } from './interfaces.js'; | ||
const { command, options, version } = config; | ||
const args = process.argv.slice(2); | ||
// Normalize args to support --option=value and -o=value | ||
const args = process.argv.slice(2).flatMap(arg => { | ||
if (/^--?\w[\w-]*=/.test(arg)) { | ||
const [flag, ...rest] = arg.split('='); | ||
return [flag, rest.join('=')]; | ||
} | ||
return arg; | ||
}); | ||
const result: Record<string, any> = {}; | ||
@@ -35,3 +43,3 @@ | ||
// Validate: required positionals must come before optional ones | ||
const positionals = command.positional ?? []; | ||
const positionals = command.positionals ?? []; | ||
let foundOptional = false; | ||
@@ -174,3 +182,3 @@ for (const pos of positionals) { | ||
if (!option || !configKey) { | ||
throw new Error(`Unknown option: ${arg}`); | ||
throw new Error(`Unknown CLI option: ${arg}`); | ||
} | ||
@@ -234,3 +242,3 @@ | ||
// Build usage string for positionals | ||
const usagePositionals = (command.positional ?? []) | ||
const usagePositionals = (command.positionals ?? []) | ||
.map(p => { | ||
@@ -247,3 +255,3 @@ const variadic = p.variadic ? '..' : ''; | ||
console.log('\nPositionals:'); | ||
command.positional?.forEach(arg => { | ||
command.positionals?.forEach(arg => { | ||
console.log(` ${arg.name.padEnd(20)}${arg.description.slice(0, 65).padEnd(65)}[${arg.type || 'string'}]`); | ||
@@ -250,0 +258,0 @@ }); |
export interface ArgumentOptions { | ||
/** command option type */ | ||
/** option type */ | ||
type?: 'string' | 'boolean' | 'number' | 'array'; | ||
/** description of the command option */ | ||
/** description of the flag option */ | ||
description: string; | ||
/** defaults to undefined, provide shorter aliases as command options */ | ||
alias?: string | string[]; | ||
/** default value for the option if not provided */ | ||
default?: any; | ||
/** defaults to false, is the option required? */ | ||
@@ -17,10 +21,15 @@ required?: boolean; | ||
name: string; | ||
/** positional argument description */ | ||
description: string; | ||
/** postional argument type */ | ||
type?: 'string' | 'boolean' | 'number' | 'array'; | ||
/** defaults to false, allows multiple values for this positional argument */ | ||
variadic?: boolean; | ||
/** default value for the option if not provided */ | ||
default?: any; | ||
/** defaults to false, is the positional argument required? */ | ||
@@ -31,12 +40,22 @@ required?: boolean; | ||
export interface CommandOptions { | ||
/** CLI command name, used in the help docs */ | ||
/** command name, used in the help docs */ | ||
name: string; | ||
/** command description */ | ||
description: string; | ||
positional?: PositionalArgument[]; | ||
/** list of positional arguments */ | ||
positionals?: PositionalArgument[]; | ||
} | ||
/** CLI options */ | ||
export interface Config { | ||
/** CLI definition */ | ||
command: CommandOptions; | ||
/** CLI list of flag options */ | ||
options: Record<string, ArgumentOptions>; | ||
/** CLI or package version */ | ||
version?: string; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46739
3.03%629
4.31%0
-100%148
3.5%