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):]:
|
||||
if not arg.startswith('-'):
|
||||
values.append(arg)
|
||||
else:
|
||||
if len(values):
|
||||
continue
|
||||
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
|
||||
values = []
|
||||
key = arg[:2].lstrip('-') + arg[2:].replace('-', '_')
|
||||
if arg.startswith('--'):
|
||||
if '=' in arg:
|
||||
key, value = key.split('=')
|
||||
values.append(value)
|
||||
kwargs[key] = values
|
||||
else:
|
||||
kwargs[key] = True
|
||||
elif arg.startswith('-'):
|
||||
for k in key:
|
||||
kwargs[k] = True
|
||||
key = arg[-1]
|
||||
else:
|
||||
kwargs[key] = True
|
||||
elif arg.startswith('-'):
|
||||
for k in key:
|
||||
kwargs[k] = True
|
||||
key = arg[-1]
|
||||
if len(values):
|
||||
kwargs[key] = values
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ def test_no_argv():
|
||||
assert args == []
|
||||
assert kwargs == {}
|
||||
|
||||
|
||||
def test_use_sys_argv_by_default():
|
||||
with mock.patch('sys.argv', ['app', 'command', '--option=one', 'two', '--flag', '-io']):
|
||||
args, kwargs = parse_args()
|
||||
@ -22,30 +21,30 @@ def test_use_sys_argv_by_default():
|
||||
'o': True
|
||||
}
|
||||
|
||||
|
||||
def test_single_positional_argument():
|
||||
args, kwargs = parse_args(['app'])
|
||||
assert args == ['app']
|
||||
assert kwargs == {}
|
||||
|
||||
|
||||
def test_single_positional_argument_with_underscore():
|
||||
args, kwargs = parse_args(['the-app'])
|
||||
assert args == ['the_app']
|
||||
assert kwargs == {}
|
||||
|
||||
|
||||
def test_positional_arguments():
|
||||
args, kwargs = parse_args(['app', 'command'])
|
||||
assert args == ['app', 'command']
|
||||
assert kwargs == {}
|
||||
|
||||
|
||||
def test_one_positional_and_optional_argument():
|
||||
args, kwargs = parse_args(['app', '--help'])
|
||||
assert args == ['app']
|
||||
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():
|
||||
args, kwargs = parse_args(['app', 'command', '--inputfile', '--outputfile'])
|
||||
@ -55,7 +54,6 @@ def test_positional_and_optional_arguments():
|
||||
'outputfile': True
|
||||
}
|
||||
|
||||
|
||||
def test_short_arg_with_single_option():
|
||||
args, kwargs = parse_args(['-a', 'b', '--abc', 'd'])
|
||||
assert args == []
|
||||
@ -64,7 +62,6 @@ def test_short_arg_with_single_option():
|
||||
'abc': 'd',
|
||||
}
|
||||
|
||||
|
||||
def test_short_arg_with_multiple_options():
|
||||
args, kwargs = parse_args(['-i', 'a', 'b', '--input', 'c', 'd'])
|
||||
assert args == []
|
||||
@ -73,7 +70,6 @@ def test_short_arg_with_multiple_options():
|
||||
'input': ['c', 'd']
|
||||
}
|
||||
|
||||
|
||||
def test_long_args_with_equals():
|
||||
args, kwargs = parse_args(['--input-file=a.py', '--output-file=b.py'])
|
||||
assert args == []
|
||||
@ -82,13 +78,11 @@ def test_long_args_with_equals():
|
||||
'output_file': 'b.py'
|
||||
}
|
||||
|
||||
|
||||
def test_unintended_hyphen():
|
||||
args, kwargs = parse_args(['---triple-hyphen-'])
|
||||
assert args == []
|
||||
assert kwargs == {'_triple_hyphen_': True}
|
||||
|
||||
|
||||
@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'],
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user