zhmcclient / zhmccli

A CLI for the IBM Z HMC
Apache License 2.0
10 stars 9 forks source link

How to specify complex input parameters or properties in command line options #515

Closed andy-maier closed 10 months ago

andy-maier commented 1 year ago

There is a number of create / update command options that are not yet implemented because they need to set parameters/properties that have complex data types, and we do not yet have an agreed-upon way to specify those. There are a few cases of comma-separated string values, but more complex things than that are a gap today.

Example: The "Create Image Activation Profile" operation has an input parameter "ssc-network-info" whose type is an array of "ssc-network" data structures, which are dictionaries with 5 properties, one of which is again a dictionary with 2 properties.

This issue is to discuss and agree on a way to specify the value of such complex parameters or properties in zhmc command line options.

I did some research on this problem, but could not find anything useful. My research was probably not exhaustive, though.

The following lists some options I could think of:

Option 1: JSON

Description:

Pro/Con:

Example for "ssc-network-info":

--ssc-network-info '[{"port": 444, "ipaddr-type": "static", "vlan-id": 53, "static-ip-info": {"type": "ipv4", "ip-address": "10.11.12.13"}}]'

Example for "ssc-network-info" when using shell/env variables:

--ssc-network-info "[{\"port\": 444, \"ipaddr-type\": \"static\", \"vlan-id\": $vlan_id, \"static-ip-info\": {\"type\": \"ipv4\", \"ip-address\": \"$ip_address\"}}]"

Option 2: name=value syntax

Description:

Pro/Con:

Example for "ssc-network-info":

--ssc-network-info "[{port=444 ipaddr-type=static vlan-id=53 static-ip-info={type=ipv4 ip-address=10.11.12.13}]"

Example for "ssc-network-info" when using shell/env variables:

--ssc-network-info "[{port=444 ipaddr-type=static vlan-id=$vlan_id static-ip-info={type=ipv4 ip-address=$ip_address}]"

Examples with a (fantasy) escaped string value:

--properties '{description="ab\ncd"}'
--properties "{description=\"ab\\ncd\" name=\"${name}\"}"

Option 2 is implemented in PR #516

simon-spinner commented 1 year ago

Have you tried to use string concatenation in option 1?

--ssc-network-info '[{"port": 444, "ipaddr-type": "static", "vlan-id": '$vlan_id', "static-ip-info": {"type": "ipv4", "ip-address": "'$ip_address'"}}]'

You need to put the shell variables in single quotation marks (basically end and start the string again). But at least bash typically concatenates everything and passes it as a single string to the program

simon-spinner commented 1 year ago

Another observation: Option 2 looks very similar to the single line syntax of YAML. The most obvious difference is that you need commas between fields of an object and use : instead of =

andy-maier commented 1 year ago

Option 3: Single line YAML (aka "Flow Style")

The YAML spec calls this Flow Collection Styles.

Some sources claim that quotes around property names and string values would be needed, but that does not seem to be the case. Example: https://yaml-online-parser.appspot.com/?yaml=prop%3A+%7Bresult%3A+42%2C+name%3A+alice%7D%0A&type=json Also the YAML spec confirms that no quotes are needed, see Example 7.15: https://yaml.org/spec/1.2.2/#rule-ns-s-flow-map-entries

Note that the flow collection style YAML is a superset of JSON, so double quotes around property names and string values can also be used.

Example for "ssc-network-info":

--ssc-network-info "[{port: 444, ipaddr-type: static, vlan-id: 53, static-ip-info: {type: ipv4, ip-address: '10.11.12.13'}}]"

Example for "ssc-network-info" when using shell/env variables:

--ssc-network-info "[{port: 444, ipaddr-type: static, vlan-id: $vlan_id, static-ip-info: {type: ipv4, ip-address: '$ip_address'}}]"

Examples with a (fantasy) escaped string value:

--properties '{description: ab\ncd}'
--properties "{description: ab\\ncd}"

I must say, I like option 3 the best.

We probably need to write a help topic with a short explanation of the syntax, since the YAML spec is hard to read for occasional users.

More understandable descriptions of flow style that we could refrence:

Probably not so good:

andy-maier commented 11 months ago

I have implemented option 3 in PR #537 - if we agree on that PR, I consider the decision made for using option 3.

andy-maier commented 10 months ago

We agreed on PR #537, so we will continue using option 3 in the future. This issue is now complete.