Minor code improvements and a new test
This commit is contained in:
parent
6488a3d40a
commit
a077a6b569
30
pargv.py
30
pargv.py
@ -22,22 +22,22 @@ def parse_args(argv=None):
|
|||||||
for arg in argv[len(args):]:
|
for arg in argv[len(args):]:
|
||||||
if not arg.startswith('-'):
|
if not arg.startswith('-'):
|
||||||
values.append(arg)
|
values.append(arg)
|
||||||
else:
|
continue
|
||||||
if len(values):
|
if len(values):
|
||||||
|
kwargs[key] = values
|
||||||
|
values = []
|
||||||
|
key = arg[:2].lstrip('-') + arg[2:].replace('-', '_')
|
||||||
|
if arg.startswith('--'):
|
||||||
|
if '=' in arg:
|
||||||
|
key, value = key.split('=')
|
||||||
|
values.append(value)
|
||||||
kwargs[key] = values
|
kwargs[key] = values
|
||||||
values = []
|
else:
|
||||||
key = arg[:2].lstrip('-') + arg[2:].replace('-', '_')
|
kwargs[key] = True
|
||||||
if arg.startswith('--'):
|
elif arg.startswith('-'):
|
||||||
if '=' in arg:
|
for k in key:
|
||||||
key, value = key.split('=')
|
kwargs[k] = True
|
||||||
values.append(value)
|
key = arg[-1]
|
||||||
kwargs[key] = values
|
|
||||||
else:
|
|
||||||
kwargs[key] = True
|
|
||||||
elif arg.startswith('-'):
|
|
||||||
for k in key:
|
|
||||||
kwargs[k] = True
|
|
||||||
key = arg[-1]
|
|
||||||
if len(values):
|
if len(values):
|
||||||
kwargs[key] = values
|
kwargs[key] = values
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,6 @@ def test_no_argv():
|
|||||||
assert args == []
|
assert args == []
|
||||||
assert kwargs == {}
|
assert kwargs == {}
|
||||||
|
|
||||||
|
|
||||||
def test_use_sys_argv_by_default():
|
def test_use_sys_argv_by_default():
|
||||||
with mock.patch('sys.argv', ['app', 'command', '--option=one', 'two', '--flag', '-io']):
|
with mock.patch('sys.argv', ['app', 'command', '--option=one', 'two', '--flag', '-io']):
|
||||||
args, kwargs = parse_args()
|
args, kwargs = parse_args()
|
||||||
@ -22,30 +21,30 @@ def test_use_sys_argv_by_default():
|
|||||||
'o': True
|
'o': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_single_positional_argument():
|
def test_single_positional_argument():
|
||||||
args, kwargs = parse_args(['app'])
|
args, kwargs = parse_args(['app'])
|
||||||
assert args == ['app']
|
assert args == ['app']
|
||||||
assert kwargs == {}
|
assert kwargs == {}
|
||||||
|
|
||||||
|
|
||||||
def test_single_positional_argument_with_underscore():
|
def test_single_positional_argument_with_underscore():
|
||||||
args, kwargs = parse_args(['the-app'])
|
args, kwargs = parse_args(['the-app'])
|
||||||
assert args == ['the_app']
|
assert args == ['the_app']
|
||||||
assert kwargs == {}
|
assert kwargs == {}
|
||||||
|
|
||||||
|
|
||||||
def test_positional_arguments():
|
def test_positional_arguments():
|
||||||
args, kwargs = parse_args(['app', 'command'])
|
args, kwargs = parse_args(['app', 'command'])
|
||||||
assert args == ['app', 'command']
|
assert args == ['app', 'command']
|
||||||
assert kwargs == {}
|
assert kwargs == {}
|
||||||
|
|
||||||
|
|
||||||
def test_one_positional_and_optional_argument():
|
def test_one_positional_and_optional_argument():
|
||||||
args, kwargs = parse_args(['app', '--help'])
|
args, kwargs = parse_args(['app', '--help'])
|
||||||
assert args == ['app']
|
assert args == ['app']
|
||||||
assert kwargs == {'help': True}
|
assert kwargs == {'help': True}
|
||||||
|
|
||||||
|
def test_one_positional_and_optional_argument_with_values():
|
||||||
|
args, kwargs = parse_args(['app', '--amount=2', '3'])
|
||||||
|
assert args == ['app']
|
||||||
|
assert kwargs == {'amount': ['2', '3']}
|
||||||
|
|
||||||
def test_positional_and_optional_arguments():
|
def test_positional_and_optional_arguments():
|
||||||
args, kwargs = parse_args(['app', 'command', '--inputfile', '--outputfile'])
|
args, kwargs = parse_args(['app', 'command', '--inputfile', '--outputfile'])
|
||||||
@ -55,7 +54,6 @@ def test_positional_and_optional_arguments():
|
|||||||
'outputfile': True
|
'outputfile': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_short_arg_with_single_option():
|
def test_short_arg_with_single_option():
|
||||||
args, kwargs = parse_args(['-a', 'b', '--abc', 'd'])
|
args, kwargs = parse_args(['-a', 'b', '--abc', 'd'])
|
||||||
assert args == []
|
assert args == []
|
||||||
@ -64,7 +62,6 @@ def test_short_arg_with_single_option():
|
|||||||
'abc': 'd',
|
'abc': 'd',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_short_arg_with_multiple_options():
|
def test_short_arg_with_multiple_options():
|
||||||
args, kwargs = parse_args(['-i', 'a', 'b', '--input', 'c', 'd'])
|
args, kwargs = parse_args(['-i', 'a', 'b', '--input', 'c', 'd'])
|
||||||
assert args == []
|
assert args == []
|
||||||
@ -73,7 +70,6 @@ def test_short_arg_with_multiple_options():
|
|||||||
'input': ['c', 'd']
|
'input': ['c', 'd']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_long_args_with_equals():
|
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 == []
|
||||||
@ -82,13 +78,11 @@ def test_long_args_with_equals():
|
|||||||
'output_file': 'b.py'
|
'output_file': 'b.py'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_unintended_hyphen():
|
def test_unintended_hyphen():
|
||||||
args, kwargs = parse_args(['---triple-hyphen-'])
|
args, kwargs = parse_args(['---triple-hyphen-'])
|
||||||
assert args == []
|
assert args == []
|
||||||
assert kwargs == {'_triple_hyphen_': True}
|
assert kwargs == {'_triple_hyphen_': True}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('argv, args, kwargs', ((
|
@pytest.mark.parametrize('argv, args, kwargs', ((
|
||||||
['/pargv/pargv.py', 'command', 'positional', '--flag', '--optional=value', 'test', '--output-file', 'filename', '-flg', 'name', 'name2'],
|
['/pargv/pargv.py', 'command', 'positional', '--flag', '--optional=value', 'test', '--output-file', 'filename', '-flg', 'name', 'name2'],
|
||||||
['/pargv/pargv.py', 'command', 'positional'],
|
['/pargv/pargv.py', 'command', 'positional'],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user