64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import sys
|
|
import pytest
|
|
from unittest import mock
|
|
from pargv import parse_args
|
|
|
|
|
|
def test_no_argv():
|
|
with mock.patch('sys.argv', []):
|
|
assert parse_args() == ([], {})
|
|
|
|
def test_use_sys_argv_by_default():
|
|
with mock.patch('sys.argv', ['argument', '--flag']):
|
|
assert parse_args() == (['argument'], {'flag': True})
|
|
|
|
def test_single_positional_argument():
|
|
assert parse_args(['argument']) == (['argument'], {})
|
|
|
|
def test_single_positional_argument_with_underscore():
|
|
assert parse_args(['the-argument']) == (['the_argument'], {})
|
|
assert parse_args(['a-b']) == (['a_b'], {})
|
|
|
|
def test_optional_argument_with_underscore():
|
|
assert parse_args(['--a-b']) == ([], {'a_b': True})
|
|
|
|
def test_positional_arguments():
|
|
assert parse_args(['argument', 'command']) == (['argument', 'command'], {})
|
|
|
|
def test_one_positional_and_optional_argument():
|
|
assert parse_args(['argument', '--help']) == (['argument'], {'help': True})
|
|
|
|
def test_one_positional_and_optional_argument_with_values():
|
|
assert parse_args(['argument', '--amount=2', '3']) == (['argument'], {'amount': ['2', '3']})
|
|
|
|
def test_short_arg_with_single_option():
|
|
assert parse_args(['-a', 'b', '--abc', 'd']) == ([], {'a': 'b', 'abc': 'd'})
|
|
|
|
def test_short_arg_with_multiple_options():
|
|
assert parse_args(['-i', 'a', 'b']) == ([], {'i': ['a', 'b']})
|
|
|
|
def test_short_args_with_equals():
|
|
assert parse_args(['-ab=c']) == ([], {'a': True, 'b': 'c'})
|
|
assert parse_args(['-ab=c', 'd']) == ([], {'a': True, 'b': ['c', 'd']})
|
|
|
|
def test_long_args_with_equals():
|
|
assert parse_args(['--input-file=a.py']) == ([], {'input_file': 'a.py'})
|
|
|
|
def test_unintended_hyphen():
|
|
assert parse_args(['---triple-hyphen-']) == ([], {'_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'],
|
|
{
|
|
'flag': True,
|
|
'optional': ['value', 'test'],
|
|
'output_file': 'filename',
|
|
'f': True,
|
|
'l': True,
|
|
'g': ['name', 'name2'],
|
|
}
|
|
),))
|
|
def test_all_parameters_at_once(argv, args, kwargs):
|
|
assert parse_args(argv) == (args, kwargs)
|