Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of argparse for parsing command-line options #2

Merged
merged 1 commit into from
Jun 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use argparse
  • Loading branch information
DarkCeptor44 committed Jun 5, 2020
commit 95cecf1e86aa49d7bf822cce2f05de7d5ba6d933
14 changes: 8 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import argparse
from PIL import Image


Expand Down Expand Up @@ -55,18 +56,19 @@ def ascii_art(ascii_matrix, filename=None):


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Image2ASCII Converter')
parser.add_argument('filename', help='File to convert')
parser.add_argument('-f', help='If you want to store the ASCII as a txt file', action='store_true')
args = parser.parse_args()

filepath = sys.argv[1]
options = [opt for opt in sys.argv[1:] if opt.startswith("-")]

filepath = args.filename
filename = None

if '-f' in options:
if args.f:
filename = os.path.splitext(filepath)[0]

try:
image = Image.open(filepath)
except:
except Exception:
raise SystemExit(f"File was not found.")

pixel_matrix = build_pixel_matrix(image)
Expand Down