declarative cli sanitization and execution for @magic
sanitizes cli flags from aliases to default names
rewrites process.argv accordingly
provides autogenerated --help output (that can be customized)
handles commands and environment.
ecmascript modules only. no commonjs support.
- @magic/log: console.log wrapper with loglevels
- @magic/types: type checking library
- @magic/types: case checking library (CamelCase, snake_case, kebab-case)
@magic/log, @magic/cases and @magic/types have no dependencies.
be in a node ecmascript module project.
npm i --save-dev --save-exact @magic/clithere are some quirks that need some careful consideration when designing a cli api depending on your requirements, these caveats should seldomly apply.
if your last argument does not have a corresponding flag, it will still be assigned to the last flag prior to it.
if one of your options gets an argument that is equal to a command, this command will be executed
cli arguments that start with a - will always be treated as flags, not values.
those issues might get addressed in the future.
first, define the cli file
// ./bin.jsimport{cli}from'@magic/cli'constres=cli({commands: [['cmd1','cmd1alias'],'cmd2'],options: [['--flag1','-f1'],['--flag2','-f2'],],default: {'--default-key': 'default-value',},required: ['--default-key'],single: ['--default-key'],env: [[['--production','--prod','--p','-p'],'NODE_ENV','production']],pure: true,// do neither change process.argv nor process.envpureArgv: true,// do not change process.argvpureEnv: true,// do not change process.env})console.log(res)argv mappings handle options and option aliases
using the cli file above
./bin.js -f1 arg1 arg2 -f2resulting process.argv:
process.argv=['/path/to/bin/node','/path/to/bin.js','cmd1','--flag1''arg1','arg2','--flag2',]logged javascript object
{argv: {'--flag1': ['arg1',arg2],'--flag2': []},args: {flag1: ['arg1','arg2'],flag2: []},// ... other fields}cli commands will be handled too.
// call./bin.jscmd1// results:{commands: {cmd1: true},// ... other fields}@magic/cli will parse your configuration and create a help text based on it.
// ./bin.jsimportclifrom'@magic/cli'constargs={commands: [['magic','m']],options: [['--spell','-s']],env: [[['dev','development'],'NODE_ENV','development']],help: 'custom help text',}constargv=cli(args)then run ./bin.js without arguments
./bin.js
// help output
`@magic/cli wrapped cli.custom help textcli commandsmagic - aliases: ["m"]possible command line flags:--spell - aliases: ["-s"]environment switches:dev: set NODE_ENV to development - aliases ["development"]`the help property will accept an object which maps to the args object
importclifrom'@magic/cli'constargs={commands: [['magic','m']],options: [['--spell','-s']],env: [[['dev','development'],'NODE_ENV','development']],prepend: 'prepend',append: 'append',help: {name: 'cli name',text: 'custom help text',commands: {magic: 'magic info help text',},options: {'--spell': 'cast a simple spell',},env: ['dev','set environment to development'],},}constargv=cli(args)// running./bin.js// without arguments// help output`cli namecustom help textcommands:magic - aliases: ["m"]flags:--spell - aliases: ["-s"]environment switches:dev: set process.NODE_ENV to development - aliases ["development"]`some cli arguments will be expected to return a string instead of a list of arguments.
this can be achieved using the single array
constargs={options: [['--single','-s']],single: ['--single'],}constres=cli(args)console.log(res)some cli arguments will be required.
this can be achieved using the required array.
if a required field is missing, a error message and the help will be shown.
constargs={options: [['--required','-r']],required: ['--required'],}constres=cli(args)console.log(res)there are some configuration parameters that can be passed to the cli function
constargs={pure: false,// set to true to prevent changes to process.argv and process.envpureEnv: false,// set to true to prevent changes to process.envpureArgv: false,// set to true to prevent changes to process.argv}cli(args)process.argv values can be prepended and appended
importclifrom'@magic/cli'constargs={prepend: ['prepended']append: ['appended']}cli(args)use this to set default process.argv key: value pairs that should be set if they are not
importclifrom'@magic/cli'constargs={options: [['--default-key'],],default: {'--default-key': 'default-value',},}constargv=cli(args)// returns{argv: {'--default-key': 'default-value',},}first release
cli's should now correctly process.exit(1) on error of the spawned process.
console help output now aligns nicely
node 12.4.0 does not have --experimental-node-modules fladg.
readd --experimental-node-modules flag for 13.1.0+
update dependencies bump node version
help is shown if cli has commands but none are given
update dependencies
update dependencies
- parsed.args added. is copy of argv, but using camelCased keys without leading --.
- no commonjs fallback, ecmascript modules all the way
- parsed does not return aliases. env, argv, args, commands. thats it.
add @magic/cases dependency
update deps
--help works for cli scripts without commands too
cli will always provide --help and -h flags to show help
- args can be set to be single now, making them return a .join(' ')ed string instead of an array
- args can be set to be required now, making the cli error and show the help if they are not.
required args can now be an array. this allows '--a' or '--b' to be required. errors if both are given.
- the command
cli-name allnow automagically sets all available commands to true. - command keys will always be set to a boolean, return false if task is supposed to not be active.
regression: calling cli that has commands without commands will show help again.
regression: make commands only have keys for active commands again
finally get rid of the command regressions
- add cli.prompt to get user input.
- do not error if args.options is empty
- exec and spawn now are separate functions corresponding to node builtins
- cli.prompt: change call signature, remove PasswordStream
- fix required node version
- update dependencies
- prompt now has a yesDefault option
- prompt will add y/N or Y/n to the prompt message if it is missing
- default help arguments are now output by default.
nicer output for prompt messages
bump required node version to 14.2.0
update dependencies
- update dependencies
- bump required node version to 14.15.4
- update dependencies
update dependencies
update dependencies
update dependencies
- parse now can get an opts object as third argument to overwrite child_process.exec options
- help.argToHelp now errors if the first argument is not an array, before errors only got triggered by falsy arg.
- exec now uses @magic/error for errors.
- exec does not trim() the result.
- findLongestString sorts by length and then alphabetically
- export execFile
- update dependencies
update dependencies
update dependencies
update dependencies
- update devdependencies
- parse.argv does not error if args do not have a length
update dependencies
update dependencies
update dependencies
update dependencies
- update dependencies
- add colors to default arg output
- help.example can be an array
- update dependencies
- cli.prompt - msg can be an array
- cli.exec allows stderrToStdout redirect config option
- update dependencies
- rename .mjs to js
- add more tests
- update dependencies
- types changed to allow correct props when calling /src/index.js cli function
- update dependencies
- remove empty test file
- types: add single to ParseProps
- update dependencies
- exec and execFile options are correctly typed
- actually publish the types from 0.0.51.
- export exec, execFile and spawn option types
- update types
- update dependencies
- update dependencies
- ...