Sywac Versions Save

:no_entry_sign: :mouse: Asynchronous, single package CLI framework for Node

v1.3.0

4 years ago

Thanks to @elliot-nelson, you can now configure sywac to error when unknown flags or arguments are encountered during parsing, giving your CLI an extra layer of security against typos for sensitive operations.

To enable strict mode, call the .strict() synchronous configuration method, just like you would do for any other configuration.

A good example

Consider this use-case from #17:

// example of sensitive operation WITHOUT strict mode
require('sywac')
  .boolean('-D, --dry-run', { desc: 'Dry run' })
  .command('delete-all', {
    desc: 'Deletes everything',
    run: argv => {
      console.log(argv.D ? 'no harm done' : 'wreak havoc')
    }
  })
  .help('-h, --help', { implicitCommand: false })
  .parseAndExit()
$ program delete-all --dyr-run
wreak havoc

Oh no! Without .strict() mode, the parser basically ignored the mistyped --dyr-run option.

But if we add .strict() mode:

// example of sensitive operation WITH strict mode
require('sywac')
  .boolean('-D, --dry-run', { desc: 'Dry run' })
  .command('delete-all', {
    desc: 'Deletes everything',
    run: argv => {
      console.log(argv.D ? 'no harm done' : 'wreak havoc')
    }
  })
  .help('-h, --help', { implicitCommand: false })
  .strict() // <-- just add this one line here
  .parseAndExit()
$ program delete-all --dyr-run
Usage: program delete-all [options]

Options:
  -D, --dry-run  Dry run               [boolean]
  -h, --help     Show help             [boolean]

Unknown options: --dyr-run

Whew! The option was mistyped and the CLI erred out before wreaking havoc.

Caveats

If you use .showHelpByDefault() along with .strict() mode for your command-driven CLI, sywac will still Do The Right Thing™ and print help text without an error until a runnable command is actually specified, at which point .strict() mode kicks in and makes sure all arguments and options for that command are kosher.

However, using .strict() mode along with your own custom default command (i.e. one with a * alias) will not work because strict mode will dominate parsing control before your default command is given a chance to run.

Again, thanks to @elliot-nelson, we have an idea that may fix this in the future, but for now see further info about this here.

Now it's your turn

Give the new .strict() mode a test spin and let us know what you think. Thanks!

v1.2.2

4 years ago

This is a patch release that reflects some very minimal updates to the published codebase.

Preserving functionality on Node 12

Now, using sywac on Node 12 works just the same as it does on Node 10 or Node 8.

Before this release, some validation messages might have printed to stdout like this on Node 12:

Value "[ 'web', 'docs' ]" is invalid for argument services. Choices are: web, api, db

Whereas the same error message on Node 10 would have printed like this:

Value "web,docs" is invalid for argument services. Choices are: web, api, db

This has been fixed to standardize on the latter.

Non-functional changes to the codebase

We've also made sure that the tests and dev dependencies are up-to-date and functioning properly in CI.

Some non-functional code changes were made to preserve syntax compatibility with standard 13.x.

Note that we've dropped testing support for all versions of Node that are EOL, including Node 4 and now Node 6, but the 1.x release line should still be compatible with those older versions of Node. In the future, we may upgrade the codebase to use async/await available in Node 8+, but at that time we will bump sywac to the next major version and start a 2.x release line.

Wrapping it all up

Regardless of the minimal changes, we want to make sure our community of users knows that the sywac team is still committed to maintaining and updating the CLI library and related tools. It's been a while since the previous release, but rest assured that sywac is still alive and kicking!

Drop us a line (er, open an issue) if you have any questions or concerns. Thanks!