npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

program-utils

v1.1.0

Published

A module to parse arguments and to provide easy configuration file

Downloads

3

Readme

program-utils

npm-version build-status npm-downloads dependencies-status

A module to parse arguments and to provide easy configuration file.

1.1.0 Changelog

  • It is now possible to prevent the value parsing with the config parser using := assignment.
  • Linted README.md and modified some things in it.

Parse config

The config style is inspired from the windows INI file. But it works slightly differently.

const programUtils = require('program-utils');
let parsedConfig = programUtils.parseConfig(filePath);

Exemples :

# example.conf

foo = bar
spam = true
t = false
nbr = 5.5

# you can do a comment with an '#' on a blank line
# => {foo:"bar", spam: true, t: false, nbr:5.5}

Every value that is not true, false, or a number is a string.

But you can do subobjects with the syntax [obj subobj]

# example.conf

var_in_global = bar

[test]
var_in_test_in_global = bar

[test comp]
var_in_comp_in_test_in_global = bar

# => {var_in_global:"bar",test:{var_in_test_in_global:"bar",comp:{var_in_comp_in_test_in_global:"bar"}}}

Notes and other examples

# example.conf

hey1 = this is a hey1
test = is this going to be overridden ?

[test]
res = yes !
# if you create a subobject with the same name as an existing property, the property will be overriden by the 
# subobject name

[]
hey2 = this is a hey2
# you can return to global scope with [], so "hey2" will be along "hey1"

[test]
# this case will not override the current test, so there is already "res" defined here
[]
test = not a subobject anymore
# if test is reassigned as a property in parent scope, the test subobject will be overriden also
[hey this is very cool]
# this scope is the "cool" object in "very" in "is" in "this" in "hey" in global
[hey this is]
# this scope targetting "is" in "this" in "hey" in global
[]
no_space= space
to_much_space=  space
# it's possible to have no space around the equal sign, or one (but more will add a space in the value)
# so in this example "no_space" is equal to "space" but "to_much_space" is equal to " space"

You can also force the config parser to not parse the value using := instead of =:

# Will be equal to 45 as a number
my_number=45
# Will be equal to "45" as a string
my_other_number:=45

# It works also with booleans
a_bool=true
string_bool:=true

Build a config

You might want to build a config, for example the first time your program is run, in this case you can use the config builder.

  const programUtils = require('program-utils');
  let builder = new programUtils.configBuilder();
  builder
    .addSection('hey') // this will add a [hey]
    .setValue('t', 't') // this will add "t=t"
    .letSpace() // this will jump a line
    .addComment('the line above is empty')
 // .toString(); -- Returns a string containing the config content
    .toFile('./config.conf'); // Directly writes config into a file

  /*  outputs in config.conf :

    [hey]
    t=t

    # this is cool


  */

Args parser

If you want to parse args you can do this by doing for example :

  const programUtils = require('program-utils');
  let argsParser = new programUtils.argsParser();
  let arr = ['-v'];
  let args = argsParser
  .addCharFlag('v') // Adds a mono char flag to be recognized
  .addCharFlag('h') // Adds a second
//.setSourceArray(arr, startingIndex) Sets the source array and its starting point
  .addStringFlag('help') // Adds a string arg
  .getResult();

So now when the program is launched it will result according to args passed For example

args => "-v"
{"flags":{"v":[]},"unknown":[]}

args => "-vh"
{"flags":{"v":[],"h":[]},"unknown":[]}

args => "-vhp"
{"flags":{"v":[],"h":[]},"unknown":["-p"]}

args => "-vh=45"
{"flags":{"v":[],"h":[45]},"unknown":[]}

args => "-vh=100,bar"
{"flags":{"v":[],"h":[100,"bar"]},"unknown":[]}

args => "-h=100 -v=bar"
{"flags":{"h":[100],"v":["bar"]},"unknown":[]}

args => "-h=100 -v=bar --help=true"
{"flags":{"h":[100],"v":["bar"],"help":[true]},"unknown":[]}

args => "-h=100 -v=bar --help=true --wtf"
{"flags":{"h":[100],"v":["bar"],"help":[true]},"unknown":["--wtf"]}

Here it is

License

MIT