Skip to content

Commit fbf1f92

Browse files
committed
feat: using argparse to create a CLI:
this feature will make user to use the script from command line with some arguments that specified by the user. with this aproach there is no need to prompt user for any input. script will use the config.ini if user does not pass any args, or user can choose other path for config.ini or pass each args with coresponding switch. if any switch does not pass to the script, script will use the default value in define in the script it self.
1 parent 7121767 commit fbf1f92

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

start.py

+38-29
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
from typing import Pattern, AnyStr, List
1717
import curses
1818
import ssl
19+
import argparse
1920

2021
print_ping_error_message = False # initialize flag variable
2122
openssl_is_active = False
2223

24+
parser = argparse.ArgumentParser(description="Cloudflare's IP Scanner in Python") # Create argument parser instance
25+
2326
try:
2427
import ping3
2528
except ImportError:
@@ -28,10 +31,6 @@
2831

2932
# Main function
3033
def main():
31-
32-
# Check for custom config file in command-line arguments
33-
customconfig = False if len(sys.argv) <= 1 else os.path.exists(sys.argv[1])
34-
3534
DEFAULT_MAX_IP = 50
3635
DEFAULT_MAX_PING = 500
3736
DEFAULT_MAX_JITTER = 100
@@ -42,9 +41,27 @@ def main():
4241
DEFAULT_MIN_DOWNLOAD_SPEED = 3
4342
DEFAULT_MIN_UPLOAD_SPEED = 0.2
4443

44+
# Create a CLI to the values from command line
45+
parser.add_argument("--custom-config", type=str, help="Path to the custom config file")
46+
parser.add_argument("--max-ip", type=int, help=f"Specify max IP [{DEFAULT_MAX_IP}]: ", default=DEFAULT_MAX_IP)
47+
parser.add_argument("--max-ping", type=int, help=f"Specify max Ping [{DEFAULT_MAX_PING}]: ", default=DEFAULT_MAX_PING)
48+
parser.add_argument("--max-jitter", type=int, help=f"Specify max Jitter [{DEFAULT_MAX_JITTER}]: ", default=DEFAULT_MAX_JITTER)
49+
parser.add_argument("--max-latency", type=int, help=f"Specify max Latency [{DEFAULT_MAX_LATENCY}]: ", default=DEFAULT_MAX_LATENCY)
50+
parser.add_argument("--ip-include", type=str, help=f"Specify IPs to include (comma seperated, '-' to ignore) [{DEFAULT_IP_INCLUDE}]: ", default=DEFAULT_IP_INCLUDE)
51+
parser.add_argument("--ip-exclude", type=str, help=f"Specify IPs to exclude (comma seperated, '-' to ignore) [{DEFAULT_IP_EXCLUDE}]: ", default=DEFAULT_IP_EXCLUDE)
52+
parser.add_argument("--test-size", type=int, help=f"Specify test data size in KB [{DEFAULT_DOWNLOAD_SIZE_KB}]: ", default=DEFAULT_DOWNLOAD_SIZE_KB)
53+
parser.add_argument("--min-download-speed", type=int, help=f"Specify minimum download speed (Mbps) [{DEFAULT_MIN_DOWNLOAD_SPEED}]: ", default=DEFAULT_MIN_DOWNLOAD_SPEED)
54+
parser.add_argument("--min-upload-speed", type=int, help=f"Specify minimum upload speed (Mbps) [{DEFAULT_MIN_UPLOAD_SPEED}]: ", default=DEFAULT_MIN_UPLOAD_SPEED)
55+
parser.add_argument("--cf-email", type=str, help=f"Specify CloudFlare email")
56+
parser.add_argument("--cf-zone-id", type=str, help=f"Specify CloudFlare zone ID")
57+
parser.add_argument("--cf-api-key", type=str, help=f"Specify CloudFlare API key")
58+
parser.add_argument("--cf-sub-domain", type=str, help=f"Specify you active sub domain in CloudFlare")
59+
parser.add_argument("--cf-delete-extisting-record", action="store_true", default=False, help="delete extisting records of given subdomain before uploading the result to your Cloudflare(yes/no)(defaults to no)")
60+
args = parser.parse_args()
61+
4562
# Create a new configparser instance and load the configuration file
4663
config = configparser.ConfigParser()
47-
config.read(sys.argv[1] if customconfig else 'config.ini')
64+
config.read(args.custom_config if args.custom_config else 'config.ini')
4865

