2
2
Generate two factor authentication codes on the command line
3
3
"""
4
4
import argparse
5
+ import sys
5
6
from pyauthenticator .share import (
6
7
list_services ,
7
8
load_config ,
11
12
)
12
13
13
14
14
- def command_line_parser ():
15
+ def command_line_parser (cmd_args = None ):
15
16
"""
16
17
Main function primarly used for the command line interface
17
18
"""
19
+ if cmd_args is None :
20
+ cmd_args = sys .argv [1 :]
18
21
parser = argparse .ArgumentParser (prog = "pyauthenticator" )
19
22
config_dict = load_config ()
20
- parser .add_argument (
21
- "service" ,
22
- help = "Service to generate optauth code for. Available services are: "
23
- + str (list_services (config_dict = config_dict )),
24
- )
23
+ if len (config_dict ) > 0 :
24
+ parser .add_argument (
25
+ "service" ,
26
+ help = "Service to generate optauth code for. The config file ~/.pyauthenticator contains the following services: "
27
+ + ", " .join (list_services (config_dict = config_dict )),
28
+ )
29
+ else :
30
+ parser .add_argument (
31
+ "service" ,
32
+ help = "Service to generate optauth code for. Currently no service is defined in the ~/.pyauthenticator config file." ,
33
+ )
25
34
parser .add_argument (
26
35
"-qr" ,
27
36
"--qrcode" ,
@@ -31,18 +40,41 @@ def command_line_parser():
31
40
parser .add_argument (
32
41
"-a" ,
33
42
"--add" ,
34
- help = "Add service by providing the qrcode png file as additional argument." ,
43
+ help = "Add service by providing the < qrcode. png> file as additional argument." ,
35
44
)
36
- args = parser .parse_args ()
45
+ args = parser .parse_args (args = cmd_args )
37
46
if args .qrcode :
38
47
generate_qrcode (key = args .service , config_dict = config_dict )
48
+ print ("The qrcode file <" + args .service + ".png> was generated." )
39
49
elif args .add :
40
50
add_service (
41
51
key = args .service , qrcode_png_file_name = args .add , config_dict = config_dict
42
52
)
43
- print (args .service , "added." )
53
+ print (
54
+ "The service '"
55
+ + args .service
56
+ + "' was added, from file <"
57
+ + args .add
58
+ + ">."
59
+ )
44
60
else :
45
- print (get_two_factor_code (key = args .service , config_dict = config_dict ))
61
+ try :
62
+ print (get_two_factor_code (key = args .service , config_dict = config_dict ))
63
+ except ValueError :
64
+ if len (config_dict ) > 0 :
65
+ print (
66
+ 'The service "'
67
+ + args .service
68
+ + '" does not exist.\n \n The config file ~/.pyauthenticator contains the following services:'
69
+ )
70
+ for service in list_services (config_dict = config_dict ):
71
+ print (" * " + service )
72
+ print ("\n Choose one of these or add a new service using:" )
73
+ else :
74
+ print (
75
+ "The config file ~/.pyauthenticator does not contain any services. To add a new service use:"
76
+ )
77
+ print (" pyauthenticator --add <qr-code.png> <servicename>\n " )
46
78
47
79
48
80
if __name__ == "__main__" :
0 commit comments