saltstack/salt — pull request #11368 by terminalmage (2014-03-19 23:24:42+00:00)
Title: Overhaul argument parsing

This pull request moves argument parsing out of ``salt.minion.parse_args_and_kwargs()``, and renames this function to ``salt.minion.load_args_and_kwargs()`` (a wrapper function has been put in its place to call the new function, as well as log a deprecation warning).

All argument parsing is now done via a new utils module (``salt.utils.args``), and more specifically, through the function ``salt.utils.args.parse_input()``. This function expects a list of string'ed args and kwargs, like so:

```python
['foo', 'bar', 'baz', 'abc=123', 'def={"foo": "bar", "baz": "qux"}']
```

... and parses them, running them through ``yaml.safe_load()`` to convert the strings to data structures, producing the following list which matches what @jacksontj's recent changes to ``salt.minion.parse_args_and_kwargs()`` expect to see:

```python
['foo', 'bar', 'baz', {'abc': 123, 'def': {'foo': 'bar', 'baz': 'qux'}, '__kwarg__': True}]
```

Backwards compatibility is preserved just in case someone tries to pass string-based args and kwargs to the function, and a deprecation notice will be triggered if a string'ed kwarg is present.

All refs to ``salt.minion.parse_args_and_kwargs()`` have been updated to using this new method of arg parsing.

Testing with CLI input appears to verify that the argument parsing works as expected:

```python
root@virtubuntu:~# salt virtubuntu test.arg 'Hello world' string_arg='foo' dict_arg='{"abc": 123}' list_arg='[1,2,3]' int_arg=123 float_arg=1.23 --out=pprint

{'virtubuntu': {'args': ['Hello world'],
'kwargs': {'__pub_arg': ['Hello world',
{'__kwarg__': True,
'dict_arg': {'abc': 123},
'float_arg': 1.23,
'int_arg': 123,
'list_arg': [1, 2, 3],
'string_arg': 'foo'}],
'__pub_fun': 'test.arg',
'__pub_jid': '20140318210925200208',
'__pub_ret': '',
'__pub_tgt': 'virtubuntu',
'__pub_tgt_type': 'glob',
'__pub_user': 'root',
'dict_arg': {'abc': 123},
'float_arg': 1.23,
'int_arg': 123,
'list_arg': [1, 2, 3],
'string_arg': 'foo'}}}
```

In fact, in the process of doing this refactoring, I actually caught and fixed a bug in which floats were being loaded as strings, instead of floats. This issue is fixed by the pull request, as well.