argv-one

cross-platform helper for process.argv[1]

Usage no npm install needed!

<script type="module">
  import argvOne from 'https://cdn.skypack.dev/argv-one';
</script>

README

argv-one

cross-platform helper for process.argv[1]

npm module Build Status Build status

Why?

I've recently started writing CLI tools for Windows with Node.js, and I've noticed that process.argv[1] doesn't have the same value there.

Installation

npm install --save argv-one

Usage

  • package.json:
{
  "bin": {
    "my-cli": "bin/index.js"
  }
}
  • bin/index.js:
#!/usr/bin/env node

var cmd = require('argv-one')({ argv: process.argv });

console.log(process.argv[1]);
// Windows: => "C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\my-cli\\bin\\index.js"
// OS X or Linux: => "my-cli"

console.log(cmd);
// Windows, OSX or Linux: => "my-cli"

API

argvOne({ argv?: String[], pkg?: Object, pkgPath?: String }) => String

If there is nothing wrong with your CLIs package.json or arguments, but process.argv[1] could not be reduced to the executable's base name, then proecess.argv[1] is returned as-is.