-
Notifications
You must be signed in to change notification settings - Fork 2
/
argsparser.py
50 lines (38 loc) · 1.47 KB
/
argsparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import argparse
from shared import Mode, Debug
# instance of parser for reading cli arguments
parser = argparse.ArgumentParser("Optical graph recognition")
parser.add_argument("-p", "--path", help="Absolute path to input image", required=True)
parser.add_argument("-m", "--mode", help=Mode.HELP, choices=Mode.CHOICES, default=Mode.DEFAULT, type=str.lower)
parser.add_argument("-d", "--debug", help=Debug.HELP, choices=Debug.CHOICES, default=Debug.DEFAULT, type=str.lower)
def parse_argument(args) -> (int, str, str):
"""
Parses the command line arguments
:param: args: Command line arguments
:return: mode, path to photo, path to save the result
"""
save_path = parse_path(args.path)
mode = Mode.get_mode(args.mode)
debug = Debug.get_debug(args.debug)
return mode, debug, args.path, save_path
def parse_path(file_path: str) -> str:
"""
Checks the path to the photo and specifies the path to save
:param: file_path: path to photo
:return: path to save the result
"""
file_path.replace(" ", "")
if file_path.count('.') != 1:
print("1: File path is incorrect. Must be only one dot.")
return ''
head, tail = os.path.split(file_path)
if len(tail) == 0:
print("1: File name no exist")
return ''
file_name, file_ext = os.path.splitext(tail)
if len(file_name) == 0:
print("1: File name not found")
return ''
save_path = head + '/' + file_name
return save_path