Skip to content

Commit

Permalink
Merge pull request #43 from standage/cli/entry
Browse files Browse the repository at this point in the history
New CLI entry point
  • Loading branch information
standage committed Dec 20, 2016
2 parents 0f50a53 + 1e330e0 commit 30ba88c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Changed
- CLI implemented using `entry_points` instead of a dedicated script.

## [0.1.0] - 2016-12-16
### Added
- Basic data structures
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ devenv:
pip install pytest pytest-cov pep8 sphinx

style:
pep8 tag/*.py tests/*.py tag/cli/*.py bin/tag
pep8 tag/*.py tests/*.py tag/cli/*.py

loc:
cloc --exclude-list-file=<(echo tag/_version.py) tag/*.py
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author_email='daniel.standage@gmail.com',
license='BSD-3',
packages=['tag', 'tag.cli'],
scripts=list(glob.glob('bin/tag')),
entry_points={'console_scripts': ['tag = tag.__main__:main']},
install_requires=['intervaltree'],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
17 changes: 12 additions & 5 deletions bin/tag → tag/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
}


def main(args):
def main(args=None):
"""
Entry point for the tag CLI.
Isolated as a method so that the CLI can be called by other Python code
(e.g. for testing), in which case the arguments are passed to the function.
If no arguments are passed to the function, parse them from the command
line.
"""
if args is None:
args = tag.cli.parser().parse_args()

assert args.cmd in mains
mainmethod = mains[args.cmd]
mainmethod(args)


if __name__ == '__main__':
main(tag.cli.parser().parse_args())
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def test_cli_args():
sys.stdout = oldstdout
sys.stderr = oldstderr

parser = tag.cli.parser()
args = parser.parse_args(['gff3', '-r', 'tests/testdata/mito-trna.gff3'])
args.out = StringIO()
tag.__main__.main(args)
testout = tag.pkgdata('mito-trna-out.gff3').read()
assert args.out.getvalue() == testout

with pytest.raises(SystemExit):
tag.__main__.main()


def test_gff3_strict():
args = type('', (), {})()
Expand Down

0 comments on commit 30ba88c

Please sign in to comment.