ovh-cli icon indicating copy to clipboard operation
ovh-cli copied to clipboard

How to pass array arguments

Open YouriT opened this issue 8 years ago • 6 comments

Great job, it's a nice wrapper :)

I was just wondering how do you pass an array with the cli?

For example the call on ./ovh-eu cloud project PROJECT_ID instance create can take --networks which is an array containing objects that contain networkId but I've no clue on how to pass that.

Any hints would be lovely!

YouriT avatar Nov 22 '16 16:11 YouriT

I double checked the code, it does not appear to be supported at all. This code I wrote a while back so... I may miss something.

Can you test linked commit ? It adds support for "text", "boolean" and complex types. For complex types, you can pass it encoded as json like --networks '[{}, {}, ...]'.

Using json is not ideal but I have no idea how to support arrays of structures without it. For simple arrays, I could just allow the argument to be repeated multiple times. For structures we could imagine some kind of "dot notation". But the mix of both is not trivial. And AFAIK, few people are using this code anyway...

yadutaf avatar Nov 22 '16 22:11 yadutaf

Works fine, merci bien !

Indeed it would require some additional work from your side and therefore this way is a good "hack" to achieve the purpose I was aiming to.

If you want a suggestion for the arguments, would use the standard JS notation which is pretty straightforward and avoids confusion on what will be the output like this:

./ovh-eu \
    --networks[0].networkId 89YT8675FB
    --networks[1].networkId R79090I969Y # order doesn't matter
    --networks[0].otherVariable value
# would result in
[{"networkId":"89YT8675FB","otherVariable":"value"},{"networkId":"R79090I969Y"}]

Are you yourself using this module?

P.S.: Can you merge the change?

YouriT avatar Nov 23 '16 13:11 YouriT

Hi,

The changed is merged. I really like your proposition. It feels clean and standard. The bad news is, there is no way I can parse it with argparse so I'll need to find / write another parser and... that's a pain. sight.

I use the api.ovh.com/console interface by default and use the CLI when I don't have my 2 factor auth device nearby :)

yadutaf avatar Nov 25 '16 22:11 yadutaf

OK, here is a slight improvement:

  • complex types can be passed as json
  • arrays of simple types (str, int, bool, enaum, ...) can be passed like --arg 1 --arg 2 ...
  • arrays of complex types can be passed like --complex '{"key": "value 1"}' --complex '{"key": "value 2"}' ...

This is not yet the best / perfect way. But that's as far as I can go without rewriting argparse. Which I sadly don't have the time to do...

yadutaf avatar Nov 26 '16 19:11 yadutaf

@yadutaf thanks a lot anyhow, it's way better than nothing :) 👍 👍

YouriT avatar Dec 01 '16 16:12 YouriT