Skip to content

Commit

Permalink
refactor main, bump version, added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
bet0x committed Sep 17, 2023
1 parent 7c54927 commit a9c2e3e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
26 changes: 14 additions & 12 deletions manwrapper/main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import subprocess
import sys

def main():
if len(sys.argv) < 2 or sys.argv[1] in ['-h', '--help']:
print("Usage: manwrapper <topic>")
print("Please provide a topic for the manual.")
sys.exit(1)

manTopic = sys.argv[1]

cmd = ['man', '-P', 'cat', manTopic]

def get_man_page(topic):
cmd = ['man', '-P', 'cat', topic]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()

encoding = sys.getdefaultencoding()

if stdout:
print(stdout.decode(encoding))
return stdout.decode(encoding)
if stderr:
print("Error:", stderr.decode(encoding))
return "Error: " + stderr.decode(encoding)

def main():
if len(sys.argv) < 2 or sys.argv[1] in ['-h', '--help']:
print("Usage: manwrapper <topic>")
print("Please provide a topic for the manual.")
sys.exit(1)

manTopic = sys.argv[1]
output = get_man_page(manTopic)
print(output)

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="manwrapper",
version="0.2",
version="0.4",
description="A simple command-line tool to view man pages.",
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
Expand Down
Empty file added tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/test_manwrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# test_manwrapper.py

from manwrapper.main import get_man_page

def test_get_man_page():
# Test a known command
result = get_man_page('ls')
assert "list directory contents" in result

# Test an unknown command
result = get_man_page('nonexistentcommand')
assert "Error:" in result

0 comments on commit a9c2e3e

Please sign in to comment.