cliffy icon indicating copy to clipboard operation
cliffy copied to clipboard

[Question] Optional unordered key/value pairs ?

Open tianalemesle opened this issue 3 years ago • 5 comments

Hello,

How can I do the following using cliffy ?

--param2=foo --param1=bar --param3

Thanks

tianalemesle avatar Jun 03 '22 13:06 tianalemesle

Hey @tianalemesle. Cliffy doesn't currently support "options" with associated values like in your example. We only support unlabeled parameters. It's an interesting idea, so I'll think about how we might add support in the future.

Here is what the call would look like with cliffy: my_command foo bar @param3

Here's an example implementation:

const cli = new CLI()
    .addCommand("my_command", {
        description: "Do something",
        options: [{ label: "param3", description: "Param 3" }],
        parameters: ["param1", "param2],
        action: (params, options) => {
            if (options.param3) {
                console.log(`Option param3 was passed`)
                return;
            }

            console.log(params.param1) // foo
            console.log(params.param2) // bar
        },

Short explanation

In cliffy, "options" are specified using the @ symbol instead of the --. The reason we do this is to allow negative numbers to be passed as parameters.

drew-y avatar Jun 08 '22 20:06 drew-y

Yes, I've carfully read the README, and had to use vorpal for now, which is unmaintained :

cli
    .command(
        'list',
        'List hooks'
    )
    .option('--limit <limit>', 'Limit')
    .option('--status <status>', 'Filter by pending/completed/failed status')
    .option('--sortBy <sortBy>', 'Sort by creation/post date')
    .option('--sortOrder <sortOrder>', 'Sort asc/desc')
    .option('--minPostDate <minPostDate>', 'Filter by min post date')
    .option('--maxPostDate <maxPostDate>', 'Filter by max post date')
    .option('--minCreationDate <minCreationDate>', 'Filter by min creation date')
    .option('--maxCreationDate <maxCreationDate>', 'Filter by max creation date')

All those options need to be optional, unordered and associated with values. For example, @foo=bar @baz="fum qux" would be okay. Thanks

KaKi87 avatar Jun 09 '22 05:06 KaKi87

Yeah. Not fully supported yet. You can do optional parameters however.

drew-y avatar Jun 09 '22 19:06 drew-y

Yes, but ordered, which means an optional parameter cannot be specified without specifying the previous optional ones.

KaKi87 avatar Jun 10 '22 12:06 KaKi87