js-fire

A javascript clone of google/python-fire

README

js-fire Node.js CI


A javascript implementation of google/python-fire


js-fire is a library for automatically generating command line interfaces
(CLIs) from most js objects.

- js Fire is a simple way to create a CLI in js.
- js Fire helps with exploring existing code or turning other people's code
  into a CLI.
- js Fire makes transitioning between Bash and js easier.

Installation


  1. ```
  2. yarn add js-fire
  3. ```

  1. ```
  2. npm install js-fire
  3. ```

js-fire is exposed as both an API and a CLI.

API Usage


You can call `Fire` on any functions and objects:

Here's an example of calling Fire on a object, you can infinitely nest objects to create subcommands.

  1. ``` js
  2. const fire = require("js-fire");

  3. const calculator = {
  4.   __description__: "I am a math machine",
  5.   double: (number) => {
  6.     // I double things
  7.     return 2 * number;
  8.   },
  9.   add: (n1 = Math.PI, n2) => {
  10.     return n1 + n2;
  11.   },
  12.   misc: {
  13.     year: () => "1999",
  14.     brand: () => "casio",
  15.     hello: (name) => `hello ${name}`,
  16.   },
  17. };

  18. fire(calculator);
  19. ```

Then, from the command line, you can run:

  1. ``` sh
  2. node calculator.js double --number=15  # 30
  3. ```

  1. ``` sh
  2. node calculator.js misc hello hobochild  # 'hello hobochild'
  3. ```

Automactic --help command.

  1. ``` sh
  2. node calculator.js --help

  3. USAGE:
  4. node calculator.js

  5. DESCRIPTION:
  6. I am a math machine

  7. COMMANDS:

  8. half  --number=<number>
  9. double  --number=<number>
  10. add  --n1=3.141592653589793  --n2=<n2>

  11. misc
  12.   year
  13.   brand
  14.   hello  --name=<name>
  15. ```

Automatic --interactive mode:
asciicast

For additional examples, see /examples.

CLI Usage


js-fire exposes a CLI that takes modulePath and passes it to js-fire.

  1. ``` sh
  2. USAGE:
  3. js-fire  --modulePath=<modulePath>
  4. ```

Example


So you can js-fire on _most_ js modules.

  1. ``` sh
  2. js-fire fs -- writeFileSync --path=hello.txt --data="hiii"
  3. ```

You can also use interactive and help mode to explore a modules api:

  1. ``` sh
  2. js-fire fs -- -h

  3. USAGE:
  4. js-fire

  5. COMMANDS:

  6. appendFile <flags> --path=<path>  --data=<data>  --options=<options>  --callback=<callback>
  7. appendFileSync <flags> --path=<path>  --data=<data>  --options=<options>
  8. access <flags> --path=<path>  --mode=<mode>  --callback=<callback>
  9. accessSync <flags> --path=<path>  --mode=<mode>
  10. chown <flags> --path=<path>  --uid=<uid>  --gid=<gid>  --callback=<callback>
  11. chownSync <flags> --path=<path>  --uid=<uid>  --gid=<gid>
  12. chmod <flags> --path=<path>  --mode=<mode>  --callback=<callback>
  13. chmodSync <flags> --path=<path>  --mode=<mode>
  14. close <flags> --fd=<fd>  --callback=<callback>
  15. closeSync <flags> --fd=<fd>
  16. copyFile <flags> --src=<src>  --dest=<dest>  --flags=<flags>  --callback=<callback>
  17. copyFileSync <flags> --src=<src>  --dest=<dest>  --flags=<flags>
  18. createReadStream <flags> --path=<path>  --options=<options>
  19. createWriteStream <flags> --path=<path>  --options=<options>
  20. exists <flags> --path=<path>  --callback=<callback>
  21. existsSync <flags> --path=<path>
  22.   ...
  23. ```