Store single values as string and not as list

This commit is contained in:
Patrick Elmer 2022-08-31 18:48:32 +09:00
parent e8ae940cb6
commit 421fc7107b
3 changed files with 28 additions and 15 deletions

View File

@ -29,7 +29,7 @@ args, kwargs = parse_args(argv=['pargv.py', '--name=pargv'])
# Specification # Specification
`parse_args` parses arguments in the following way, assuming the following command line arguments (`sys.argv`): `['/pargv/pargv.py', 'command', 'positional', '--flag', '--optional=value', 'test', '--output-file', 'filename', '-flg', 'name', 'name2']` `parse_args` parses arguments in the following way, assuming the following command line arguments (`sys.argv`): `['/pargv/pargv.py', 'command', 'positional', '--flag', '--optional=value', 'test', '--output-file', 'filename', '-ab', 'one', 'two']`
By calling `args, kwargs = parse_args()`, this would return the following list and dict: By calling `args, kwargs = parse_args()`, this would return the following list and dict:
@ -38,10 +38,9 @@ args = ['/pargv/pargv.py', 'command', 'positional']
kwargs = { kwargs = {
'flag': True, 'flag': True,
'optional': ['value', 'test'], 'optional': ['value', 'test'],
'output_file': ['filename'], 'output_file': 'filename',
'f': True, 'a': True,
'l': True, 'b': ['one', 'two'],
'g': ['name', 'name2'],
} }
``` ```
@ -49,6 +48,7 @@ kwargs = {
- All arguments before the first option (starting with a hyphen) are considered positional arguments (`args`) - All arguments before the first option (starting with a hyphen) are considered positional arguments (`args`)
- All other arguments are considered optional keyword arguments (`kwargs`) - All other arguments are considered optional keyword arguments (`kwargs`)
- Optional arguments without leading hyphens are considered as values to preceding keyword arguments and saved as list - Optional arguments without leading hyphens are considered as values to preceding keyword arguments
- Flags are recorded with the value `True` in the dict - One value of an option is stored as a string, multiple values are stored as a list
- Up to 2 leading hyphens are stripped from options, all other hyphens are converted into underscores (`---test-this-` would become `_test_this_`) - Flags are stored with the value `True` in the dict
- Up to 2 leading hyphens are stripped from options, all other hyphens are converted into underscores (`---test-this-` would become the key `_test_this_` in the dict)

View File

@ -38,4 +38,8 @@ def parse_args(argv=None):
key = arg[-1] key = arg[-1]
if len(values): if len(values):
kwargs[key] = values kwargs[key] = values
for k, v in kwargs.items():
if isinstance(v, list) and len(v) == 1:
kwargs[k] = v[0]
return (args, kwargs) return (args, kwargs)

View File

@ -50,12 +50,21 @@ def test_positional_and_optional_arguments():
} }
def test_short_arg_with_multiple_options(): def test_short_arg_with_single_option():
args, kwargs = parse_args(['-a', '-i', 'a', 'b']) args, kwargs = parse_args(['-a', 'b', '--abc', 'd'])
assert args == [] assert args == []
assert kwargs == { assert kwargs == {
'a': True, 'a': 'b',
'i': ['a', 'b'] 'abc': 'd',
}
def test_short_arg_with_multiple_options():
args, kwargs = parse_args(['-i', 'a', 'b', '--input', 'c', 'd'])
assert args == []
assert kwargs == {
'i': ['a', 'b'],
'input': ['c', 'd']
} }
@ -63,8 +72,8 @@ def test_long_args_with_equals():
args, kwargs = parse_args(['--input-file=a.py', '--output-file=b.py']) args, kwargs = parse_args(['--input-file=a.py', '--output-file=b.py'])
assert args == [] assert args == []
assert kwargs == { assert kwargs == {
'input_file': ['a.py'], 'input_file': 'a.py',
'output_file': ['b.py'] 'output_file': 'b.py'
} }
@ -80,7 +89,7 @@ def test_unintended_hyphen():
{ {
'flag': True, 'flag': True,
'optional': ['value', 'test'], 'optional': ['value', 'test'],
'output_file': ['filename'], 'output_file': 'filename',
'f': True, 'f': True,
'l': True, 'l': True,
'g': ['name', 'name2'], 'g': ['name', 'name2'],