Skip to content

Commit c06c396

Browse files
committed
refactor: Simplify menu display and add flag support for direct menu selection, version display, and update checks
1 parent 780596a commit c06c396

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

bugscanx/main.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sys
2+
from argparse import ArgumentParser
3+
from importlib import import_module, metadata
24
from rich import print
35
from bugscanx import clear_screen, banner, text_ascii
46

@@ -19,38 +21,44 @@
1921

2022
def display_menu():
2123
banner()
22-
for key, (desc, color) in MENU_OPTIONS.items():
23-
padding = ' ' if len(key) == 1 else ''
24-
print(f"[{color}] [{key}]{padding} {desc}")
24+
print("\n".join(f"[{color}] [{k}]{' ' if len(k)==1 else ''} {desc}"
25+
for k, (desc, color) in MENU_OPTIONS.items()))
2526

26-
def run_menu_option(choice):
27+
def run_option(choice):
2728
if choice == '12':
2829
return False
30+
if choice not in MENU_OPTIONS:
31+
return True
2932

3033
clear_screen()
3134
text_ascii(MENU_OPTIONS[choice][0], color="bold magenta")
3235

3336
try:
34-
module = __import__('bugscanx.entrypoints.runner', fromlist=[f'run_{choice}'])
35-
getattr(module, f'run_{choice}')()
37+
getattr(import_module('bugscanx.entrypoints.runner'), f'run_{choice}')()
38+
print("\n[yellow] Press Enter to continue...", end="")
3639
except KeyboardInterrupt:
3740
print("\n[yellow] Operation cancelled by user.")
38-
39-
print("\n[yellow] Press Enter to continue...", end="")
40-
input()
4141
return True
4242

4343
def main():
44+
parser = ArgumentParser()
45+
parser.add_argument('-v', '--version', action='store_true')
46+
parser.add_argument('-u', '--update', action='store_true')
47+
parser.add_argument('option', nargs='?')
48+
args = parser.parse_args()
49+
50+
if args.version:
51+
print(f"[bold cyan]BugScanX version {metadata.version('bugscan-x')}[/bold cyan]")
52+
return
53+
if args.update:
54+
return run_option('11')
55+
if args.option:
56+
return 0 if run_option(args.option) else 1
57+
4458
try:
4559
while True:
4660
display_menu()
47-
choice = input("\n\033[36m [-] Your Choice: \033[0m")
48-
49-
if choice not in MENU_OPTIONS:
50-
continue
51-
52-
if not run_menu_option(choice):
61+
if not run_option(input("\n\033[36m [-] Your Choice: \033[0m")):
5362
break
54-
5563
except KeyboardInterrupt:
56-
sys.exit()
64+
sys.exit(0)

0 commit comments

Comments
 (0)