Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cbb3e36127 | |||
| a19fb22f54 | |||
| 1f9e42f222 |
@ -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', '-ab', 'one', 'two']`
|
`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:
|
||||||
|
|
||||||
@ -52,3 +52,4 @@ kwargs = {
|
|||||||
- One value of an option is stored as a string, multiple values are stored as a list
|
- 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
|
- 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)
|
- 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)
|
||||||
|
- For multiple short options such as `-vao=file.ext` or `-vao file.ext`, the last option (`o`) is set to the specified value, while other options (`v` and `a`) are set to `True`.
|
||||||
|
|||||||
2
pargv.py
2
pargv.py
@ -1,7 +1,7 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
__version__ = '0.2.0'
|
__version__ = '0.2.1'
|
||||||
|
|
||||||
|
|
||||||
def parse_args(argv=None):
|
def parse_args(argv=None):
|
||||||
|
|||||||
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ with open('README.md') as f:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='pargv',
|
name='pargv',
|
||||||
version='0.2.0',
|
version='0.2.1',
|
||||||
description='Parse command line arguments into a list of args and a dict of kwargs.',
|
description='Parse command line arguments into a list of args and a dict of kwargs.',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
|
|||||||
60
tests/test_parse_args.py
Normal file
60
tests/test_parse_args.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
from pargv import parse_args
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def test_io():
|
||||||
|
for (argv, args, kwargs) in (
|
||||||
|
# Arguments
|
||||||
|
('name', ['name'], {}),
|
||||||
|
('name command', ['name', 'command'], {}),
|
||||||
|
# Hyphens
|
||||||
|
('a-b', ['a_b'], {}),
|
||||||
|
('---a', [], {'_a': True}),
|
||||||
|
('-a-', [], {'a': True, '_': True}),
|
||||||
|
# Options
|
||||||
|
('--option', [], {'option': True}),
|
||||||
|
('--option=2', [], {'option': '2'}),
|
||||||
|
('--option=2 4', [], {'option': ['2', '4']}),
|
||||||
|
('--option 2', [], {'option': '2'}),
|
||||||
|
('--option 2 4', [], {'option': ['2', '4']}),
|
||||||
|
# Short options
|
||||||
|
('-o', [], {'o': True}),
|
||||||
|
('-o=2', [], {'o': '2'}),
|
||||||
|
('-o=2 4', [], {'o': ['2', '4']}),
|
||||||
|
('-o 2', [], {'o': '2'}),
|
||||||
|
('-o 2 4', [], {'o': ['2', '4']}),
|
||||||
|
# Multiple short options
|
||||||
|
('-ox', [], {'o': True, 'x': True}),
|
||||||
|
('-ox=2', [], {'o': True, 'x': '2'}),
|
||||||
|
('-ox=2 4', [], {'o': True, 'x': ['2', '4']}),
|
||||||
|
('-ox 2', [], {'o': True, 'x': '2'}),
|
||||||
|
('-ox 2 4', [], {'o': True, 'x': ['2', '4']}),
|
||||||
|
):
|
||||||
|
assert parse_args(argv.split()) == (args, kwargs)
|
||||||
|
|
||||||
|
def test_empty_input():
|
||||||
|
for argv in (
|
||||||
|
'',
|
||||||
|
[],
|
||||||
|
{},
|
||||||
|
None,
|
||||||
|
False,
|
||||||
|
):
|
||||||
|
args, kwargs = parse_args(argv)
|
||||||
|
assert (len(args), len(kwargs)) == (1, 0)
|
||||||
|
|
||||||
|
def test_type_errors():
|
||||||
|
for argv in (
|
||||||
|
1,
|
||||||
|
True,
|
||||||
|
{'a': 1},
|
||||||
|
):
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
parse_args(argv)
|
||||||
|
|
||||||
|
def test_attribute_errors():
|
||||||
|
for argv in (
|
||||||
|
[1],
|
||||||
|
):
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
parse_args(argv)
|
||||||
Loading…
x
Reference in New Issue
Block a user