{"name":"sade","version":"1.2.0","description":"Smooth (CLI) operator 🎶","repository":{"type":"git","url":"git+https://github.com/lukeed/sade.git"},"main":"lib/index.js","license":"MIT","files":["lib"],"author":{"name":"Luke Edwards","email":"luke.edwards05@gmail.com","url":"lukeed.com"},"scripts":{"test":"tape test/*.js | tap-spec"},"dependencies":{"mri":"^1.1.0","pad-right":"^0.2.2"},"engines":{"node":">= 6"},"keywords":["cli","cli-app","commander","arguments","parser","yargs","argv"],"devDependencies":{"tap-spec":"^4.1.1","tape":"^4.8.0"},"gitHead":"36caa74f7b0360810265886939accd1f9f226a3c","bugs":{"url":"https://github.com/lukeed/sade/issues"},"homepage":"https://github.com/lukeed/sade#readme","_id":"sade@1.2.0","_shasum":"6712d874a2a305ebe6c741541328e47f8a0d87ef","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.11.1","_npmUser":{"name":"lukeed","email":"luke@lukeed.com"},"dist":{"shasum":"6712d874a2a305ebe6c741541328e47f8a0d87ef","tarball":"http://registry-npm.ywwl.com/sade/download/sade-1.2.0.tgz","integrity":"sha512-khGnRZbh55kRru4vfTE7v+52JMjfjX+QxE4SQZQIW7chnE8jfHNqYuwnFZzlRozfe74HDMiO5bw8ITwm0EwLMg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDkzvB9hICvNFWg3YbvN+zvYYo9jVHnzrJKHGqDsD0yPwIhANr8nM9BnZoagTYbv2iAnZwoEzkWmGSfLuAU7HNk2k3t"}],"size":7102,"noattachment":false,"key":"/sade/-/sade-1.2.0.tgz"},"maintainers":[{"name":"lukeed","email":"luke@lukeed.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sade-1.2.0.tgz_1514077877770_0.3635411881841719"},"directories":{},"readme":"# sade [![Build Status](https://travis-ci.org/lukeed/sade.svg?branch=master)](https://travis-ci.org/lukeed/sade)\n\n> Smooth (CLI) Operator 🎶\n\nSade is a small but powerful tool for building command-line interface (CLI) applications for Node.js that are fast, responsive, and helpful!\n\nIt enables default commands, git-like subcommands, option flags with aliases, default option values with type-casting, required-vs-optional argument handling, command validation, and automated help text generation!\n\nYour app's UX will be as smooth as butter... just like [Sade's voice](https://www.youtube.com/watch?v=4TYv2PhG89A). 😉\n\n\n## Install\n\n```\n$ npm install --save sade\n```\n\n\n## Usage\n\n***Input:***\n\n```js\n#!/usr/bin/env node\n\nconst sade = require('sade');\n\nconst prog = sade('my-cli');\n\nprog\n  .version('1.0.5')\n  .option('--global, -g', 'An example global flag')\n  .option('-c, --config', 'Provide path to custom config', 'foo.config.js');\n\nprog\n  .command('build <src> <dest>')\n  .describe('Build the source directory. Expects an `index.js` entry file.')\n  .option('-o, --output', 'Change the name of the output file', 'bundle.js')\n  .example('build src build --global --config my-conf.js')\n  .example('build app public -o main.js')\n  .action((src, dest, opts) => {\n    console.log(`> building from ${src} to ${dest}`);\n    console.log('> these are extra opts', opts);\n  });\n\nprog.parse(process.argv);\n```\n\n***Output:***\n\n```a\n$ my-cli --help\n\n  Usage\n    $ my-cli <command> [options]\n\n  Available Commands\n    build    Build the source directory.\n\n  For more info, run any command with the `--help` flag\n    $ my-cli build --help\n\n  Options\n    -v, --version    Displays current version\n    -g, --global     An example global flag\n    -c, --config     Provide path to custom config  (default foo.config.js)\n    -h, --help       Displays this message\n\n\n$ my-cli build --help\n\n  Description\n    Build the source directory.\n    Expects an `index.js` entry file.\n\n  Usage\n    $ my-cli build <src> [options]\n\n  Options\n    -o, --output    Change the name of the output file  (default bundle.js)\n    -g, --global    An example global flag\n    -c, --config    Provide path to custom config  (default foo.config.js)\n    -h, --help      Displays this message\n\n  Examples\n    $ my-cli build src build --global --config my-conf.js\n    $ my-cli build app public -o main.js\n```\n\n## Tips\n\n- **Define your global/program-wide version, options, description, and/or examples first.**<br>\n  _Once you define a Command, you can't access the global-scope again._\n\n- **Define all commands & options in the order that you want them to appear.**<br>\n  _Sade will not mutate or sort your CLI for you. Global options print before local options._\n\n- **Required arguments without values will error & exit**<br>\n  _An `Insufficient arguments!` error will be displayed along with a help prompt._\n\n- **Don't worry about manually displaying help~!**<br>\n  _Your help text is displayed automatically... including command-specific help text!_\n\n- **Automatic default/basic patterns**<br>\n  _Usage text will always append `[options]` & `--help` and `--version` are done for you._\n\n- **Only define what you want to display!**<br>\n  _Help text sections (example, options, etc) will only display if you provide values._\n\n\n## Subcommands\n\nSubcommands are defined & parsed like any other command! When defining their [`usage`](#usage-1), everything up until the first argument (`[foo]` or `<foo>`) is interpreted as the command string.\n\nThey should be defined in the order that you want them to appear in your general `--help` output.\n\nLastly, it is _not_ necessary to define the subcommand's \"base\" as an additional command. However, if you choose to do so, it's recommended that you define it first for better visibility.\n\n```js\nconst prog = sade('git');\n\n// Not necessary for subcommands to work, but it's here anyway!\nprog\n  .command('remote')\n  .describe('Manage set of tracked repositories')\n  .action(opts => {\n    console.log('~> Print current remotes...');\n  });\n\nprog\n  .command('remote add <name> <url>', 'Demo...')\n  .action((name, url, opts) => {\n    console.log(`~> Adding a new remote (${name}) to ${url}`);\n  });\n\nprog\n  .command('remote rename <old> <new>', 'Demo...')\n  .action((old, nxt, opts) => {\n    console.log(`~> Renaming from ${old} to ${nxt}~!`);\n  });\n```\n\n\n## Single Command Mode\n\nIn certain circumstances, you may only need `sade` for a single-command CLI application.\n\n> **Note:** Until `v1.6.0`, this made for an awkward pairing.\n\nTo enable this, you may make use of the [`isSingle`](#issingle) argument. Doing so allows you to pass the program's entire [`usage` text](#usage-1) into the `name` argument.\n\nWith \"Single Command Mode\" enabled, your entire binary operates as one command. This means that any [`prog.command`](#progcommandusage-desc-opts) calls are disallowed & will instead throw an Error. Of course, you may still define a program version, a description, an example or two, and declare options. You are customizing the program's attributes as a whole.<sup>*</sup>\n\n> <sup>*</sup> This is true for multi-command applications, too, up until your first `prog.command()` call!\n\n***Example***\n\nLet's reconstruct [`sirv-cli`](https://github.com/lukeed/sirv), which is a single-command application that (optionally) accepts a directory from which to serve files. It also offers a slew of option flags:\n\n```js\nsade('sirv [dir]', true)\n  .version('1.0.0')\n  .describe('Run a static file server')\n  .example('public -qeim 31536000')\n  .example('--port 8080 --etag')\n  .example('my-app --dev')\n  .option('-D, --dev', 'Enable \"dev\" mode')\n  .option('-e, --etag', 'Enable \"Etag\" header')\n  // There are a lot...\n  .option('-H, --host', 'Hostname to bind', 'localhost')\n  .option('-p, --port', 'Port to bind', 5000)\n  .action((dir, opts) => {\n    // Program handler\n  })\n  .parse(process.argv);\n```\n\nWhen `sirv --help` is run, the generated help text is trimmed, fully aware that there's only one command in this program:\n\n```\n  Description\n    Run a static file server\n\n  Usage\n    $ sirv [dir] [options]\n\n  Options\n    -D, --dev        Enable \"dev\" mode\n    -e, --etag       Enable \"Etag\" header\n    -H, --host       Hostname to bind  (default localhost)\n    -p, --port       Port to bind  (default 5000)\n    -v, --version    Displays current version\n    -h, --help       Displays this message\n\n  Examples\n    $ sirv public -qeim 31536000\n    $ sirv --port 8080 --etag\n    $ sirv my-app --dev\n```\n\n## Command Aliases\n\nCommand aliases are alternative names (aliases) for a command. They are often used as shortcuts or as typo relief!\n\nThe aliased names do not appear in the general help text.<br>\nInstead, they only appear within the Command-specific help text under an \"Aliases\" section.\n\n***Limitations***\n\n* You cannot assign aliases while in [Single Command Mode](#single-command-mode)\n* You cannot call [`prog.alias()`](#progaliasnames) before defining any Commands (via `prog.commmand()`)\n* You, the developer, must keep track of which aliases have already been used and/or exist as Command names\n\n***Example***\n\nLet's reconstruct the `npm install` command as a Sade program:\n\n```js\nsade('npm')\n  // ...\n  .command('install [package]', 'Install a package', {\n    alias: ['i', 'add', 'isntall']\n  })\n  .option('-P, --save-prod', 'Package will appear in your dependencies.')\n  .option('-D, --save-dev', 'Package will appear in your devDependencies.')\n  .option('-O, --save-optional', 'Package will appear in your optionalDependencies')\n  .option('-E, --save-exact', 'Save exact versions instead of using a semver range operator')\n  // ...\n```\n\nWhen we run `npm --help` we'll see this general help text:\n\n```\n  Usage\n    $ npm <command> [options]\n\n  Available Commands\n    install    Install a package\n\n  For more info, run any command with the `--help` flag\n    $ npm install --help\n\n  Options\n    -v, --version    Displays current version\n    -h, --help       Displays this message\n```\n\nWhen we run `npm install --help` &mdash; ***or*** the help flag with any of `install`'s aliases &mdash; we'll see this command-specific help text:\n\n```\n  Description\n    Install a package\n\n  Usage\n    $ npm install [package] [options]\n\n  Aliases\n    $ npm i\n    $ npm add\n    $ npm isntall\n\n  Options\n    -P, --save-prod        Package will appear in your dependencies.\n    -D, --save-dev         Package will appear in your devDependencies.\n    -O, --save-optional    Package will appear in your optionalDependencies\n    -E, --save-exact       Save exact versions instead of using a semver range operator\n    -h, --help             Displays this message\n```\n\n\n\n## API\n\n### sade(name, isSingle)\nReturns: `Program`\n\nReturns your chainable Sade instance, aka your `Program`.\n\n#### name\nType: `String`<br>\nRequired: `true`\n\nThe name of your `Program` / binary application.\n\n#### isSingle\nType: `Boolean`<br>\nDefault: `name.includes(' ');`\n\nIf your `Program` is meant to have ***only one command***.<br>\nWhen `true`, this simplifies your generated `--help` output such that:\n\n* the \"root-level help\" is your _only_ help text\n* the \"root-level help\" does not display an `Available Commands` section\n* the \"root-level help\" does not inject `$ name <command>` into the `Usage` section\n* the \"root-level help\" does not display `For more info, run any command with the `--help` flag` text\n\nYou may customize the `Usage` of your command by modifying the `name` argument directly.<br>\nPlease read [Single Command Mode](#single-command-mode) for an example and more information.\n\n> **Important:** Whenever `name` includes a custom usage, then `isSingle` is automatically assumed and enforced!\n\n### prog.command(usage, desc, opts)\n\nCreate a new Command for your Program. This changes the current state of your Program.\n\nAll configuration methods (`prog.describe`, `prog.action`, etc) will apply to this Command until another Command has been created!\n\n#### usage\n\nType: `String`\n\nThe usage pattern for your current Command. This will be included in the general or command-specific `--help` output.\n\n_Required_ arguments are wrapped with `<` and `>` characters; for example, `<foo>` and `<bar>`.\n\n_Optional_ arguments are wrapped with `[` and `]` characters; for example, `[foo]` and `[bar]`.\n\nAll arguments are ***positionally important***, which means they are passed to your current Command's [`handler`](#handler) function in the order that they were defined.\n\nWhen optional arguments are defined but don't receive a value, their positionally-equivalent function parameter will be `undefined`.\n\n> **Important:** You **must** define & expect required arguments _before_ optional arguments!\n\n```js\nsade('foo')\n\n  .command('greet <adjective> <noun>')\n  .action((adjective, noun, opts) => {\n    console.log(`Hello, ${adjective} ${noun}!`);\n  })\n\n  .command('drive <vehicle> [color] [speed]')\n  .action((vehicle, color, speed, opts) => {\n    let arr = ['Driving my'];\n    arr.push(color ? `${color} ${vehicle}` : vehicle);\n    speed && arr.push(`at ${speed}`);\n    opts.yolo && arr.push('...YOLO!!');\n    let str = arr.join(' ');\n    console.log(str);\n  });\n```\n\n```sh\n$ foo greet beautiful person\n# //=> Hello, beautiful person!\n\n$ foo drive car\n# //=> Driving my car\n\n$ foo drive car red\n# //=> Driving my red card\n\n$ foo drive car blue 100mph --yolo\n# //=> Driving my blue car at 100mph ...YOLO!!\n```\n\n\n#### desc\n\nType: `String`<br>\nDefault: `''`\n\nThe Command's description. The value is passed directly to [`prog.describe`](#progdescribetext).\n\n#### opts\n\nType: `Object`<br>\nDefault: `{}`\n\n##### opts.alias\nType: `String|Array`\n\nOptionally define one or more aliases for the current Command.<br>\nWhen declared, the `opts.alias` value is passed _directly_ to the [`prog.alias`](#progaliasnames) method.\n\n```js\n// Program A is equivalent to Program B\n// ---\n\nconst A = sade('bin')\n  .command('build', 'My build command', { alias: 'b' })\n  .command('watch', 'My watch command', { alias: ['w', 'dev'] });\n\nconst B = sade('bin')\n  .command('build', 'My build command').alias('b')\n  .command('watch', 'My watch command').alias('w', 'dev');\n```\n\n\n##### opts.default\n\nType: `Boolean`\n\nManually set/force the current Command to be the Program's default command. This ensures that the current Command will run if no command was specified.\n\n> **Important:** If you run your Program without a Command _and_ without specifying a default command, your Program will exit with a `No command specified` error.\n\n```js\nconst prog = sade('greet');\n\nprog.command('hello');\n//=> only runs if :: `$ greet hello`\n\n// $ greet\n//=> error: No command specified.\n\nprog.command('howdy', '', { default:true });\n//=> runs as `$ greet` OR `$ greet howdy`\n\n// $ greet\n//=> runs 'howdy' handler\n\n// $ greet foobar\n//=> error: Invalid command\n```\n\n\n### prog.describe(text)\n\nAdd a description to the current Command.\n\n#### text\n\nType: `String|Array`\n\nThe description text for the current Command. This will be included in the general or command-specific `--help` output.\n\nInternally, your description will be separated into an `Array` of sentences.\n\nFor general `--help` output, ***only*** the first sentence will be displayed. However, **all sentences** will be printed for command-specific `--help` text.\n\n> **Note:** Pass an `Array` if you don't want internal assumptions. However, the first item is _always_ displayed in general help, so it's recommended to keep it short.\n\n\n### prog.alias(...names)\n\nDefine one or more aliases for the current Command.\n\n> **Important:** An error will be thrown if:<br>1) the program is in [Single Command Mode](#single-command-mode); or<br>2) `prog.alias` is called before any `prog.command`.\n\n#### names\n\nType: `String`\n\nThe list of alternative names (aliases) for the current Command.<br>\nFor example, you may want to define shortcuts and/or common typos for the Command's full name.\n\n> **Important:** Sade _does not_ check if the incoming `names` are already in use by other Commands or their aliases.<br>During conflicts, the Command with the same `name` is given priority, otherwise the first Command (according to Program order) with `name` as an alias is chosen.\n\nThe `prog.alias()` is append-only, so calling it multiple times within a Command context will _keep_ all aliases, including those initially passed via [`opts.alias`](#optsdefault).\n\n```js\nsade('bin')\n  .command('hello <name>', 'Greet someone by their name', {\n    alias: ['hey', 'yo']\n  })\n  .alias('hi', 'howdy')\n  .alias('hola', 'oi');\n//=> hello aliases: hey, yo, hi, howdy, hola, oi\n```\n\n\n### prog.action(handler)\n\nAttach a callback to the current Command.\n\n#### handler\n\nType: `Function`\n\nThe function to run when the current Command is executed.\n\nIts parameters are based (positionally) on your Command's [`usage`](#usage-1) definition.\n\nAll options, flags, and extra/unknown values are included as the last parameter.\n\n> **Note:** Optional arguments are also passed as parameters & may be `undefined`!\n\n```js\nsade('foo')\n  .command('cp <src> <dest>')\n  .option('-f, --force', 'Overwrite without confirmation')\n  .option('-c, --clone-dir', 'Copy files to additional directory')\n  .option('-v, --verbose', 'Enable verbose output')\n  .action((src, dest, opts) => {\n    console.log(`Copying files from ${src} --> ${dest}`);\n    opts.c && console.log(`ALSO copying files from ${src} --> ${opts['clone-dir']}`);\n    console.log('My options:', opts);\n  })\n\n// $ foo cp original my-copy -v\n//=> Copying files from original --> my-copy\n//=> My options: { _:[], v:true, verbose:true }\n\n// $ foo cp original my-copy --clone-dir my-backup\n//=> Copying files from original --> my-copy\n//=> ALSO copying files from original --> my-backup\n//=> My options: { _:[], c:'my-backup', 'clone-dir':'my-backup' }\n```\n\n\n### prog.example(str)\n\nAdd an example for the current Command.\n\n#### str\n\nType: `String`\n\nThe example string to add. This will be included in the general or command-specific `--help` output.\n\n> **Note:** Your example's `str` will be prefixed with your Program's [`name`](#sadename).\n\n\n### prog.option(flags, desc, value)\n\nAdd an Option to the current Command.\n\n#### flags\n\nType: `String`\n\nThe Option's flags, which may optionally include an alias.\n\nYou may use a comma (`,`) or a space (` `) to separate the flags.\n\n> **Note:** The short & long flags can be declared in any order. However, the alias will always be displayed first.\n\n> **Important:** If using hyphenated flag names, they will be accessible **as declared** within your [`action()`](#progactionhandler) handler!\n\n```js\nprog.option('--global'); // no alias\nprog.option('-g, --global'); // alias first, comma\nprog.option('--global -g'); // alias last, space\n// etc...\n```\n\n#### desc\n\nType: `String`\n\nThe description for the Option.\n\n#### value\n\nType: `String`\n\nThe **default** value for the Option.\n\nFlags and aliases, if parsed, are `true` by default. See [`mri`](https://github.com/lukeed/mri#minimist) for more info.\n\n> **Note:** You probably only want to define a default `value` if you're expecting a `String` or `Number` value type.\n\nIf you _do_ pass a `String` or `Number` value type, your flag value will be casted to the same type. See [`mri#options.default`](https://github.com/lukeed/mri#optionsdefault) for info~!\n\n\n### prog.version(str)\n\nThe `--version` and `-v` flags will automatically output the Program version.\n\n#### str\n\nType: `String`<br>\nDefault: `0.0.0`\n\nThe new version number for your Program.\n\n> **Note:** Your Program `version` is `0.0.0` until you change it.\n\n### prog.parse(arr, opts)\n\nParse a set of CLI arguments.\n\n#### arr\n\nType: `Array`\n\nYour Program's `process.argv` input.\n\n> **Important:** Do not `.slice(2)`! Doing so will break parsing~!\n\n#### opts\n\nType: `Object`<br>\nDefault: `{}`\n\nAdditional `process.argv` parsing config. See [`mri`'s options](https://github.com/lukeed/mri#mriargs-options) for details.\n\n> **Important:** These values _override_ any internal values!\n\n```js\nprog\n  .command('hello')\n  .option('-f, --force', 'My flag');\n//=> currently has alias pair: f <--> force\n\nprog.parse(process.argv, {\n  alias: {\n    f: ['foo', 'fizz']\n  },\n  default: {\n    abc: 123\n  }\n});\n//=> ADDS alias pair: f <--> foo\n//=> REMOVES alias pair: f <--> force\n//=> ADDS alias pair: f <--> fizz\n//=> ADDS default: abc -> 123 (number)\n```\n\n#### opts.unknown\n\nType: `Function`<br>\nDefault: `undefined`\n\nCallback to run when an unspecified option flag has been found. This is [passed directly to `mri`](https://github.com/lukeed/mri#optionsunknown).\n\nYour handler will receive the unknown flag (string) as its only argument.<br>\nYou may return a string, which will be used as a custom error message. Otherwise, a default message is displayed.\n\n```js\nsade('sirv')\n  .command('start [dir]')\n  .parse(process.argv, {\n    unknown: arg => `Custom error message: ${arg}`\n  });\n\n/*\n$ sirv start --foobar\n\n  ERROR\n    Custom error message: --foobar\n\n  Run `$ sirv --help` for more info.\n*/\n```\n\n#### opts.lazy\n\nType: `Boolean`<br>\nDefault: `false`\n\nIf true, Sade will not immediately execute the `action` handler. Instead, `parse()` will return an object of `{ name, args, handler }` shape, wherein the `name` is the command name, `args` is all arguments that _would be_ passed to the action handler, and `handler` is the function itself.\n\nFrom this, you may choose when to run the `handler` function. You also have the option to further modify the `args` for any reason, if needed.\n\n```js\nlet { name, args, handler } = prog.parse(process.argv, { lazy:true });\nconsole.log('> Received command: ', name);\n\n// later on...\nhandler.apply(null, args);\n```\n\n### prog.help(cmd)\n\nManually display the help text for a given command. If no command name is provided, the general/global help is printed.\n\nYour general and command-specific help text is automatically attached to the `--help` and `-h` flags.\n\n> **Note:** You don't have to call this directly! It's automatically run when you `bin --help`\n\n#### cmd\nType: `String`<br>\nDefault: `null`\n\nThe name of the command for which to display help. Otherwise displays the general help.\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","publish_time":1514077877853,"_cnpm_publish_time":1514077877853,"dist-tags":{"latest":"1.8.1"}}