foparsearguments

Function to parse command line arguments for Node.js programs

Usage no npm install needed!

<script type="module">
  import foparsearguments from 'https://cdn.skypack.dev/foparsearguments';
</script>

README

foParseArguments

Parse Node.js arguments library.

Getting Started

  1. Install foParseArguments via NPM.

npm install foparsearguments

Optionally: rename foparsearguments to foParseArguments: npm is unable to handle the complexity of uppercase characters in a module name. Node.js on Windows does not have this problem, so renaming the folder is not required for you to use the module.

  1. Require foParseArguments in your project.

var foParseArguments=require("foParseArguments");

  1. Parse arguments using foParseArguments.
var oArguments = foParseArguments({
  "dfxTypesConverters": { 
    "myType": function (sValue) {
      if (!isvalid(sValue) throw new Error("must be a valid xxx");
      return processed(sValue);
    }, // define a type
    ...
  },
  "adxParameters": [ 
    {
      "sName": "first parameter",
      "sTypeDescription": "string",
      "sHelpText": "This will be shown when you run the script with -?",
      "xDefaultValue": "first"
    },
    {
      "sName": "second parameter",
      "sTypeDescription": "int",
      "sHelpText": "This will be shown when you run the script with -?",
      "xDefaultValue": 2
    },
    ...
  ],
  "dxSwitches": {
    "switch-name": {
      "sHelpText": "This will be shown when you run the script with -?",
    },
    ...
  ],
  "dxOptions": {
    "option-name": {
      "sTypeDescription": "myType", // option must be of mytype.
      "sHelpText": "This will be shown when you run the script with -?",
      "xDefaultValue": 2
    },
    ...
  }
});
  1. Used parsed arguments in your script
var uSomeNumber = oArguments.dxOptions["some-number"];
var sSomeString = oArguments.axParameters[0];
var bSomeBoolean = oArguments.dbSwitches["some-boolean"];

Notes

Alpha release

This has not been extensively tested and the API will most likely change in the future.


License

This code is licensed under CC0 v1.0 Universal.