Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array support #62

Open
coolaj86 opened this issue Feb 12, 2015 · 5 comments
Open

array support #62

coolaj86 opened this issue Feb 12, 2015 · 5 comments

Comments

@coolaj86
Copy link

If you ever have a rainy day and want to implement an 'array' type such that I could do -a foo -a bar to produce [ "foo", "bar" ] would be awesome.

@therebelrobot
Copy link

+1

@deadratfink
Copy link

deadratfink commented Jul 26, 2016

Could it be more intuitive to provide it as follows on cli: -a ["foo","bar"], try to detect the enclosing brackets and JSON.parse the whole token...

Maybe we have to escape the paranthesis.

@lejenome
Copy link

you would then have no matches error on Bash/Zsh.

A better solution would be -a arg1,arg2,"long arg"

@deadratfink
Copy link

deadratfink commented Jul 26, 2016

Pls check out this small piece of code (index.js):

#!/usr/bin/env node

'use strict';

var cli = require('cli');
var path = require('path');
var assert = require('assert');

var usage = path.basename(__filename) + ' [OPTIONS]';
var packagePath = __dirname + '/package.json';

var options = {
    a: [ 'a', 'The a', 'string', '[]' ], // of course, there needs to be the native support for Array here later! 
    b: [ 'b', 'The a', 'string', 'b' ]
};

function main(args, options) {
    cli.info(JSON.stringify(options, null, 4));
    options.a = JSON.parse(options.a);
    cli.info(JSON.stringify(options, null, 4));
    assert(Array.isArray(options.a), 'options.a should be array');
}

cli.setUsage(usage);
cli.setApp(packagePath);
cli.parse(options);
cli.main(main);

When I call it like this:

$ ./index.js -a "[\"foo bar\", {\"foo\": 4}]" -b testb

or even unescaped double quotes:

$ ./index.js -a '["foo bar", {"foo": 4}]' -b testb

it prints:

INFO: UNPARSED: {
    "a": "[\"foo bar\", {\"foo\": 4}]",
    "b": "testb"
}
INFO: ARRAY PARSED: {
    "a": [
        "foo bar",
        {
            "foo": 4
        }
    ],
    "b": "testb"
}

At least, couldn't that be a starting point to implement in cli? Or am I missing something here...?

BTW, I tried that on Zsh.

@YounGoat
Copy link

YounGoat commented Dec 30, 2017

@coolaj86 Good requirement! I have supported such idea in package commandos with MULTIPLE decorator:

const commandos = require('commandos');

const options0 = commandos.parse('test -a foo', { options: [ '-a MULTIPLE' ] });
// options0 := { $: [] }

const options1 = commandos.parse('test -a foo', { options: [ '-a MULTIPLE' ] });
// options1 := { a: [ 'foo' ], $: [] }

const options2 = commandos.parse('test -a foo -b bar', { options: [ '-a MULTIPLE' ] });
// options2 := { a: [ 'foo', 'bar'], $: [] }

The returned options will have or not a property named a. If it has, the property will be an array no matter how many -a <value> set in CLI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants