Git Product home page Git Product logo

string-argv's Introduction

What is it?

string-argv parses a string into an argument array to mimic process.argv. This is useful when testing Command Line Utilities that you want to pass arguments to and is the opposite of what the other argv utilities do.

Installation

npm install string-argv --save

Usage

// Typescript
import stringArgv from 'string-argv';

const args = stringArgv(
  '-testing test -valid=true --quotes "test quotes" "nested \'quotes\'" --key="some value" --title="Peter\'s Friends"',
  'node',
  'testing.js'
);

console.log(args);
// Javascript
var { parseArgsStringToArgv } = require('string-argv');

var args = parseArgsStringToArgv(
    '-testing test -valid=true --quotes "test quotes" "nested \'quotes\'" --key="some value" --title="Peter\'s Friends"',
    'node',
    'testing.js'
);

console.log(args);
/** output
[ 'node',
  'testing.js',
  '-testing',
  'test',
  '-valid=true',
  '--quotes',
  'test quotes',
  'nested \'quotes\'',
  '--key="some value"',
  '--title="Peter\'s Friends"' ]
  **/

params

required: arguments String: arguments that you would normally pass to the command line.

optional: environment String: Adds to the environment position in the argv array. If ommitted then there is no need to call argv.split(2) to remove the environment/file values. However if your cli.parse method expects a valid argv value then you should include this value.

optional: file String: file that called the arguments. If omitted then there is no need to call argv.split(2) to remove the environment/file values. However if your cli.parse method expects a valid argv value then you should include this value.

string-argv's People

Contributors

cellule avatar chocolateboy avatar gliviu avatar jamesmessinger avatar mccormicka avatar rafase282 avatar styfle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

string-argv's Issues

Doesn't support empty quotes

An empty pair of quotes (e.g. "") should pass an empty string argument. But string-argv currently passes the actual quote characters instead.

STEPS TO REPRODUCE

$ node
> var stringArgv = require('string-argv');
undefined
> stringArgv('git commit --message ""')

EXPECTED RESULTS

[ 'git', 'commit', '--message', '' ]        // <--- Last arg is an empty string

ACTUAL RESULTS

[ 'git', 'commit', '--message', '""' ]      // <--- Last arg is two quotes

Nested quotes with no spaces not handled

Hi, congrats on the lib! It is a dependency for the exits bin, and I've found this to be an issue on string-argv@0.1.2:

const toArgv = require('string-argv');
console.log(toArgv('jake run:silent["echo 1"]'));

Expect: [ 'jake', 'run:silent["echo 1"]' ]
Get: [ 'jake', 'run:silent["echo 1"', ']' ]

Escaped space not handled

STEPS TO REPRODUCE
$ node
> var stringArgv = require('string-argv');
undefined
> stringArgv('one\ one two')
EXPECTED RESULTS

[ 'one one', 'two' ]

ACTUAL RESULTS

[ 'one', 'one', 'two' ]

Doesn't correctly handle nested quotes

test.js

let parseArgv = require('string-argv').parseArgsStringToArgv;
let string = `--title "Peter's Friends" --name 'Phil "The Power" Taylor'`;

console.log(parseArgv(string));

expect

[ "--title", "Peter's Friends", "--name", 'Phil "The Power" Taylor' ]

get

[ "--title", "Peter", "s", "Friends", " --name ", "Phil", "The Power", "Taylor" ]

Missing license file

Hello, is is possible to incorporate the LICENSE file inside the repository and the npm package. Now it's missing. It's implied by MIT license that if licensed under the MIT the source code must be accompanied by full license text. If the license file is not in the npm package or the repo a lot of people cannot use it.

Thank you

Supports for shell script string?

$ node
> var stringArgv = require('string-argv');
undefined
> stringArgv('eslint `git ls-files`')
[ 'eslint', '`cat', 'a`' ]
> stringArgv('eslint $(git ls-files)')
[ 'eslint', '$(cat', 'a)' ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.