Giter Club home page Giter Club logo

node-arguments's Introduction

Node Arguments

Overview

Programs are more functional when we can change the input. If there's no way to change the input, it has to be hard-coded in the source code and this is a very bad practice. Imagine a function that calculates the sum of numbers. The numbers are hard-coded in the source code. You're doing your taxes, and each time you need to do a new calculation, you go to your file, change the values, hit save and re-run the program. What if you make a mistake in the code and delete some symbol when you're editing the number? And saving the file each time you need to calculate a sum will slow you down.

Your program needs to have an input which is independent of the source code (the program itself). The easiest way to implement such input is to use command-line arguments. This lesson will cover the Node command-line arguments and how to access them.

Objectives

  1. Describe how the CLI argument input to Node script works
  2. Describe an argument array
  3. Describe first arguments

CLI Arguments

How do we pass data to Node? node is just a command-line program and we can pass arguments to it. For example, we can pass two numbers to a program (file sum.js) that calculates the sum of them:

node sum.js 10 47

The result of 10 + 47 is 57. We can pass numbers or strings as arguments. This approach is useful when you're running scripts which require small enough input that you can fit it in the command line, or when building command-line tools with Node. Another use case is when you have a complex program that needs to be launched in different modes, e.g., development mode with more verbose logging versus production mode.

Accessing Arguments

You've learned how to pass arguments, but how can you access them when you write your own program instead of using existing programs like sum.js? It's straightforward. All you need is to access process.argv, which is an array of the arguments passed to the file. All JavaScript array methods are available.

Consider the sum.js program. First we use shift to get rid of the first two elements and then forEach() to iterate through an indefinite number of arguments. Then we convert each argument to a number and add them up to the sum variable:

var sum = 0
process.argv.shift()
process.argv.shift()
process.argv.forEach(function summarize(item, index, list) {
  sum += parseInt(item, 10)
})
console.log(sum)

First Arguments

The first arguments of the array were node and sum or the command and the script name. This is exactly what we had in the command line when we were running the program:

node sum 10 47

Note: The .js extension is optional. Node will try to match the filename to what's in the folder. If there's a sum.js file, it will run it.

The process.argv array will look something like this:

[ '/usr/local/bin/node',
  '/Users/azat/Documents/Code/learn-co/node-arguments/sum',
  '10',
  '47' ]

It will have four items. The first two might be different on your machine, but the 10 and 47 will be the same.

Just for fun, take a look at this command in which we use -e to execute code right away without saving it to a file (without saving means there's no file, none, zip, nada!):

node -e "console.log(process.argv)" 10 47

Note: If you need a refresher on the -e option, go back to node-run-node.

Can you guess what it will output? That's right. The numbers will start from the second argument, not third as in the sum.js example:

[ '/usr/local/bin/node', '10', '47' ]

The difference is self evident when you look at the command. The first command had two arguments (node and the name of the file) and the second command (-e) only one which was node.

When working with process.argv, it's important to be mindful of the first arguments. They might not be the data you expect.

Resources

  1. process.argv official documentation
  2. Unix and Node: Command-line Arguments
  3. Using Command Line Arguments in a Node Script
  4. Commander.js: Solution for command-line interfaces
  5. Yargs: CLI Arguments Parser

View node-arguments on Learn.co and start learning to code for free.

View Node Arguments on Learn.co and start learning to code for free.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.