4966
# Get the values of the configuration variables, using default values if not available
5067
max_ip = int(config.get('DEFAULT', 'max_ip', fallback=DEFAULT_MAX_IP))
@@ -78,20 +95,17 @@ def main():
7895

7996

8097
try:
81-
8298
# If no custom config file was specified...
83-
if not customconfig:
84-
85-
# Prompt user for input with default values from configuration file
86-
max_ip = input(f"Enter max IP [{max_ip}]: ") or max_ip
87-
max_ping = input(f"Enter max ping [{max_ping}]: ") or max_ping
88-
max_jitter = input(f"Enter max jitter [{max_jitter}]: ") or max_jitter
89-
max_latency = input(f"Enter max latency [{max_latency}]: ") or max_latency
90-
ip_include = input(f"Enter IPs to include (comma seperated, '-' to ignore) [{ip_include}]: ") or ip_include
91-
ip_exclude = input(f"Enter IPs to exclude (comma seperated, '-' to ignore) [{ip_exclude}]: ") or ip_exclude
92-
test_size = input(f"Enter test data size in KB [{test_size}]: ") or test_size
93-
min_download_speed = input(f"Enter minimum download speed (Mbps) [{min_download_speed}]: ") or min_download_speed
94-
min_upload_speed = input(f"Enter minimum upload speed (Mbps) [{min_upload_speed}]: ") or min_upload_speed
99+
if not args.custom_config:
100+
max_ip = args.max_ip
101+
max_ping = args.max_ping
102+
max_jitter = args.max_jitter
103+
max_latency = args.max_latency
104+
ip_include = args.ip_include
105+
ip_exclude = args.ip_exclude
106+
test_size = args.test_size
107+
min_download_speed = args.min_download_speed
108+
min_upload_speed = args.min_upload_speed
95109

96110
# Clear the include regex in case "-" provided by the user
97111
if ip_include == '-':
@@ -111,18 +125,13 @@ def main():
111125
min_upload_speed = float(min_upload_speed)
112126

113127

114-
# Prompt the user for whether they want to upload the result to their Cloudflare subdomain
115-
upload_results = input(f"Do you want to upload the result to your Cloudflare subdomain (yes/no) [{upload_results}]? ") or upload_results
116-
117128
# Code block to execute if upload_results is 'y' or 'yes'
118-
if upload_results.lower() in ["y", "yes"]:
119-
delete_existing = input(f"Do you want to delete extisting records of given subdomain before uploading the result to your Cloudflare (yes/no) [{delete_existing}]? ") or delete_existing
120-
email = input(f"Cloudflare email [{email}]: ") or email
121-
zone_id = input(f"Cloudflare zone ID [{zone_id}]: ") or zone_id
122-
api_key = input(f"Cloudflare API key [{api_key}]: ") or api_key
123-
124-
# Prompt user to enter subdomain to modify
125-
subdomain = input(f"Subdomain to modify (i.e ip.my-domain.com) [{subdomain}]: ") or subdomain
129+
if args.cf_email and args.cf_zone_id and args.cf_api_key:
130+
delete_existing = args.cf_delete_extisting_record
131+
email = args.cf_email
132+
zone_id = args.cf_zone_id
133+
api_key = args.cf_api_key
134+
subdomain = args.cf_sub_domain
126135

127136
# Check if provided credentials are correct and retry if they are not
128137
while not validateCloudflareCredentials(email, api_key, zone_id):

0 commit comments

Comments
 (0)