MySQL Shell 8.0

5.8.2.2 Defining Arguments

As mentioned in Section 5.8.2.1, “Command Line Integration for MySQL Shell API Functions”, most of the APIs available in MySQL Shell expect a specific data type for the arguments being provided. Values in command-line arguments can be provided using the JSON specification with the following considerations. Some terminals do their own pre-processing of the data which can impact the way the data is provided to MySQL Shell, and this varies depending on the terminal being used. For example:

MySQL Shell interprets the values as provided by the terminal it is running in, therefore you must provide the data to the terminal in a way that is correctly formatted. For example:

The following examples illustrate some of the handling of parameters.

When using the command-line integration, defining a JSON array has its own caveats. For example, in the MySQL Shell interactive mode you define a JSON array as:

["simple",123]

To use the same array in the command-line integration requires specific quoting. The following example illustrates how to correctly quote the JSON array:

To use a JSON array which contains JSON objects requires quoting in a similar way. For example, in the MySQL Shell interactive mode you define a JSON array which contains JSON objects as:

{"firstName":"John","lastName":"Smith"}

The following example illustrates how to correctly quote the same array in the command-line integration:

Due to the difficulties shown and the fact that the way the terminals in different platforms behave might be different, the following formats are supported.

String Arguments

Strings require quoting only in the following cases:

  • The value contains spaces

  • The value itself contains commas and is for a list parameter (to avoid splitting)

  • The value contains escaped characters

  • The value is a number, null, true, false but it is meant to be a string. In these cases the value should be quoted with inner escaped quotes. In other words, if a string value is "true", it should be defined in a CLI call as ""true"".

List Arguments

In addition to a JSON array, an argument for a list parameter can be provided as:

  • a comma separated list of values

  • separate anonymous arguments

When a list parameter is being processed (in positional order), all of the remaining anonymous arguments are part of the list. The following MySQL Shell CLI calls are equivalent:

  • Using a comma separated list of values:

    $ mysqlsh root@localhost -- util dump-schemas sakila,employees
  • Using consecutive anonymous arguments:

    $ mysqlsh root@localhost -- util dump-schemas sakila employees
  • Using a JSON array:

    $ mysqlsh root@localhost -- util dump-schemas ["\"sakila\"","\"employees\""]
Dictionary Arguments

Dictionaries are created using key-value pairs, the value for a key in a dictionary argument can also be specified using named arguments:

--key=value

The following MySQL Shell CLI call illustrates how the threads and osBucketName keys are defined for the options parameter in the util.dumpInstance() function:

$ mysqlsh -- util dump-instance my-dump --threads=8 --osBucketName=my-bucket

List Keys

You can define the values of a list key in a dictionary in the following ways:

  • Defining the value as a JSON array.

  • Defining the value as a comma separated list of values.

  • Defining values for the key repeatedly.

For example, in the following calls, the definition of the excludeSchemas key passed to the util.dumpInstance() operation is equivalent:

  • Using a comma separated list of values:

    $ mysqlsh root@localhost -- util dump-instance --outputUrl="my-dump" --excludeSchemas=sakila,employees
  • Using a JSON array:

    $ mysqlsh root@localhost -- util dump-instance --outputUrl="my-dump" --excludeSchemas=["\"sakila\"","\"employees\""]
  • Defining several values for the --excludeSchemas key:

    $ mysqlsh root@localhost -- util dump-instance --outputUrl="my-dump" --excludeSchemas=sakila --excludeSchemas=employees

Dictionary Keys

Nested dictionaries are supported with the following restrictions:

  • Only one level of nesting is supported.

  • Validation for inner pre-defined keys is not supported.

  • Validation for inner expected data types is not supported.

The syntax to define a value for a key in a nested dictionary is as follows:

--key=innerKey=value

For example, to define the decodeColumns key and pass it to the util.importTable() operation:

$ mysqlsh -- util import-table --decodeColumns=myColumn=1
Additional Named Arguments

As shown in the previous section, dictionary parameters are supported through named arguments using the --key=value syntax. There is another case when arguments must be specified as named arguments: parameters which are defined after a list parameter. The most convenient way to provide arguments that belong to a list parameter is by using anonymous arguments, for example as shown in the example at List Arguments:

$ mysqlsh root@localhost -- util dump-schemas sakila employees

However, this example is missing the argument for the outputUrl parameter, which is mandatory for the util.dumpSchemas() operation. Because all of the remaining anonymous arguments are included as items in the schemas list, there is no way to specify the outputUrl as an anonymous argument. For example the following would not work:

$ mysqlsh root@localhost -- util dump-schemas sakila employees path/to/dump

In this call, the path path/to/dump would be interpreted as another item in the schemas list. For this reason, any parameter defined after a list parameter must be specified as a named argument when calling the function from the command-line. For example:

$ mysqlsh root@localhost -- util dump-schemas sakila employees --outputUrl=path/to/dump