66from argparse import ArgumentParser
77from getpass import getpass
88from sys import exit
9+ from re import compile , IGNORECASE
910
1011def check_address (address ):
1112 if not address or address .strip () == "" :
1213 raise ValueError ('address required' )
13- elif len (address ) < 4 :
14- raise ValueError ("address too short" )
14+
15+ # These regex come from Django project
16+ # https://github.com/django/django/blob/master/django/core/validators.py
17+ ul = '\u00a1 -\uffff '
18+ ipv4_re = r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
19+ hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9])?'
20+ domain_re = r'(?:\.[a-z' + ul + r'0-9]+(?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9]+)*)*'
21+ tld_re = r'\.[a-z' + ul + r']{2,}\.?'
22+ host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
23+ regex = compile (
24+ r'(?:' + ipv4_re + '|' + host_re + ')'
25+ r'$' , IGNORECASE
26+ )
27+
28+ if not regex .match (address ):
29+ raise ValueError ('not a valid address' )
1530
1631def check_username (username ):
1732 if not username or username .strip () == "" :
@@ -49,7 +64,7 @@ def check_prompt_field(checker, field = "", password = False):
4964ap .add_argument ("-u" , "--user" , help = "username to use" )
5065ap .add_argument ("-p" , "--password" , help = "password to use" )
5166ap .add_argument ("-P" , "--prompt-for-password" , dest = "prompt_pwd" , action = "store_true" , help = "prompt for password to use" )
52- ap .add_argument ("-a" , "--address" , help = "root address of the WordPress blog (example : 'blog.example.net')" )
67+ ap .add_argument ("-a" , "--address" , help = "root address of the WordPress blog (examples : 'blog.example.net' or '192.168.20.53 ')" )
5368ap .add_argument ("--http" , dest = "https" , action = "store_false" , help = "use HTTP as protocol" )
5469ap .add_argument ("--https" , dest = "https" , action = "store_true" , help = "use HTTPS as protocol (default)" )
5570ap .add_argument ("-v" , "--version" , action = "version" , version = "%(prog)s 1.1.0" )
@@ -59,7 +74,7 @@ def check_prompt_field(checker, field = "", password = False):
5974
6075try :
6176 if not args ["address" ]:
62- address = check_prompt_field (check_address , 'URL : ' )
77+ address = check_prompt_field (check_address , 'Address : ' )
6378 else :
6479 address = check_field (check_address , args ["address" ])
6580
0 commit comments