Created
April 18, 2020 16:39
-
-
Save ndamulelonemakh/e507c68fb2b3d9f223d6c2d723161303 to your computer and use it in GitHub Desktop.
Creating python console executables with pyinstaller and argparse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| A simple python script that uses the psutil package to show processes running on a host machine. | |
| """ | |
| __version__ = '1.0.0' | |
| __author__ = 'Ndamulelo N [email protected]' | |
| import psutil | |
| import argparse | |
| """ | |
| We use argeparse to allow the user to specify the limit on the number of processes to show. | |
| Example usage: $ python proclist.py --limit 3 | |
| """ | |
| parser = argparse.ArgumentParser(prog='ProcList', | |
| description='Simple console application that show process info on the host machine') | |
| parser.add_argument('--limit', type=int, help='Maximum number of processes to show') | |
| arguments = parser.parse_args() | |
| DEFAULT_LIMIT = 5 | |
| def main(): | |
| limit = arguments.limit or DEFAULT_LIMIT | |
| for idx, proc in enumerate(psutil.process_iter(['pid', 'name'])): | |
| print(proc.info) | |
| if idx >= limit: | |
| break | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment