How to take input and output file path via cli?

Hello Everyone,

I want to know how to take the input and output file path via CLI and use those paths in the npm package.

I want to make a command something like this.


and then I want to use these paths in my package to access files at these paths so that I can read the file and save the output to the output file that I received from the cli.

Anyone can help me with this.

Same answer as before, you access the instructions passed to the script via process.argv.

Tbh, I would just use a library for writing this as your needs become more complex, it’s much easier (eg commander or vorpal, and there are a load more)

1 Like


npx convert input.json -o output.yml
From the above command, I should access the arguments like this
process.argv[2] and process.argv[4]
and then pass these values to my npm package but the issue how to access these files path just by knowing a file name.

If I do this to access a file path

const inputFilename = process.argv[2]
const outputFilename = process.argv[4]
const inputpath = path.join(__dirname, inputFilename);
const outputpath = path.join(__dirname, outputFilename);

My doubt is how should I access inputpath and outputpath so that I can read it from my package using fs.

I am very new to this.

If there is any tutorial to understand this concept, please share with me

when I do console.log(inputpath, outputpath), it returns this
/Users/username/Documents/postman-to-openapi-cli/input.json /Users/username/Documents/postman-to-openapi-cli/output.yml

postman-to-openapi-cli is my package folder name, it’s searching for the file in my package directory, but my file is located somewhere else.

So that input/outputFilename, that can be any path. __dirname is a convenience variable Node gives you that is the directory the script is running in, so the filename can be any path from there – npx convert path/to/the/input.json -o path/to/the/output.yml

Note that one of the benefits of using a CLI library like the ones mentioned is that they normally provide path autocompletion in the terminal when you’re using a program built with them

Let say someone run the command from this folder

and the input and output file is stored in the downloads like this

then user should run this command
npx convert /Users/username/downloads/input.json -o /Users/username/downloads/output.yaml to convert the input file to output file.

I mean they need to give an absolute path where is the file saved or they need to give the path of the input and output file from the current directory?

No, __dirname will resolve to Users/username/documents, so it’s just whatever the path is after that.

path.join does a few things as well, it’s a safety mechanism. So for path.join(__dirname, thePath):

  • if the path is input.json, you’ll get Users/username/downloads/input.json.
  • if the path is /input.json, same
  • if the path is ./input.json, same
  • if will make sure the slashes in the path are the correct way depending on if you’re on Windows or a machine using UNIX-style (\ or / respectively)
  • ../ will work as expected (it’ll go up a directory)

Do use the docs, they’re pretty good:

And you can test in the console, like

> node
// Opens node repl
node> const path = require("path")
node> path.join(__dirname, "foo")

And so on

1 Like

I’ll read the docs after that will give you an update.


1 Like

I don’t know why I am not able to understand how to access those input/output files.

Can you please give me an example?

Let say you have an input.json file in the Downloads folder (/Users/username/downloads/input/input.json) and you need to put the output after conversion in the output.json file that is located in /Users/username/downloads/output/output.json.

And you open a terminal from the documents folder (/Users/username/documents)

How should you pass the both file path as an argument in the command(npx convert INPUT_FILE_PATH -o OUTPUT_FILE_PATH).

This is my code for cli.js file that runs when someone run the package from the cli.

#!/usr/bin/env node

const path = require('path')
const inputFilename = process.argv[2];

const outputFilename = process.argv[4];

const inputpath = path.join(__dirname, inputFilename);

const outputpath = path.join(__dirname, outputFilename);

console.log(inputpath, outputpath);

package URL: postman-to-openapi-cli - npm

It just returns the path of the input and output file

When I run this command
npx convert downloads/Webhook.postman_collection.json -o downloads/output.json from the /Users/username then it return this output

Input file path: /Users/username/node_modules/postman-to-openapi-cli/downloads/Webhook.postman_collection.json

Output file path: /Users/username/node_modules/postman-to-openapi-cli/downloads/output.json

why this append /node_modules/postman-to-openapi-cli in my path, it should be this
Input file path: /Users/username/downloads/Webhook.postman_collection.json

Output file path: /Users/username/downloads/output.json

I don’t know what I am missing?

Yes you’re not doing anything except printing out the paths. You’ve written a program that takes two paths to files and returns the fully resolved paths. What did you think this was going to do?

I changed the code a little bit.
Now, It works

Thanks for your help.

Hello there,
Now, Everything works fine
and I am using a commander library,
I just want to know am I doing it correctly?

const program = require('commander');

const ver = require('./package.json')
    .version(ver.version, '-v, --version')
    .option('-s, --source <source>', 'Relative path of the Postman Collection v2.0/v2.1')
    .option('-o, --output <output>', 'Relative path of the OpenAPI')
console.log(`Input: ${process.argv[3]} \nOutput: ${process.argv[5]}`)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.