diff --git a/README.md b/README.md index 461b0bf..108c685 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ args, kwargs = parse_args(argv=['pargv.py', '--name=pargv']) # 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: @@ -38,10 +38,9 @@ args = ['/pargv/pargv.py', 'command', 'positional'] kwargs = { 'flag': True, 'optional': ['value', 'test'], - 'output_file': ['filename'], - 'f': True, - 'l': True, - 'g': ['name', 'name2'], + 'output_file': 'filename', + 'a': True, + 'b': ['one', 'two'], } ``` @@ -49,6 +48,7 @@ kwargs = { - All arguments before the first option (starting with a hyphen) are considered positional arguments (`args`) - 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 -- Flags are recorded 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 `_test_this_`) +- Optional arguments without leading hyphens are considered as values to preceding keyword arguments +- One value of an option is stored as a string, multiple values are stored as a list +- 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) diff --git a/pargv.py b/pargv.py index e9c921d..71f189d 100644 --- a/pargv.py +++ b/pargv.py @@ -38,4 +38,8 @@ def parse_args(argv=None): key = arg[-1] if len(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) diff --git a/tests/test_functionality.py b/tests/test_functionality.py index b317998..3f4fd1f 100644 --- a/tests/test_functionality.py +++ b/tests/test_functionality.py @@ -50,12 +50,21 @@ def test_positional_and_optional_arguments(): } -def test_short_arg_with_multiple_options(): - args, kwargs = parse_args(['-a', '-i', 'a', 'b']) +def test_short_arg_with_single_option(): + args, kwargs = parse_args(['-a', 'b', '--abc', 'd']) assert args == [] assert kwargs == { - 'a': True, - 'i': ['a', 'b'] + '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']) assert args == [] assert kwargs == { - 'input_file': ['a.py'], - 'output_file': ['b.py'] + 'input_file': 'a.py', + 'output_file': 'b.py' } @@ -80,7 +89,7 @@ def test_unintended_hyphen(): { 'flag': True, 'optional': ['value', 'test'], - 'output_file': ['filename'], + 'output_file': 'filename', 'f': True, 'l': True, 'g': ['name', 'name2'],