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

argv-split

v3.2.1

Published

Split argv(argument vector) and handle special cases, such as quoted values.

Downloads

33,417

Readme

Build Status

argv-split

Split argv(argument vector) and handle special cases, such as quoted or escaped values.

Why?

const split = require('split')

const mkdir = 'mkdir "foo bar"'
mkdir.split(' ')    // ['mkdir', '"foo', 'bar"']  -> Oops!
split(mkdir)        // ['mkdir', 'foo bar']       -> Oh yeah!

const mkdir2 = 'mkdir foo\\ bar'.split(' ')
mkdir2.split(' ')   // ['mkdir', 'foo\\', 'bar']  -> Oops!
split(mkdir2)       // ['mkdir', 'foo bar']       -> Oh yeah!

argv-split handles all special cases with complete unit tests.

# shell command:        javascript array:
foo a\ b                # ['foo', 'a b']
foo \'                  # ['foo', '\\\'']
foo \"                  # ['foo', '\\"']
foo "a b"               # ['foo', 'a b']
foo "a\ b"              # ['foo', 'a\\ b']
foo '\'                 # ['foo', '\\']
foo --abc="a b"         # ['foo', '--abc=a b']
foo --abc=a\ b          # ['foo', '--abc=a b']

# argv-split also handles line feeds
foo \
    --abc=a\ b          # ['foo', '--abc=a b']

# etc
split('foo \\\n    --abc=a\\ b')    // ['foo', '--abc=a b']

Error Codes

UNMATCHED_SINGLE

If a command missed the closing single quote, the error will throw:

Shell command:

foo --abc 'abc
try {
  split('foo --abc \'abc')
} catch (e) {
  console.log(e.code)   // 'UNMATCHED_SINGLE'
}

UNMATCHED_DOUBLE

If a command missed the closing double quote, the error will throw:

foo --abc "abc

ESCAPED_EOF

If a command unexpectedly ends with a \, the error will throw:

foo --abc a\# if there is nothing after \, the error will throw
foo --abc a\ # if there is a whitespace after, then -> ['foo', '--abc', 'a ']

NON_STRING

If the argument passed to split is not a string, the error will throw

split(undefined)

Install

$ npm i argv-split

Methods

split(string) -> Array

Splits a string, and balance quoted parts. The usage is quite simple, see examples above.

Returns Array<string>

split.join(args, options?) -> string

Join the given array of argument vectors into a valid argument string

New in 3.1.0

  • args Array<string> arguments to be joined
  • options? Object=
    • quote string=" should we use single quote or double quote when a certain argument needs to be quoted. Defaults to "
'command ' + join(['foo "bar', "'baz"])

// command "foo \"bar" "'baz"

Handle Line Feeds

There is a special value of split.LF which could help us to create valid commands with line feeds:

'kubectl' + join(['apply', '--prune', '-f', 'manifest.yaml', split.LF, '-l', 'app=nginx'])

// kubectl apply --prune -f manifest.yaml \
// -l app=nginx

License

MIT