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

Allow script to transform flag input via callback #81

Open
jrmykolyn opened this issue Mar 15, 2018 · 3 comments
Open

Allow script to transform flag input via callback #81

jrmykolyn opened this issue Mar 15, 2018 · 3 comments

Comments

@jrmykolyn
Copy link

WHAT
Allow dependent scripts to (optionally) specify how flag input should be parsed by providing callback functions at instantiation time.

Callback functions would be applied to the raw CLI input, and the resulting values would be exposed to the dependent script as normal (eg. via cli.flags).

For example:

meow( {
    flags: {
        unicorn: {
            alias: 'u',
            callback: value => {
                // Perform transformation and return result. 
            }
        }
    }
} );

WHY
This would be useful for cases where the underlying API requires an array value for a specific option.

Rather than updating the API to support/parse both arrays and strings, the transformation could be handled directly in the CLI layer.

For example:

meow( {
    flags: {
        excludes: {
            callback: value => {
                return value.split(',');
            }
        }
    }
} );
$ <cli command> <input> --excludes logs,temp,dist // [ 'logs', 'temp', 'dist' ]

HOW
I've stubbed out an initial implementation and test on my fork, which I'd be happy to submit for review.

NOTES
I stumbled on Issue #44 after digging into this proposed update. I'm not sure what the state of that issue is, but it looks like there's at least some overlap in terms of functionality.

@sindresorhus
Copy link
Owner

Yeah, I think this could be useful. I would name it parse instead of callback though.

Rather than updating the API to support/parse both arrays and strings,

I think it would be useful to support an array type. The problem is that there are two conventions for specifying arrays. Either foo --array=1 --array=2 or foo --array=1,2.

@jrmykolyn
Copy link
Author

@sindresorhus Good points!

The problem is that there are two conventions for specifying arrays

If both conventions are well known/understood, I think the second example (flag name appears once, followed by comma delimited values) wins out in terms of usability. Alternatively, would it be possible to support both styles?

If meow is updated to support arrays, will it be possible to enforce a specific type on the values?

@jrmykolyn
Copy link
Author

Anything I can do to help?

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

2 participants