BASIC User Group > Code Challenges

Menu me this....

(1/9) > >>

Okay, so here's a coding challenge that can be used in the real world.  I had to do this for multiple OS's for work.

It's a terminal-based front-end to back up user data (I'm excluding the backup part).

The idea is to present the user/technician with a menu were they can select a user to backup.


* A menu with a title
* The ability to pass both the Menu Title and the Home Folder location where the user data is stored (under *nix this is typically "/home", on macOS/Windows it's "/Users")
* The ability to specify exclusions so that certain accounts/folders are NOT displayed
* The menu needs to list a number associated with the user id, and the user logon id itself
* A prompt that only accepts numerical input, within the range specified in the menu
* A warning message if the input is not in range
* Upon a successful selection, output the user id that was selected
Here is an implementation in Bash (more to come):

--- Code: Bash ---#!/bin/bash EXCLUDE='Shared|Guest' function SelectFolder() {    # PARAMETERS:    #    $1 - Menu Title    #    $2 - User Folders Location     clear    TITLE="$1"    TLEN=${#TITLE}     echo "$TITLE"    printf %${TLEN}s | tr " " "="    echo     USER_LOC=$(ls "$2" | egrep -v $EXCLUDE)     PS3="Select User: "     select USER_ACCOUNT in $USER_LOC; do        test -n "$USER_ACCOUNT" && break        echo ">>> Invalid Selection <<<"    done} # change the second parameter depending on OS...SelectFolder "Armando's Test Menu" "/Users" printf "\nYou Selected: $USER_ACCOUNT\n" 



(It's an OPEN FORUM after all!!!  8))

--- Code: Python ---#!/usr/bin/env python import glob,os,sys,time excluded = '''Shared              Guest'''.split()                          def clear():        sys.stderr.write("\x1b[2J\x1b[H") def SelectFolder(menu_title,path):         loop = True        selection = {}        x = [x for x in glob.glob(path+'/*') if os.path.isdir(x) and not (os.path.basename(x) in excluded)]        while loop:                clear()                print,"-")                 for cnt,item in enumerate(x):                        print '{}) {}'.format(cnt+1, os.path.basename(item))                        selection[str(cnt+1)] = item                 print "-"*30                print                choice = raw_input('Select a User Folder: ')                 try:                        selected = selection[choice]                        loop = False                except KeyError:                        print ">> Invalid Selection <<"                        time.sleep(1)                        continue         return selected # change second parameter according to OS...selected = SelectFolder(" Armando's Test Menu ",'/Users')print "\nYou Selected: %s" % os.path.basename(selected) 

And for all the BASIC enthusiasts, a BACON VERSION  ;D

--- Code: Text ---DECLARE Exclude$ = "Guest|Shared" FUNCTION SelectFolder$(Title$, Folder$)    LOCAL Menu$ ASSOC STRING    LOCAL tLen,cnt,Amount TYPE int    LOCAL Border$ TYPE STRING    LOCAL Loop TYPE bool     tLen = LEN(Title$)    Loop = TRUE    Border$ = FILL$(LEN(Title$), 45 )     WHILE Loop        Count = 1        OPEN Folder$ FOR DIRECTORY AS mydir         CLEAR         PRINT Title$         PRINT Border$         REPEAT            GETFILE myfile$ FROM mydir            IF LEFT$(myfile$,1)== "." OR ISFALSE(LEN(myfile$)) OR REGEX(myfile$,Exclude$) THEN                CONTINUE            ELSE                Menu$(STR$(Count)) = myfile$                PRINT Count,") ", myfile$                INCR Count            ENDIF        UNTIL ISFALSE(LEN(myfile$))                CLOSE DIRECTORY mydir         PRINT Border$                Amount = NRKEYS(Menu$)        PRINT         INPUT "Select User: ",Selection$        blah = VAL(Selection$)        IF (blah < 1 OR blah > Amount) THEN            PRINT ">>> Invalid Selection <<"            SLEEP 1000            CONTINUE        ELSE            Loop = FALSE        ENDIF    WEND    RETURN Menu$(Selection$)END FUNCTION selected$ = SelectFolder$("Armando's Test Menu","/Users") PRINTPRINT "You Selected: ", selected$ 

Would it be okay if I submitted my Script BASIC cross platform command line backup select utility to accept a path/file mask as an argument so the utility could be used in a 'batch' file? STDIO

Sure, as long as the core requirements are met.


[0] Message Index

[#] Next page

Go to full version