Creating a Command Line Tool

Are you tired of this?


    $ node adder.js 4 9
    4 + 9 = 13

Do you want this?


    $ adder 4 9
    4 + 9 = 13

WELL NOW YOU CAN!

Method 1:

Command Line Executable Javascript

Method 1

  • Create a Javascript file
 
    $ touch adder.js

Method 1

  • Create a Javascript file
  • Give it execute permission

    $ chmod +x adder.js

Method 1

  • Create a Javascript file
  • Give it execute permission
  • Tell it to interpret using Node
    
    #!/usr/bin/env node

    console.log('I added those 2 numbers in my head... I promise');

Method 1

  • Create a Javascript file
  • Give it execute permission
  • Tell it to interpret using Node
  • Run it!

    $ ./adder.js 50 4

    I added those 2 numbers in my head... I promise

Method 1

  • Create a Javascript file
  • Give it execute permission
  • Tell it to interpret using Node
  • Run it!
  • Link it to your $path
    
    $ ln adder.js /usr/local/bin/adder

Method 1

  • Create a Javascript file
  • Give it execute permission
  • Tell it to interpret using Node
  • Run it!
  • Link it to your $path
  • Run it!

    $ adder 50 4

    I added those 2 numbers in my head... I promise

Method 1: Overview


    $ touch adder.js

    $ chmod +x adder.js

    $ ln adder.js usr/local/bin/adder

    $ adder 17 24
    17+24=41

    #!/usr/bin/env node

    var a1 = Number(process.argv[2]);
    var a2 = Number(process.argv[3]);
    var sum = a1 + a2;

    console.log(a1 + "+" + a2 + "=" + sum);

Great! Can I require any Node packages?

No.

Method 2:

Make Your Own Node Package

Method 2

  • Make a package.json file

    $ npm init

    name: (directory-name)
    version: (1.0.0)
    entry point: (index.js)
    blah: 
    blah:
    blah:
    

Method 2

  • Make a package.json file
  • Specify the command name
    {
      "name": "slide-test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\""
      },
      "author": "",
      "license": "ISC",

    ///////////////////////////////////////
      "bin": {
        "commandname":"path/to/file"
      },
      "preferGlobal": true
    ///////////////////////////////////////

    }

Method 2

  • Make a package.json file
  • Specify the command name
  • Create a Javascript file

    #!/usr/bin/env node

    console.log("hello, world");

Method 2

  • Make a package.json file
  • Specify the command name
  • Create a Javascript file
  • Add the package to the $path

    $ npm link

Method 2

  • Make a package.json file
  • Specify the command name
  • Create a Javascript file
  • Add the package to the $path
  • Run it anywhere!

Method 2: Overview


    $ npm init

    $ touch adder.js

    $ npm link

    $ adder 17 24
    17+24=41

    adder.js:    

    #!/usr/bin/env node

    var a1 = Number(process.argv[2]);
    var a2 = Number(process.argv[3]);
    var sum = a1 + a2;

    console.log(a1 + "+" + a2 + "=" + sum);

    package.json:

    "bin": {
        "commandname":"path/to/file"
      },
      "preferGlobal": true

Publishing to NPM

Publishing to NPM

  • Create or login a user

    $ npm adduser

    OR

    $ npm login

Publishing to NPM

  • Create or login a user
  • Publish

    $ npm publish

Publishing to NPM

  • Create or login a user
  • Publish
  • Update

    $ npm version [patch]

CommanderJS:

Making command line parsing FUN!

CommanderJS

  • Specify Command Line Options
 
    program
    	.option('-c, --captain', 
            'Specify that this person is a captain.')
    	.option('-n, --name [name]', 
            'Specify the name of the person.', 
            'Jack Sparrow')
        .parse(process.argv);
    
    console.log(program.name);

CommanderJS

  • Specify Usage
    
    program
    	.usage('<directory> [options]');
    

CommanderJS

  • Specify a Command and an Action
    
    program
    	.command('angular <someVar>')
    	.description('')
    	.action(function callback(someVar, options) {
            console.log('do some things');
        });
    
 

CommanderJS

  • Specify a Command from another file
    
    program
    	.command('angular', 'this generator does some stuff!')
    	.command('angular-fullstack', 'this generator does more stuff!');
    
 

CommanderJS

  • Alias a command
    
    program
    	.command('yeoman')
        .alias('yo')
        .action(someCallback);
    

CommanderJS

  • Receive automated help
    
    $ griffin --help
    
    Usage: griffin [options] [command]


      Commands:
    
        compliment [options]             generate a random compliment
        calculate|calc [options] <base>  do some pretty complicated math
    
      Options:
    
        -h, --help  output usage information

DEMO TIME!

Command Line Tool

  • compliment
  • calculate

Required package

  • add
  • subtract
  • multiply
  • divide

npm install

griffins-awesome-project

Command Line Tool

By gtelljohann

Command Line Tool

  • 531
Loading comments...

More from gtelljohann